Repository: kornia/kornia

Issue:
Fix a Bug in `kornia.augmentation.RandomRain`
### Describe the bug


When applying `[kornia.augmentation.RandomRain](https://kornia.readthedocs.io/en/latest/augmentation.module.html#kornia.augmentation.RandomRain)` over an image, it seems that subsequent distortions are all affected by the rain effect. For example, if you apply `RandomRotation` before and after calling rain, the rotation before `RandomRain` is fine. However, the rotation after calling `RandomRain` is influenced by the rain. same thing happens for all of the other distortion you may call after `RandomRotation` function. I tried several other [augmentation ](https://kornia.readthedocs.io/en/latest/augmentation.module.html#kornia.augmentation)here and this is the case for all of them. 



![image](https://github.com/kornia/kornia/assets/107806531/b741865f-9737-44b9-b55e-45c1d912494c)







### Reproduction steps

```bash
The following link is a minimal code as an example (on google colab).
```
[sample code](https://colab.research.google.com/drive/1m_Pjqa6-MgO2ybZeNnLYdDerrErYpKw_#scrollTo=dwr_iTFt28t5)

![image](https://github.com/kornia/kornia/assets/107806531/c48b6903-820b-4fef-bdab-849eab21a6c5)

### Expected behavior

When applying the `kornia.augmentation.RandomRain` augmentation to an image, it should only affect the rain effect on that particular image and not introduce any unintended global effects on subsequent transformations. For example, if rotation is applied before and after calling `RandomRain`, the rotation after the rain effect should not be influenced by the rain.


### Environment

```shell
- Running on colab
- OS  Linux
- Python version: 2.0.1+cu118
- GPU models and configuration:T4
```


### Additional context

_No response_


Discussion:
I see. There should be an ``image = image.clone()`` in the forehead.
@f-amerehi are you willing to open a PR with the fix?
Hi @edgarriba  hope that all is well. I guess the issue might be linked to the way rain droplets are being directly applied to the image tensor. I'll give it a try and if the issue is resolved, will create a pull request. Thanks.
Hi @edgarriba , I've made a small change [here ](https://github.com/kornia/kornia/compare/master...f-amerehi:kornia:master) and it [does solve that global effect issue](https://colab.research.google.com/drive/1oWZL7nhG0wPNnCPyObSrLbmBljQ_28fk#scrollTo=nARAkpPWY2jF). But one thing that happens as a result is that, the rain droplets are now very tiny no matter how big choosing the `drop_height`  or `drop_width`!  Could this stem from the [RainGenerator](https://github.com/f-amerehi/kornia/blob/master/kornia/augmentation/random_generator/_2d/random_rain.py)?


that's weird. Any ideas @shijianjian ?
@f-amerehi just revert other changes and place the image clone in line 66.

Resolving patch:
diff --git a/kornia/augmentation/_2d/intensity/random_rain.py b/kornia/augmentation/_2d/intensity/random_rain.py
--- a/kornia/augmentation/_2d/intensity/random_rain.py
+++ b/kornia/augmentation/_2d/intensity/random_rain.py
@@ -63,7 +63,7 @@ def apply_transform(
             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()
         for i in range(image.shape[0]):
             number_of_drops: int = int(params['number_of_drops_factor'][i])
             # We generate tensor with maximum number of drops, and then remove unnecessary drops.
@@ -87,5 +87,5 @@ def apply_transform(
             y = torch.linspace(start=0, end=width_of_drop, steps=size_of_line, dtype=torch.long).to(image.device)
             # Draw lines
             for k in range(x.shape[0]):
-                image[i, :, coords[0] + x[k], coords[1] + y[k]] = 200 / 255
-        return image
+                modeified_img[i, :, coords[0] + x[k], coords[1] + y[k]] = 200 / 255
+        return modeified_img


Test patch: