Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Fix] 3d augmentations return wrong transformation matrix (#665)

FGeri69e02a44d927
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index fe7d822bb..516697c07 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -144,7 +144,7 @@ class RandomVerticalFlip(AugmentationBase):
(tensor([[[[0., 1., 1.],
[0., 0., 0.],
[0., 0., 0.]]]]), tensor([[[ 1., 0., 0.],
- [ 0., -1., 3.],
+ [ 0., -1., 2.],
[ 0., 0., 1.]]]))
"""
diff --git a/kornia/augmentation/augmentation3d.py b/kornia/augmentation/augmentation3d.py
index b71b9b1db..cf247afe2 100644
--- a/kornia/augmentation/augmentation3d.py
+++ b/kornia/augmentation/augmentation3d.py
@@ -54,7 +54,7 @@ class RandomHorizontalFlip3D(AugmentationBase3D):
<BLANKLINE>
[[0., 0., 1.],
[0., 1., 0.],
- [1., 0., 0.]]]]]), tensor([[[-1., 0., 0., 3.],
+ [1., 0., 0.]]]]]), tensor([[[-1., 0., 0., 2.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]]]))
@@ -111,7 +111,7 @@ class RandomVerticalFlip3D(AugmentationBase3D):
[[0., 0., 1.],
[0., 1., 0.],
[1., 0., 0.]]]]]), tensor([[[ 1., 0., 0., 0.],
- [ 0., -1., 0., 3.],
+ [ 0., -1., 0., 2.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]]]))
"""
@@ -167,7 +167,7 @@ class RandomDepthicalFlip3D(AugmentationBase3D):
[0., 1., 0.],
[0., 0., 1.]]]]]), tensor([[[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
- [ 0., 0., -1., 3.],
+ [ 0., 0., -1., 2.],
[ 0., 0., 0., 1.]]]))
"""
@@ -252,7 +252,7 @@ class RandomAffine3D(AugmentationBase3D):
Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float]]],
translate: Optional[Union[torch.Tensor, Tuple[float, float, float]]] = None,
scale: Optional[Union[torch.Tensor, Tuple[float, float],
- Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float]]]] = None,
+ Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float]]]] = None,
shears: Union[torch.Tensor, float, Tuple[float, float], Tuple[float, float, float, float, float, float],
Tuple[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float],
Tuple[float, float], Tuple[float, float]]] = None,
diff --git a/kornia/augmentation/functional3d.py b/kornia/augmentation/functional3d.py
index f6b9d6bcf..6f466053e 100644
--- a/kornia/augmentation/functional3d.py
+++ b/kornia/augmentation/functional3d.py
@@ -111,7 +111,7 @@ def compute_hflip_transformation3d(input: torch.Tensor, params: Dict[str, torch.
to_flip = params['batch_prob'].to(input.device)
trans_mat: torch.Tensor = torch.eye(4, device=input.device, dtype=input.dtype).repeat(input.shape[0], 1, 1)
w: int = input.shape[-1]
- flip_mat: torch.Tensor = torch.tensor([[-1, 0, 0, w],
+ flip_mat: torch.Tensor = torch.tensor([[-1, 0, 0, w - 1],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
@@ -168,7 +168,7 @@ def compute_vflip_transformation3d(input: torch.Tensor, params: Dict[str, torch.
h: int = input.shape[-2]
flip_mat: torch.Tensor = torch.tensor([[1, 0, 0, 0],
- [0, -1, 0, h],
+ [0, -1, 0, h - 1],
[0, 0, 1, 0],
[0, 0, 0, 1]])
@@ -226,7 +226,7 @@ def compute_dflip_transformation3d(input: torch.Tensor, params: Dict[str, torch.
d: int = input.shape[-3]
flip_mat: torch.Tensor = torch.tensor([[1, 0, 0, 0],
[0, 1, 0, 0],
- [0, 0, -1, d],
+ [0, 0, -1, d - 1],
[0, 0, 0, 1]])
trans_mat[to_flip] = flip_mat.type_as(input)
diff --git a/kornia/augmentation/mix_augmentation.py b/kornia/augmentation/mix_augmentation.py
index 6930efd27..5969e12ee 100644
--- a/kornia/augmentation/mix_augmentation.py
+++ b/kornia/augmentation/mix_augmentation.py
@@ -26,6 +26,7 @@ class MixAugmentation(AugmentationBase):
wont be concatenated.
"""
+
def __init__(self):
super(MixAugmentation, self).__init__(return_transform=False)
@@ -104,6 +105,7 @@ class RandomMixUp(MixAugmentation):
[0.4550, 0.5725, 0.4980]]]]), tensor([[0.0000, 0.0000, 0.1980],
[1.0000, 1.0000, 0.4162]]))
"""
+
def __init__(self, p: float = 1.0, lambda_val: Optional[Union[torch.Tensor, Tuple[float, float]]] = None,
same_on_batch: bool = False) -> None:
super(RandomMixUp, self).__init__()
@@ -192,6 +194,7 @@ class RandomCutMix(MixAugmentation):
[0.4294, 0.8854, 0.5739]]]]), tensor([[[0.0000, 0.0000, 0.4444],
[1.0000, 1.0000, 0.0000]]]))
"""
+
def __init__(self, height: int, width: int, p: float = 0.5, num_mix: int = 1,
cut_size: Optional[Union[torch.Tensor, Tuple[float, float]]] = None,
beta: Optional[Union[torch.Tensor, float]] = None, same_on_batch: bool = False) -> None:
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 4059564d9..e619f97bf 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -528,6 +528,7 @@ class Rescale(nn.Module):
Returns:
torch.Tensor: The rescaled tensor.
"""
+
def __init__(
self, factor: Union[float, Tuple[float, float]], interpolation: str = "bilinear", align_corners: bool = False
) -> None:
diff --git a/test/augmentation/test_augmentation_3d.py b/test/augmentation/test_augmentation_3d.py
index 5eac35608..286af2c11 100644
--- a/test/augmentation/test_augmentation_3d.py
+++ b/test/augmentation/test_augmentation_3d.py
@@ -51,7 +51,7 @@ class TestRandomHorizontalFlip3D:
expected = expected.to(device)
- expected_transform = torch.tensor([[-1., 0., 0., 4.],
+ expected_transform = torch.tensor([[-1., 0., 0., 3.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]) # 4 x 4
@@ -78,15 +78,15 @@ class TestRandomHorizontalFlip3D:
input = torch.tensor([[[[[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]]]]) # 1 x 1 x 4 x 4
+ [0., 1., 1.]]]]]) # 1 x 1 x 1 x 3 x 3
input = input.to(device)
expected = torch.tensor([[[[[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]]]]) # 1 x 1 x 4 x 4
+ [1., 1., 0.]]]]]) # 1 x 1 x 1 x 3 x 3
expected = expected.to(device)
- expected_transform = torch.tensor([[[-1., 0., 0., 3.],
+ expected_transform = torch.tensor([[[-1., 0., 0., 2.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
@@ -130,7 +130,7 @@ class TestRandomHorizontalFlip3D:
[0., 1., 1.]]]]) # 1 x 1 x 3 x 3
input = input.to(device)
- expected_transform = torch.tensor([[[-1., 0., 0., 3.],
+ expected_transform = torch.tensor([[[-1., 0., 0., 2.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
@@ -212,7 +212,7 @@ class TestRandomVerticalFlip3D:
expected = expected.to(device)
expected_transform = torch.tensor([[1., 0., 0., 0.],
- [0., -1., 0., 3.],
+ [0., -1., 0., 2.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]) # 4 x 4
expected_transform = expected_transform.to(device)
@@ -246,7 +246,7 @@ class TestRandomVerticalFlip3D:
expected = expected.to(device)
expected_transform = torch.tensor([[[1., 0., 0., 0.],
- [0., -1., 0., 3.],
+ [0., -1., 0., 2.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
expected_transform = expected_transform.to(device)
@@ -290,7 +290,7 @@ class TestRandomVerticalFlip3D:
input = input.to(device)
expected_transform = torch.tensor([[[1., 0., 0., 0.],
- [0., -1., 0., 3.],
+ [0., -1., 0., 2.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
expected_transform = expected_transform.to(device)
@@ -371,7 +371,7 @@ class TestRandomDepthicalFlip3D:
expected_transform = torch.tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
- [0., 0., -1., 2.],
+ [0., 0., -1., 1.],
[0., 0., 0., 1.]]) # 4 x 4
expected_transform = expected_transform.to(device)
@@ -412,7 +412,7 @@ class TestRandomDepthicalFlip3D:
expected_transform = torch.tensor([[[1., 0., 0., 0.],
[0., 1., 0., 0.],
- [0., 0., -1., 2.],
+ [0., 0., -1., 1.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
expected_transform = expected_transform.to(device)
@@ -459,7 +459,7 @@ class TestRandomDepthicalFlip3D:
expected_transform = torch.tensor([[[1., 0., 0., 0.],
[0., 1., 0., 0.],
- [0., 0., -1., 2.],
+ [0., 0., -1., 1.],
[0., 0., 0., 1.]]]) # 1 x 4 x 4
expected_transform = expected_transform.to(device)
diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.py
index 8aab54a56..51b6951d2 100644
--- a/test/augmentation/test_augmentation_mix.py
+++ b/test/augmentation/test_augmentation_mix.py
@@ -198,7 +198,7 @@ class TestRandomCutMix:
assert (out_label[:, :, 0] == label).all()
assert (out_label[:, :, 1] == torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]])).all()
assert_allclose(out_label[:, :, 2], torch.tensor([[0., 0.], [0., 0.], [0., 0.0833], [0., 0.], [0.5, 0.3333]],
- device=device, dtype=dtype), rtol=1e-4, atol=1e-4)
+ device=device, dtype=dtype), rtol=1e-4, atol=1e-4)
def test_random_mixup_same_on_batch(self, device, dtype):
torch.manual_seed(0)