π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[Fix] Fixed RandomCrop bug (#951)
shijianjian6749797b7dd0
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.pyindex 49624f7ab..a01a0eb4d 100644--- a/kornia/augmentation/augmentation.py+++ b/kornia/augmentation/augmentation.py@@ -786,12 +786,21 @@ class RandomCrop(AugmentationBase2D):def forward(self, input: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],params: Optional[Dict[str, torch.Tensor]] = None, return_transform: Optional[bool] = None) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:- if type(input) == tuple:- input = (self.precrop_padding(input[0]), input[1])+ if isinstance(input, (tuple, list)):+ _input = (self.precrop_padding(input[0]), input[1])else:- input = cast(torch.Tensor, input)- input = self.precrop_padding(input)- return super().forward(input, params, return_transform)+ input = cast(torch.Tensor, input) # TODO: weird that cast is not working under this context.+ _input = self.precrop_padding(input) # type: ignore+ out = super().forward(_input, params, return_transform)+ if not self._params['batch_prob'].all():+ # undo the pre-crop if nothing happened.+ if isinstance(out, tuple) and isinstance(input, tuple):+ return input[0], out[1]+ elif isinstance(out, tuple) and not isinstance(input, tuple):+ return input, out[1]+ else:+ return input+ return outclass RandomResizedCrop(AugmentationBase2D):diff --git a/kornia/augmentation/base.py b/kornia/augmentation/base.pyindex eafbdef1d..aa335fef6 100644--- a/kornia/augmentation/base.py+++ b/kornia/augmentation/base.py@@ -40,7 +40,7 @@ class _BasicAugmentationBase(nn.Module):self.p_batch = p_batchself.same_on_batch = same_on_batchself.keepdim = keepdim- self._params = None # type: Union[None,Dict[str, torch.Tensor]]+ self._params: Dict[str, torch.Tensor] = {}if p != 0. or p != 1.:self._p_gen = Bernoulli(self.p)if p_batch != 0. or p_batch != 1.:diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex 77fc38c45..f08133632 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -2056,6 +2056,36 @@ class TestRandomCrop:assert_allclose(out, expected, atol=1e-4, rtol=1e-4)+ def test_padding_no_forward(self, device, dtype):+ torch.manual_seed(0)+ inp = torch.tensor([[[+ [3., 4., 5.],+ [6., 7., 8.]+ ]]], device=device, dtype=dtype)+ trans = torch.tensor([[+ [1., 2., 3.],+ [3., 4., 5.],+ [6., 7., 8.]+ ]], device=device, dtype=dtype)+ # Not return transform+ rc = RandomCrop(size=(2, 3), padding=(0, 1, 2, 3), fill=9, align_corners=True, p=0.)++ out = rc(inp)+ assert_allclose(out, inp, atol=1e-4, rtol=1e-4)++ out = rc((inp, trans))+ assert_allclose(out[0], inp, atol=1e-4, rtol=1e-4)+ assert_allclose(out[1], trans, atol=1e-4, rtol=1e-4)++ # with return transform+ rc = RandomCrop(size=(2, 3), padding=(0, 1, 2, 3), fill=9, align_corners=True, p=0., return_transform=True)+ out = rc(inp)+ assert_allclose(out[0], inp, atol=1e-4, rtol=1e-4)++ out = rc((inp, trans))+ assert_allclose(out[0], inp, atol=1e-4, rtol=1e-4)+ assert_allclose(out[1], trans, atol=1e-4, rtol=1e-4)+def test_pad_if_needed(self, device, dtype):torch.manual_seed(0)batch_size = 2