Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Motion blur (#2075)

nitaifingerhut2e304d9dd23e
diff --git a/kornia/augmentation/_2d/intensity/motion_blur.py b/kornia/augmentation/_2d/intensity/motion_blur.py
index e5ade1fda..0fd12f85a 100644
--- a/kornia/augmentation/_2d/intensity/motion_blur.py
+++ b/kornia/augmentation/_2d/intensity/motion_blur.py
@@ -1,11 +1,11 @@
from typing import Any, Dict, List, Optional, Tuple, Union, cast
import torch
-from torch import Tensor
from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D
from kornia.constants import BorderType, Resample
+from kornia.core import Tensor, tensor
from kornia.filters import motion_blur
@@ -83,7 +83,7 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, Tensor]:
params = super().generate_parameters(batch_shape)
- params["idx"] = torch.randint(batch_shape[0], (1,)).item()
+ params["idx"] = tensor([0]) if batch_shape[0] == 0 else torch.randint(batch_shape[0], (1,))
return params
def apply_transform(
@@ -91,7 +91,13 @@ class RandomMotionBlur(IntensityAugmentationBase2D):
) -> Tensor:
# sample a kernel size
kernel_size_list: List[int] = params["ksize_factor"].tolist()
- idx: int = cast(int, params["idx"])
+
+ # 1. We have to apply the same kernel size to all samples in the batch, thus we take the previously
+ # selected random index --- `params["idx"][0]` --- to determine the applied kernel size.
+ # 2. The `VideoSequential` flattens the first two dimensions, effectively creating a larger batch.
+ # Its method `VideoSequential.__repeat_param_across_channels__` repeats the previously selected index,
+ # creating a tensor with equal values. Hence, taking the first one (`params["idx"][0]`) is legit.
+ idx: int = cast(int, params["idx"][0])
return motion_blur(
input,
kernel_size=kernel_size_list[idx],
diff --git a/test/augmentation/test_motionblur.py b/test/augmentation/test_motionblur.py
index a7468cf9e..364993a7d 100644
--- a/test/augmentation/test_motionblur.py
+++ b/test/augmentation/test_motionblur.py
@@ -70,6 +70,7 @@ class TestRandomMotionBlur:
'angle_factor': torch.tensor([30.0]),
'direction_factor': torch.tensor([-0.5]),
'border_type': torch.tensor([0]),
+ 'idx': torch.tensor([0]),
}
assert gradcheck(
RandomMotionBlur(kernel_size=3, angle=(10, 30), direction=(-0.5, 0.5), p=1.0),
@@ -138,6 +139,7 @@ class TestRandomMotionBlur3D:
'angle_factor': torch.tensor([[30.0, 30.0, 30.0]]),
'direction_factor': torch.tensor([-0.5]),
'border_type': torch.tensor([0]),
+ 'idx': torch.tensor([0]),
}
assert gradcheck(
RandomMotionBlur3D(kernel_size=3, angle=(10, 30), direction=(-0.5, 0.5), p=1.0),