question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: cannot assign parameters before Module.__init__() call

See original GitHub issue

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

(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

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!

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
shijianjiancommented, Apr 1, 2022

@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.

0reactions
tileb1commented, Apr 7, 2022

@shijianjian then, I get:

tensor([0., 0.])
tensor([0., 0.])

So now at least I have gradient, but they are 0 for some reason. If you check the loss I use

loss = nn.MSELoss()(out, images[:, :, :224, :224])

(so I’m enforcing the output of crop to be the upper left crop of size 224x224) I don’t see why the gradients are 0. In fact, if I change the loss to simpler things, I still get gradients of 0 e.g.

loss = nn.MSELoss()(out, torch.tensor(0.0))
Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: cannot assign module before Module.__init__ ...
I have a class as follows. class QuestionClassifier(nn.Module): def __init__(self, dictionary, embeddings_index, max_seq_length ...
Read more >
cannot assign module before Module.__init__() call
I am getting the following error. AttributeError: cannot assign module before Module.__init__() call I have a class as follows. class ...
Read more >
cannot assign module before Module.__init__() call even if ...
AttributeError : cannot assign module before Module.init() call. I'm trying to create an instance of my class : class ResNetGenerator(nn.
Read more >
cannot assign module before Module.__init__() call
[pytorch] AttributeError: cannot assign module before Module.__init__() call : 네이버 블로그
Read more >
pytorch报错cannot assign module before Module.__init__() call
AttributeError : cannot assign module before Module.__init__() call 出现这个错误的原因非常简单,调用了自定义的类,但是在自定义的类的__init__函数 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found