Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): sample same number of drops in RandomRain when same_on_batch=True (#4448) (#4453)

topqaz0e74e6cf7c5c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f1fb42418..2111aa0ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -379,6 +379,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `RandomRain` with `same_on_batch=True` now samples the same number of rain drops for all samples in the batch. (#4453)
* `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)
diff --git a/kornia/augmentation/random_generator/_2d/random_rain.py b/kornia/augmentation/random_generator/_2d/random_rain.py
index 9d607766b..1c364d038 100644
--- a/kornia/augmentation/random_generator/_2d/random_rain.py
+++ b/kornia/augmentation/random_generator/_2d/random_rain.py
@@ -70,7 +70,7 @@ class RainGenerator(RandomGeneratorBase):
_common_param_check(batch_size, same_on_batch)
_device, _dtype = _extract_device_dtype([self.drop_width, self.drop_height, self.number_of_drops])
# self.ksize_factor.expand((batch_size, -1))
- number_of_drops_factor = _adapted_rsampling((batch_size,), self.number_of_drops_sampler).to(
+ number_of_drops_factor = _adapted_rsampling((batch_size,), self.number_of_drops_sampler, same_on_batch).to(
device=_device, dtype=torch.long
)
drop_height_factor = _adapted_rsampling((batch_size,), self.drop_height_sampler, same_on_batch).to(
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index bc90e3881..0c4b31f4c 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -5395,6 +5395,18 @@ class TestRandomRain(BaseTester):
aug = RandomRain(p=0.0, drop_height=(2, 3), drop_width=(2, 3), number_of_drops=(1, 3))
aug(input_data)
+ def test_same_on_batch(self, device, dtype):
+ aug = RandomRain(p=1.0, drop_height=(2, 3), drop_width=(2, 3), number_of_drops=(5, 10), same_on_batch=True)
+ input = torch.rand(1, 3, 10, 10, device=device, dtype=dtype).repeat(4, 1, 1, 1)
+ output = aug(input)
+ self.assert_close(output[0], output[1])
+ self.assert_close(output[1], output[2])
+ self.assert_close(output[2], output[3])
+ assert (aug._params["number_of_drops_factor"] == aug._params["number_of_drops_factor"][0]).all()
+ assert (aug._params["drop_height_factor"] == aug._params["drop_height_factor"][0]).all()
+ assert (aug._params["drop_width_factor"] == aug._params["drop_width_factor"][0]).all()
+ self.assert_close(aug._params["coordinates_factor"][0], aug._params["coordinates_factor"][1])
+
class TestMultiprocessing:
torch.manual_seed(0) # for random reproductibility