Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Bug] Fix/random perspective returns wrong transformation matrix (#667)

FGerid051dd2c87b0
diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.py
index 75721fa30..e294890ee 100644
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@ -1,5 +1,6 @@
from typing import Tuple, List, Union, Dict, cast, Optional
+import kornia as K
import torch
import torch.nn as nn
@@ -473,8 +474,15 @@ def compute_perspective_transformation(input: torch.Tensor, params: Dict[str, to
"""
input = _transform_input(input)
_validate_input_dtype(input, accepted_dtypes=[torch.float16, torch.float32, torch.float64])
- transform: torch.Tensor = get_perspective_transform(
+ perspective_transform: torch.Tensor = get_perspective_transform(
params['start_points'], params['end_points']).type_as(input)
+
+ transform: torch.Tensor = K.eye_like(3, input)
+
+ to_transform = params['batch_prob'].to(input.device)
+
+ transform[to_transform] = perspective_transform[to_transform]
+
return transform
diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.py
index 57c83018d..8bf6daf6d 100644
--- a/test/augmentation/test_perspective.py
+++ b/test/augmentation/test_perspective.py
@@ -83,16 +83,48 @@ class TestRandomPerspective:
out_perspective = kornia.augmentation.RandomPerspective()(x_data)
assert out_perspective.shape == x_data.shape
- def test_transform_module(self, device):
+ def test_transform_module_should_return_identity(self, device):
+ torch.manual_seed(0)
x_data = torch.rand(1, 2, 4, 5).to(device)
- out_perspective = kornia.augmentation.RandomPerspective(
- return_transform=True)(x_data)
+ out_perspective = kornia.augmentation.RandomPerspective(p=0.0,
+ return_transform=True)(x_data)
+ assert isinstance(out_perspective, tuple)
+ assert len(out_perspective) == 2
+ assert out_perspective[0].shape == x_data.shape
+ assert out_perspective[1].shape == (1, 3, 3)
+ assert_allclose(out_perspective[0], x_data)
+ assert_allclose(out_perspective[1], torch.eye(3, device=device))
+
+ def test_transform_module_should_return_expected_transform(self, device):
+ torch.manual_seed(0)
+ x_data = torch.rand(1, 2, 4, 5).to(device)
+
+ expected_output = torch.tensor([[[[0.0000000000, 0.0000000000, 0.0000000000, 0.0197417457, 0.0429493971],
+ [0.0000000000, 0.5632190704, 0.5321710110, 0.3676981330, 0.1430126727],
+ [0.0000000000, 0.3082636893, 0.4031507671, 0.1760708243, 0.0000000000],
+ [0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000]],
+
+ [[0.0000000000, 0.0000000000, 0.0000000000, 0.1189093292, 0.0585946590],
+ [0.0000000000, 0.7087295055, 0.5419756770, 0.3995491862, 0.0863459259],
+ [0.0000000000, 0.2694899142, 0.5981453061, 0.5887590051, 0.0000000000],
+ [0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000, 0.0000000000]]]],
+ device=device, dtype=x_data.dtype)
+
+ expected_transform = torch.tensor([[[1.0522739887, 0.3492536247, 0.3045728207],
+ [-0.1066057906, 1.0426188707, 0.5845923424],
+ [0.0350575559, 0.1213315651, 1.0000000000]]],
+ device=device, dtype=x_data.dtype)
+
+ out_perspective = kornia.augmentation.RandomPerspective(p=1.0,
+ return_transform=True)(x_data)
assert isinstance(out_perspective, tuple)
assert len(out_perspective) == 2
assert out_perspective[0].shape == x_data.shape
assert out_perspective[1].shape == (1, 3, 3)
+ assert_allclose(out_perspective[0], expected_output)
+ assert_allclose(out_perspective[1], expected_transform)
def test_gradcheck(self, device):
input = torch.rand(1, 2, 5, 7).to(device)