Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): enforce strict RandomRain drop bounds (#4451)

Puttala Jeevan Kumaracb68e3ed380
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6cd9e116e..f1fb42418 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -379,6 +379,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `RandomRain` now rejects drop heights equal to the image height and absolute drop widths equal to the image
+ width with the documented validation error, instead of allowing boundary-sized drops to reach an internal
+ `IndexError`. (#4451)
* `depth_to_normals` now raises `ShapeError` when `H < 2` or `W < 2`, since surface normals need two
tangent directions. Previously, singleton axes could yield zero or non-finite normals, and empty
spatial dimensions failed inside padding. Inputs with both dimensions at least 2 are unchanged. (#4458)
diff --git a/kornia/augmentation/_2d/intensity/random_rain.py b/kornia/augmentation/_2d/intensity/random_rain.py
index 2223d3571..d5c184fb8 100644
--- a/kornia/augmentation/_2d/intensity/random_rain.py
+++ b/kornia/augmentation/_2d/intensity/random_rain.py
@@ -32,8 +32,10 @@ class RandomRain(IntensityAugmentationBase2D):
Args:
p: probability of applying the transformation.
number_of_drops: number of drops per image
- drop_height: height of the drop in image(same for each drops in one image)
- drop_width: width of the drop in image(same for each drops in one image)
+ drop_height: Height of the drop in the image (same for each drop in one image). Sampled values must be
+ greater than zero and strictly smaller than the input image height.
+ drop_width: Width of the drop in the image (same for each drop in one image). The absolute sampled value
+ must be strictly smaller than the input image width.
Shape:
- Input: :math:`(C, H, W)` or :math:`(B, C, H, W)`
- Output: :math:`(B, C, H, W)`
@@ -74,14 +76,13 @@ class RandomRain(IntensityAugmentationBase2D):
KORNIA_CHECK(image.shape[1] in {3, 1}, "Number of color channels should be 1 or 3.")
KORNIA_CHECK(
bool(
- torch.all(params["drop_height_factor"] <= image.shape[2])
- and torch.all(params["drop_height_factor"] > 0)
+ torch.all(params["drop_height_factor"] < image.shape[2]) and torch.all(params["drop_height_factor"] > 0)
),
"Height of drop should be greater than zero and less than image height.",
)
KORNIA_CHECK(
- bool(torch.all(torch.abs(params["drop_width_factor"]) <= image.shape[3])),
+ bool(torch.all(torch.abs(params["drop_width_factor"]) < image.shape[3])),
"Width of drop should be less than image width.",
)
modeified_img = image.clone()
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index c28bdb041..bc90e3881 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -5359,6 +5359,37 @@ class TestRandomRain(BaseTester):
assert err_msg in str(errinfo)
+ @pytest.mark.parametrize(
+ "drop_height,drop_width,error_message",
+ [
+ (
+ (5, 5),
+ (1, 1),
+ "Height of drop should be greater than zero and less than image height.",
+ ),
+ ((1, 1), (7, 7), "Width of drop should be less than image width."),
+ ((1, 1), (-7, -7), "Width of drop should be less than image width."),
+ ],
+ )
+ def test_drop_size_boundaries(self, drop_height, drop_width, error_message, device, dtype):
+ from kornia.core.exceptions import BaseError
+
+ input_data = torch.zeros(1, 3, 5, 7, device=device, dtype=dtype)
+ aug = RandomRain(p=1.0, drop_height=drop_height, drop_width=drop_width, number_of_drops=(1, 1))
+
+ with pytest.raises(BaseError) as errinfo:
+ aug(input_data)
+
+ assert str(errinfo.value) == error_message
+
+ def test_drop_size_immediately_inside_boundaries(self, device, dtype):
+ input_data = torch.zeros(1, 3, 5, 7, device=device, dtype=dtype)
+ aug = RandomRain(p=1.0, drop_height=(4, 4), drop_width=(6, 6), number_of_drops=(1, 1))
+
+ output_data = aug(input_data)
+
+ assert output_data.shape == input_data.shape
+
def test_zero_probability(self, device):
input_data = torch.rand(10, 3, 8, 8, device=device)
aug = RandomRain(p=0.0, drop_height=(2, 3), drop_width=(2, 3), number_of_drops=(1, 3))