Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): fix ranged RandomMotionBlur3D kernel size (#4662)

NiteeshGowda72f61ce5cab71c
diff --git a/changelog.d/4662.fixed.md b/changelog.d/4662.fixed.md
new file mode 100644
index 000000000..cb2879125
--- /dev/null
+++ b/changelog.d/4662.fixed.md
@@ -0,0 +1 @@
+Fix `RandomMotionBlur3D` ranged kernel sizes for batched inputs.
diff --git a/kornia/augmentation/random_generator/_3d/motion_blur.py b/kornia/augmentation/random_generator/_3d/motion_blur.py
index 3a5930876..51f50b7ad 100644
--- a/kornia/augmentation/random_generator/_3d/motion_blur.py
+++ b/kornia/augmentation/random_generator/_3d/motion_blur.py
@@ -30,7 +30,7 @@ class MotionBlurGenerator3D(RandomGeneratorBase):
Args:
kernel_size: motion kernel size (odd and positive).
If int, the kernel will have a fixed size.
- If Tuple[int, int], it will randomly generate the value from the range batch-wisely.
+ If Tuple[int, int], it will randomly generate one value from the range for the whole batch.
angle: angle of the motion blur in degrees (anti-clockwise rotation).
If float, it will generate the value from (-angle, angle).
direction: forward/backward direction of the motion blur.
@@ -87,7 +87,7 @@ class MotionBlurGenerator3D(RandomGeneratorBase):
if len(self.kernel_size) != 2:
raise AssertionError(f"`kernel_size` must be (2,) if it is a tuple. Got {self.kernel_size}.")
self.ksize_sampler = UniformDistribution(
- self.kernel_size[0] // 2, self.kernel_size[1] // 2, validate_args=False
+ self.kernel_size[0] // 2, self.kernel_size[1] // 2 + 1, validate_args=False
)
else:
raise TypeError(f"Unsupported type: {type(self.kernel_size)}")
@@ -108,7 +108,13 @@ class MotionBlurGenerator3D(RandomGeneratorBase):
angle_factor = torch.stack([yaw_factor, pitch_factor, roll_factor], 1)
direction_factor = _adapted_rsampling((batch_size,), self.direction_sampler, same_on_batch)
- ksize_factor = _adapted_rsampling((batch_size,), self.ksize_sampler, same_on_batch).int() * 2 + 1
+
+ if isinstance(self.kernel_size, tuple):
+ # A ranged kernel size is sampled once per call and shared across the batch.
+ ksize_factor = _adapted_rsampling((1,), self.ksize_sampler, same_on_batch).int() * 2 + 1
+ ksize_factor = ksize_factor.expand(batch_size)
+ else:
+ ksize_factor = _adapted_rsampling((batch_size,), self.ksize_sampler, same_on_batch).int() * 2 + 1
return {
"ksize_factor": ksize_factor.to(device=_device, dtype=torch.int32),
diff --git a/tests/augmentation/test_motionblur.py b/tests/augmentation/test_motionblur.py
index 0cc17e6e6..8e171ae6b 100644
--- a/tests/augmentation/test_motionblur.py
+++ b/tests/augmentation/test_motionblur.py
@@ -149,6 +149,15 @@ class TestRandomMotionBlur3D(BaseTester):
self.assert_close(output, expected, rtol=1e-4, atol=1e-4)
+ def test_ranged_kernel_size_does_not_crash_for_batch(self):
+ input = torch.rand(6, 1, 4, 5, 6)
+
+ for seed in range(20):
+ torch.manual_seed(seed)
+ output = RandomMotionBlur3D((3, 7), 35.0, 0.5, p=1.0)(input)
+
+ assert output.shape == input.shape
+
@pytest.mark.slow
def test_gradcheck(self, device):
torch.manual_seed(0) # for random reproductibility
diff --git a/tests/augmentation/test_random_generator_3d.py b/tests/augmentation/test_random_generator_3d.py
index 589b0f585..20d05ffbc 100644
--- a/tests/augmentation/test_random_generator_3d.py
+++ b/tests/augmentation/test_random_generator_3d.py
@@ -691,3 +691,51 @@ class TestRandomMotionBlur3D(RandomGeneratorBaseTests):
assert_close(res["ksize_factor"], expected["ksize_factor"], rtol=1e-4, atol=1e-4)
assert_close(res["angle_factor"], expected["angle_factor"], rtol=1e-4, atol=1e-4)
assert_close(res["direction_factor"], expected["direction_factor"], rtol=1e-4, atol=1e-4)
+
+ @pytest.mark.device_agnostic
+ def test_ranged_kernel_size_is_constant_across_batch(self, device, dtype):
+ param_gen = MotionBlurGenerator3D(
+ kernel_size=(3, 7),
+ angle=torch.tensor([(0.0, 0.0)] * 3, device=device, dtype=dtype),
+ direction=torch.tensor([0.0, 0.0], device=device, dtype=dtype),
+ )
+
+ for seed in range(20):
+ torch.manual_seed(seed)
+ res = param_gen(batch_shape=torch.Size((6,)), same_on_batch=False)
+
+ assert res["ksize_factor"].shape == (6,)
+ assert res["ksize_factor"].unique().numel() == 1
+ assert int(res["ksize_factor"][0]) in {3, 5, 7}
+
+ @pytest.mark.device_agnostic
+ def test_ranged_kernel_size_includes_upper_bound(self, device, dtype):
+ param_gen = MotionBlurGenerator3D(
+ kernel_size=(3, 7),
+ angle=torch.tensor([(0.0, 0.0)] * 3, device=device, dtype=dtype),
+ direction=torch.tensor([0.0, 0.0], device=device, dtype=dtype),
+ )
+
+ seen = set()
+ for seed in range(1000):
+ torch.manual_seed(seed)
+ res = param_gen(batch_shape=torch.Size((1,)), same_on_batch=False)
+ seen.add(int(res["ksize_factor"][0]))
+
+ assert seen == {3, 5, 7}
+
+ @pytest.mark.device_agnostic
+ def test_ranged_kernel_size_even_upper_bound_rounds_up(self, device, dtype):
+ param_gen = MotionBlurGenerator3D(
+ kernel_size=(3, 20),
+ angle=torch.tensor([(0.0, 0.0)] * 3, device=device, dtype=dtype),
+ direction=torch.tensor([0.0, 0.0], device=device, dtype=dtype),
+ )
+
+ seen = set()
+ for seed in range(1000):
+ torch.manual_seed(seed)
+ res = param_gen(batch_shape=torch.Size((1,)), same_on_batch=False)
+ seen.add(int(res["ksize_factor"][0]))
+
+ assert seen == {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}