Repository: kornia/kornia

Issue:
VideoSequential is applying the exact same augmentations from one batch to another
### Describe the bug

VideoSequential is applying the same augmentations from one batch to another. Nothing is randomized from one batch to another, defeating the point of augmentations.

For instance see the attached tensorboard visualization of the same index of the batch across batches:

![2023-06-14_14-01_2](https://github.com/kornia/kornia/assets/107197374/272da0e5-5bf6-4637-af45-30a34122b99d)

![2023-06-14_14-01_1](https://github.com/kornia/kornia/assets/107197374/a45fd148-bc38-49b9-8ad4-5d71235af513)

![2023-06-14_14-01](https://github.com/kornia/kornia/assets/107197374/8b435b05-0c0d-491f-9e6c-ba126ac03267)

This is because in [video.py](https://github.com/kornia/kornia/blob/e42a925ced6f54070130ef3f175192ea8525f451/kornia/augmentation/container/video.py) in forward() line 334 if the params have been already computed they are not recomputed. 

Instead, self.forward_parameters(input.shape) should be called each time forward() is called

### Reproduction steps

```bash
Run an pipeline with

 K.VideoSequential(
                K.RandomGaussianNoise(mean=0.0, std=0.1, p=0.1),
                data_format="BTCHW",
                same_on_frame=True,
            )

Vizualize the output across batches for batch element 0.

See that the noise is constant across batches.
```


### Expected behavior

Augmentation parameters should be random at each new batch

### Environment

```shell
PyTorch version: 1.12.1
Is debug build: False
CUDA used to build PyTorch: 11.3
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.27

Python version: 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-67-generic-x86_64-with-glibc2.27
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: 
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090
GPU 3: NVIDIA GeForce RTX 3090

Nvidia driver version: 495.29.05
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.2.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.2.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              32
On-line CPU(s) list: 0-31
Thread(s) per core:  2
Core(s) per socket:  16
Socket(s):           1
NUMA node(s):        1
Vendor ID:           AuthenticAMD
CPU family:          25
Model:               1
Model name:          AMD EPYC 7343 16-Core Processor
Stepping:            1
CPU MHz:             3852.493
CPU max MHz:         3940.6250
CPU min MHz:         1500.0000
BogoMIPS:            6388.32
Virtualization:      AMD-V
L1d cache:           32K
L1i cache:           32K
L2 cache:            512K
L3 cache:            32768K
NUMA node0 CPU(s):   0-31
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 invpcid_single hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] numpy==1.23.5
[pip3] pytorch-lightning==1.9.3
[pip3] pytorch3d==0.7.4
[pip3] torch==1.12.1
[pip3] torchmetrics==0.11.2
[pip3] torchvision==0.13.1
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.3.1               h2bc3f7f_2  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342  
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.6           py310h1128e8f_1  
[conda] mkl_random                1.2.2           py310h1128e8f_1  
[conda] numpy                     1.23.5                   pypi_0    pypi
[conda] pytorch                   1.12.1          py3.10_cuda11.3_cudnn8.3.2_0    pytorch
[conda] pytorch-lightning         1.9.3           py310h06a4308_0  
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] pytorch3d                 0.7.4                    pypi_0    pypi
[conda] torchmetrics              0.11.2          py310h2f386ee_0  
[conda] torchvision               0.13.1              py310_cu113    pytorch
```


### Additional context

_No response_


Discussion:
@shijianjian any intuition what could be wrong here ?
> @shijianjian any intuition what could be wrong here ?

It's because of this in [video.py](https://github.com/kornia/kornia/blob/e42a925ced6f54070130ef3f175192ea8525f451/kornia/augmentation/container/video.py):

`        if params is None:

            if self._params is None:

                self._params = self.forward_parameters(input.shape)

            params = self._params`

It should be


`        if params is None:

            self._params = self.forward_parameters(input.shape)

            params = self._params`

Otherwise params are not recomputed for each forward(), so you get the same augmentations across batches
> > @shijianjian any intuition what could be wrong here ?
> 
> It's because of this in [video.py](https://github.com/kornia/kornia/blob/e42a925ced6f54070130ef3f175192ea8525f451/kornia/augmentation/container/video.py):
> 
> ` if params is None:
> 
> ```
>         if self._params is None:
> 
>             self._params = self.forward_parameters(input.shape)
> 
>         params = self._params`
> ```
> 
> It should be
> 
> ` if params is None:
> 
> ```
>         self._params = self.forward_parameters(input.shape)
> 
>         params = self._params`
> ```
> 
> Otherwise params are not recomputed for each forward(), so you get the same augmentations across batches

Please make a PR if you already got a fix.
Ok, will do.
________________________________
From: Jian Shi ***@***.***>
Sent: Monday, June 19, 2023 2:07 PM
To: kornia/kornia ***@***.***>
Cc: Clement Marti ***@***.***>; Author ***@***.***>
Subject: Re: [kornia/kornia] VideoSequential is applying the exact same augmentations from one batch to another (Issue #2410)


@shijianjian<https://github.com/shijianjian> any intuition what could be wrong here ?

It's because of this in video.py<https://github.com/kornia/kornia/blob/e42a925ced6f54070130ef3f175192ea8525f451/kornia/augmentation/container/video.py>:

` if params is None:

        if self._params is None:

            self._params = self.forward_parameters(input.shape)

        params = self._params`


It should be

` if params is None:

        self._params = self.forward_parameters(input.shape)

        params = self._params`


Otherwise params are not recomputed for each forward(), so you get the same augmentations across batches

Please make a PR if you already got a fix.

—
Reply to this email directly, view it on GitHub<https://github.com/kornia/kornia/issues/2410#issuecomment-1597068463>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AZR3HPW6FUK6YFVHJL7YE2DXMA6ILANCNFSM6AAAAAAZGIXXXI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>


Resolving patch:
diff --git a/kornia/augmentation/container/video.py b/kornia/augmentation/container/video.py
--- a/kornia/augmentation/container/video.py
+++ b/kornia/augmentation/container/video.py
@@ -331,8 +331,7 @@ def forward(
             raise AssertionError(f"Input must be a 5-dim tensor. Got {input.shape}.")
 
         if params is None:
-            if self._params is None:
-                self._params = self.forward_parameters(input.shape)
+            self._params = self.forward_parameters(input.shape)
             params = self._params
 
         output = self.transform_inputs(input, params, extra_args=extra_args)


Test patch:
diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.py
--- a/test/augmentation/test_container.py
+++ b/test/augmentation/test_container.py
@@ -38,6 +38,13 @@ def reproducibility_test(input, seq):
 
 
 class TestVideoSequential:
+    def test_smoke(self, device, dtype):
+        input_1 = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
+        input_2 = torch.randn(4, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
+        aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1))
+        aug_list(input_1)
+        aug_list(input_2)
+
     @pytest.mark.parametrize('shape', [(3, 4), (2, 3, 4), (2, 3, 5, 6), (2, 3, 4, 5, 6, 7)])
     @pytest.mark.parametrize('data_format', ["BCTHW", "BTCHW"])
     def test_exception(self, shape, data_format, device, dtype):