Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Partial fix for #1165 (#1166)

hal-31402782167cad9
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index 562769402..9d6d220a2 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -89,8 +89,11 @@ class RandomHorizontalFlip(GeometricAugmentationBase2D):
def __repr__(self) -> str:
return self.__class__.__name__ + f"({super().__repr__()})"
+ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor]:
+ return dict(batch_shape=torch.as_tensor(batch_shape))
+
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
- w: int = input.shape[-1]
+ w: int = cast(int, params['batch_shape'][-1])
flip_mat: torch.Tensor = torch.tensor(
[[-1, 0, w - 1], [0, 1, 0], [0, 0, 1]], device=input.device, dtype=input.dtype
)
@@ -142,8 +145,11 @@ class RandomVerticalFlip(GeometricAugmentationBase2D):
def __repr__(self) -> str:
return self.__class__.__name__ + f"({super().__repr__()})"
+ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor]:
+ return dict(batch_shape=torch.as_tensor(batch_shape))
+
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
- h: int = input.shape[-2]
+ h: int = cast(int, params['batch_shape'][-2])
flip_mat: torch.Tensor = torch.tensor(
[[1, 0, 0], [0, -1, h - 1], [0, 0, 1]], device=input.device, dtype=input.dtype
)
diff --git a/kornia/augmentation/augmentation3d.py b/kornia/augmentation/augmentation3d.py
index 31a606d1a..ea48de31b 100644
--- a/kornia/augmentation/augmentation3d.py
+++ b/kornia/augmentation/augmentation3d.py
@@ -74,8 +74,11 @@ class RandomHorizontalFlip3D(AugmentationBase3D):
def __repr__(self) -> str:
return self.__class__.__name__ + f"({super().__repr__()})"
+ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor]:
+ return dict(batch_shape=torch.as_tensor(batch_shape))
+
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
- w: int = input.shape[-1]
+ w: int = cast(int, params['batch_shape'][-1])
flip_mat: torch.Tensor = torch.tensor(
[[-1, 0, 0, w - 1], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], device=input.device, dtype=input.dtype
)
@@ -144,8 +147,11 @@ class RandomVerticalFlip3D(AugmentationBase3D):
def __repr__(self) -> str:
return self.__class__.__name__ + f"({super().__repr__()})"
+ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor]:
+ return dict(batch_shape=torch.as_tensor(batch_shape))
+
def compute_transformation(self, input: torch.Tensor, params: Dict[str, torch.Tensor]) -> torch.Tensor:
- h: int = input.shape[-2]
+ h: int = cast(int, params['batch_shape'][-2])
flip_mat: torch.Tensor = torch.tensor(
[[1, 0, 0, 0], [0, -1, 0, h - 1], [0, 0, 1, 0], [0, 0, 0, 1]], device=input.device, dtype=input.dtype
)
diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.py
index 53750c6e8..03f786b74 100644
--- a/test/augmentation/test_container.py
+++ b/test/augmentation/test_container.py
@@ -233,6 +233,31 @@ class TestAugmentationSequential:
assert out.shape[-3:] == inp.shape[-3:]
reproducibility_test(inp, aug)
+ def test_random_flips(self, device, dtype):
+ inp = torch.randn(1, 3, 510, 1020, device=device, dtype=dtype)
+ bbox = torch.tensor([[[355, 10], [660, 10], [660, 250], [355, 250]]], device=device, dtype=dtype)
+
+ expected_bbox_vertical_flip = torch.tensor(
+ [[[355, 499], [660, 499], [660, 259], [355, 259]]], device=device, dtype=dtype
+ )
+ expected_bbox_horizontal_flip = torch.tensor(
+ [[[664, 10], [359, 10], [359, 250], [664, 250]]], device=device, dtype=dtype
+ )
+
+ aug_ver = K.AugmentationSequential(
+ K.RandomVerticalFlip(p=1.0), data_keys=["input", "bbox"], return_transform=False, same_on_batch=False
+ )
+
+ aug_hor = K.AugmentationSequential(
+ K.RandomHorizontalFlip(p=1.0), data_keys=["input", "bbox"], return_transform=False, same_on_batch=False
+ )
+
+ out_ver = aug_ver(inp, bbox)
+ out_hor = aug_hor(inp, bbox)
+
+ assert_close(out_ver[1], expected_bbox_vertical_flip)
+ assert_close(out_hor[1], expected_bbox_horizontal_flip)
+
@pytest.mark.parametrize('random_apply', [1, (2, 2), (1, 2), (2,), 10, True, False])
@pytest.mark.parametrize('return_transform', [True, False])
def test_forward_and_inverse(self, random_apply, return_transform, device, dtype):