Repository: kornia/kornia

Issue:
AttributeError: cannot assign parameters before Module.__init__() call
### Describe the bug

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-048b789dea33> in <module>
----> 1 crop = K.RandomResizedCrop((224, 224), scale=p([0.08, 1.0]))

~/anaconda3/envs/dino/lib/python3.8/site-packages/kornia/augmentation/_2d/geometric/resized_crop.py in __init__(self, size, scale, ratio, resample, same_on_batch, align_corners, p, keepdim, cropping_mode, return_transform)
     81             p=1.0, return_transform=return_transform, same_on_batch=same_on_batch, p_batch=p, keepdim=keepdim
     82         )
---> 83         self._param_generator = cast(rg.ResizedCropGenerator, rg.ResizedCropGenerator(size, scale, ratio))
     84         self.flags = dict(
     85             size=size, resample=Resample.get(resample), align_corners=align_corners, cropping_mode=cropping_mode

~/anaconda3/envs/dino/lib/python3.8/site-packages/kornia/augmentation/random_generator/base.py in __call__(cls, *args, **kwargs)
      9 
     10     def __call__(cls, *args, **kwargs):
---> 11         obj = type.__call__(cls, *args, **kwargs)
     12         obj.__post_init__()
     13         return obj

~/anaconda3/envs/dino/lib/python3.8/site-packages/kornia/augmentation/random_generator/_2d/crop.py in __init__(self, output_size, scale, ratio)
    160         ratio: Union[torch.Tensor, Tuple[float, float]],
    161     ) -> None:
--> 162         self.scale = scale
    163         self.ratio = ratio
    164         self.output_size = output_size

~/anaconda3/envs/dino/lib/python3.8/site-packages/torch/nn/modules/module.py in __setattr__(self, name, value)
    960         if isinstance(value, Parameter):
    961             if params is None:
--> 962                 raise AttributeError(
    963                     "cannot assign parameters before Module.__init__() call")
    964             remove_from(self.__dict__, self._buffers, self._modules, self._non_persistent_buffers_set)

AttributeError: cannot assign parameters before Module.__init__() call

### Reproduction steps

```bash
(this is code slightly adapted from the paper "Differentiable Data Augmentation with Kornia": https://arxiv.org/pdf/2011.09832.pdf)

import kornia.augmentation as K
import torch
import torch.nn as nn
t = lambda x: torch.tensor(x)
p = lambda x: nn.Parameter(t(x))
import torchvision
import matplotlib.pyplot as plt

from PIL import Image
img = Image.open('/path/to/image.jpg')
img = torchvision.transforms.functional.pil_to_tensor(img).float()
img = (img - img.min()) / (img.max() - img.min())
images = img.clone().detach().requires_grad_(True)

crop = K.RandomResizedCrop((224, 224), scale=p([0.08, 1.0]))
```


### Expected behavior

If scale is a static tensor, there is no problem

\# p([0.08, 1.0])) is changed to t([0.08, 1.0]))
crop = K.RandomResizedCrop((224, 224), scale=t([0.08, 1.0])) 

I would expect K.RandomResizedCrop to work when parameters are fed as nn.Parameters and not only with static tensors

### Environment

```shell
PyTorch version: 1.8.1
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 10.0.0-4ubuntu1 
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.8.2 (default, Mar 26 2020, 15:53:00)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.13.0-37-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: NVIDIA GeForce GTX 1650
Nvidia driver version: 470.103.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.20.2
[pip3] torch==1.8.1
[pip3] torchaudio==0.8.0a0+e4e171a
[pip3] torchvision==0.9.1
[pip3] torchviz==0.0.2
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.2.89              hfd86e86_1  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.2.0           h06a4308_296  
[conda] mkl-service               2.3.0            py38h27cfd23_1  
[conda] mkl_fft                   1.3.0            py38h42c9631_2  
[conda] mkl_random                1.2.1            py38ha9443f7_2  
[conda] numpy                     1.20.2           py38h2d18471_0  
[conda] numpy-base                1.20.2           py38hfae3a4d_0  
[conda] pytorch                   1.8.1           py3.8_cuda10.2_cudnn7.6.5_0    pytorch
[conda] torchaudio                0.8.1                      py38    pytorch
[conda] torchvision               0.9.1                py38_cu102    pytorch
[conda] torchviz                  0.0.2                    pypi_0    pypi
```


Thank you in advance!


Discussion:
@tileb1 Thanks for reporting. I will fix it this weekend.

The documented methods shall work with earlier version like 0.6.0, before the refactor of the new random generator. 

Resolving patch:
diff --git a/kornia/augmentation/random_generator/_2d/crop.py b/kornia/augmentation/random_generator/_2d/crop.py
--- a/kornia/augmentation/random_generator/_2d/crop.py
+++ b/kornia/augmentation/random_generator/_2d/crop.py
@@ -159,9 +159,6 @@ def __init__(
         scale: Union[torch.Tensor, Tuple[float, float]],
         ratio: Union[torch.Tensor, Tuple[float, float]],
     ) -> None:
-        self.scale = scale
-        self.ratio = ratio
-        self.output_size = output_size
         if not (
             len(output_size) == 2
             and isinstance(output_size[0], (int,))
@@ -170,7 +167,10 @@ def __init__(
             and output_size[1] > 0
         ):
             raise AssertionError(f"`output_size` must be a tuple of 2 positive integers. Got {output_size}.")
-        super().__init__(size=output_size, resize_to=self.output_size)  # fake an intermedia crop size
+        super().__init__(size=output_size, resize_to=output_size)  # fake an intermedia crop size
+        self.scale = scale
+        self.ratio = ratio
+        self.output_size = output_size
 
     def __repr__(self) -> str:
         repr = f"scale={self.scale}, resize_to={self.ratio}, output_size={self.output_size}"
diff --git a/kornia/augmentation/random_generator/_2d/planckian_jitter.py b/kornia/augmentation/random_generator/_2d/planckian_jitter.py
--- a/kornia/augmentation/random_generator/_2d/planckian_jitter.py
+++ b/kornia/augmentation/random_generator/_2d/planckian_jitter.py
@@ -8,7 +8,6 @@
 
 
 class PlanckianJitterGenerator(RandomGeneratorBase):
-
     r"""Generate random planckian jitter parameters for a batch of images
     """
 


Test patch: