Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Bug] Affine scale bug fix (#714)

shijianjianf73f845ef921
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index faa16f8c4..c95220759 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -352,16 +352,17 @@ class RandomAffine(AugmentationBase2D):
If (a, b, c, d), the scale is randomly sampled from the range a <= scale_x <= b, c <= scale_y <= d.
Will keep original scale by default.
shear (sequence or float, optional): Range of degrees to select from.
- If shear is a number, a shear parallel to the x axis in the range (-shear, +shear)
- will be apllied. Else if shear is a tuple or list of 2 values a shear parallel to the x axis in the
- range (shear[0], shear[1]) will be applied. Else if shear is a tuple or list of 2 tuples, then
- x-axis shear in (shear[0][0], shear[0][1]) and y-axis shear in (shear[1][0], shear[1][1]) will be applied.
- Will not apply shear by default
- resample (int, str or kornia.Resample): Default: Resample.BILINEAR
- padding_mode (int, str or kornia.SamplePadding): Default: SamplePadding.ZEROS
+ If float, a shear parallel to the x axis in the range (-shear, +shear) will be apllied.
+ If (a, b), a shear parallel to the x axis in the range (-shear, +shear) will be apllied.
+ If (a, b, c, d), then x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3])
+ will be applied. Will not apply shear by default.
+ resample (int, str or kornia.Resample): resample mode from "nearest" (0) or "bilinear" (1).
+ Default: Resample.BILINEAR.
+ padding_mode (int, str or kornia.SamplePadding): padding mode from "zeros" (0), "border" (1)
+ or "refection" (2). Default: SamplePadding.ZEROS.
return_transform (bool): if ``True`` return the matrix describing the transformation
applied to each. Default: False.
- same_on_batch (bool): apply the same transformation across the batch. Default: False
+ same_on_batch (bool): apply the same transformation across the batch. Default: False.
align_corners(bool): interpolation flag. Default: False.
Examples:
@@ -397,7 +398,7 @@ class RandomAffine(AugmentationBase2D):
if len(scale) == 2:
self.scale = _range_bound(scale, 'scale', bounds=(0, float('inf')), check='singular')
elif len(scale) == 4:
- self.scale = torch.stack([
+ self.scale = torch.cat([
_range_bound(scale[:2], 'scale_x', bounds=(0, float('inf')), check='singular'),
_range_bound(scale[2:], 'scale_y', bounds=(0, float('inf')), check='singular')
])
diff --git a/kornia/augmentation/random_generator/random_generator.py b/kornia/augmentation/random_generator/random_generator.py
index 0944b3ca1..6d7aa1535 100644
--- a/kornia/augmentation/random_generator/random_generator.py
+++ b/kornia/augmentation/random_generator/random_generator.py
@@ -170,7 +170,7 @@ def random_affine_generator(
# compute tensor ranges
if scale is not None:
_joint_range_check(cast(torch.Tensor, scale[:2]), "scale")
- scale = _adapted_uniform((batch_size,), scale[0], scale[1], same_on_batch).repeat(1, 2)
+ scale = _adapted_uniform((batch_size,), scale[0], scale[1], same_on_batch).unsqueeze(1).repeat(1, 2)
if len(scale) == 4:
_joint_range_check(cast(torch.Tensor, scale[2:]), "scale_y")
scale[:, 1] = _adapted_uniform((batch_size,), scale[2], scale[3], same_on_batch)
diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.py
index 4b8bbed7a..d1bb93417 100644
--- a/test/augmentation/test_perspective.py
+++ b/test/augmentation/test_perspective.py
@@ -149,9 +149,18 @@ class TestRandomAffine:
out = kornia.augmentation.RandomAffine(0.)(x_data)
assert out.shape == x_data.shape
- def test_batch_multi_params(self, device):
+ @pytest.mark.parametrize("degrees", [45., (-45., 45.), torch.tensor([45., 45.])])
+ @pytest.mark.parametrize("translate", [(0.1, 0.1), torch.tensor([0.1, 0.1])])
+ @pytest.mark.parametrize("scale", [
+ (0.8, 1.2), (0.8, 1.2, 0.9, 1.1), torch.tensor([0.8, 1.2]), torch.tensor([0.8, 1.2, 0.7, 1.3])])
+ @pytest.mark.parametrize("shear", [
+ 5., (-5., 5.), (-5., 5., -3., 3.), torch.tensor(5.),
+ torch.tensor([-5., 5.]), torch.tensor([-5., 5., -3., 3.])
+ ])
+ def test_batch_multi_params(self, degrees, translate, scale, shear, device, dtype):
x_data = torch.rand(2, 2, 8, 9).to(device)
- out = kornia.augmentation.RandomAffine((0., 0.))(x_data)
+ out = kornia.augmentation.RandomAffine(
+ degrees=degrees, translate=translate, scale=scale, shear=shear)(x_data)
assert out.shape == x_data.shape
def test_smoke_transform(self, device):