Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

replace torch.uniform_ to Uniform in random_affine

edgarribad41f16548659
diff --git a/kornia/augmentation/param_gen.py b/kornia/augmentation/param_gen.py
index f2155c800..13e52e7c1 100644
--- a/kornia/augmentation/param_gen.py
+++ b/kornia/augmentation/param_gen.py
@@ -240,25 +240,25 @@ def _get_random_affine_params(
Returns:
torch.Tensor: params to be passed to the affine transformation.
"""
- angle = torch.empty(batch_size).uniform_(degrees[0], degrees[1])
+ angle = Uniform(degrees[0], degrees[1]).rsample((batch_size,))
# compute tensor ranges
if scales is not None:
- scale = torch.empty(batch_size).uniform_(scales[0], scales[1])
+ scale = Uniform(scales[0], scales[1]).rsample((batch_size,))
else:
scale = torch.ones(batch_size)
if shears is not None:
- shear = torch.empty(batch_size).uniform_(shears[0], shears[1])
+ shear = Uniform(shears[0], shears[1]).rsample((batch_size,))
else:
shear = torch.zeros(batch_size)
if translate is not None:
max_dx: float = translate[0] * width
max_dy: float = translate[1] * height
- translations = torch.cat([
- torch.empty(batch_size).uniform_(-max_dx, max_dx),
- torch.empty(batch_size).uniform_(-max_dy, max_dy),
+ translations = torch.stack([
+ Uniform(-max_dx, max_dx).rsample((batch_size,)),
+ Uniform(-max_dy, max_dy).rsample((batch_size,)),
], dim=-1)
else:
translations = torch.zeros(batch_size, 2)
diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.py
index 17e72c228..24c7cf27a 100644
--- a/test/augmentation/test_perspective.py
+++ b/test/augmentation/test_perspective.py
@@ -104,21 +104,21 @@ class TestRandomAffine:
def test_smoke_no_transform(self, device):
x_data = torch.rand(1, 2, 8, 9).to(device)
-
out = F.random_affine(x_data, 0.)
-
assert out.shape == x_data.shape
def test_smoke_no_transform_batch(self, device):
x_data = torch.rand(2, 2, 8, 9).to(device)
-
out = F.random_affine(x_data, 0.)
+ assert out.shape == x_data.shape
+ def test_batch_multi_params(self, device):
+ x_data = torch.rand(2, 2, 8, 9).to(device)
+ out = F.random_affine(x_data, 0., (0., 0.))
assert out.shape == x_data.shape
def test_smoke_transform(self, device):
x_data = torch.rand(1, 2, 4, 5).to(device)
-
out = F.random_affine(x_data, 0., return_transform=True)
assert isinstance(out, tuple)