Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Bugfix for larger MotionBlur kernel size ranges (#1543)

Rahul Somani5328bf40fdc1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d44fe3731..5a16a2d1b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- fix catch type for torch.svd error (#1431)
- Fix for nested AugmentationSequential containers (#1467)
- Use common bbox format xywh (#1472)
+- Fix motion blur kernel size bug for larger random generator ranges (#1540)
### :exclamation: Changes
- Add padding_mode for RandomElasticTransform augmentation (#1439)
diff --git a/kornia/augmentation/_2d/intensity/motion_blur.py b/kornia/augmentation/_2d/intensity/motion_blur.py
index b8bd77ed4..b63654f3b 100644
--- a/kornia/augmentation/_2d/intensity/motion_blur.py
+++ b/kornia/augmentation/_2d/intensity/motion_blur.py
@@ -1,4 +1,4 @@
-from typing import Dict, Optional, Tuple, Union, cast
+from typing import Dict, List, Optional, Tuple, Union, cast
import torch
@@ -83,14 +83,14 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
def apply_transform(
self, input: torch.Tensor, params: Dict[str, torch.Tensor], transform: Optional[torch.Tensor] = None
) -> torch.Tensor:
- kernel_size: int = cast(int, params["ksize_factor"].unique().item())
- angle = params["angle_factor"]
- direction = params["direction_factor"]
+ # sample a kernel size
+ kernel_size_list: List[int] = params["ksize_factor"].tolist()
+ idx: int = cast(int, torch.randint(len(kernel_size_list), (1,)).item())
return motion_blur(
input,
- kernel_size,
- angle,
- direction,
+ kernel_size=kernel_size_list[idx],
+ angle=params["angle_factor"],
+ direction=params["direction_factor"],
border_type=self.flags["border_type"].name.lower(),
mode=self.flags["resample"].name.lower(),
)
diff --git a/test/augmentation/test_motionblur.py b/test/augmentation/test_motionblur.py
index e4de24a86..c38f197cc 100644
--- a/test/augmentation/test_motionblur.py
+++ b/test/augmentation/test_motionblur.py
@@ -21,12 +21,13 @@ class TestRandomMotionBlur:
)
assert str(f) == repr
+ @pytest.mark.parametrize("kernel_size", [(3, 5), (7, 21)])
@pytest.mark.parametrize("same_on_batch", [True, False])
@pytest.mark.parametrize("return_transform", [True, False])
@pytest.mark.parametrize("p", [0.0, 1.0])
- def test_random_motion_blur(self, same_on_batch, return_transform, p, device, dtype):
+ def test_random_motion_blur(self, kernel_size, same_on_batch, return_transform, p, device, dtype):
f = RandomMotionBlur(
- kernel_size=(3, 5),
+ kernel_size=kernel_size,
angle=(10, 30),
direction=0.5,
same_on_batch=same_on_batch,