Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'dict' object has no attribute 'to' #844

Closed
carlpe opened this issue Aug 3, 2020 · 15 comments
Closed

AttributeError: 'dict' object has no attribute 'to' #844

carlpe opened this issue Aug 3, 2020 · 15 comments
Labels
question Further information is requested

Comments

@carlpe
Copy link

carlpe commented Aug 3, 2020

Trying to run MednistGAN tutorial on some own nifti files.

Images (dict) are loaded and transformed as expected. When I attempt to train, I get an error:

AttributeError: 'dict' object has no attribute 'to'

image

@Nic-Ma
Copy link
Contributor

Nic-Ma commented Aug 3, 2020

Hi @carlpe ,

I think you should move the value of expected key in the dict to device, instead of moving dict itself.
Thanks.

@Nic-Ma Nic-Ma added the question Further information is requested label Aug 3, 2020
@carlpe
Copy link
Author

carlpe commented Aug 4, 2020

It seems to be working now!

However I have run into a new issue, this time it is to do with the size parameters in the code.

image

image

Thanks for suggestions and feedback on this.

@Nic-Ma
Copy link
Contributor

Nic-Ma commented Aug 4, 2020

Hi @ericspod , could you please help take a look this issue?
It's related to the GAN networks.

Thanks.

@ericspod
Copy link
Member

ericspod commented Aug 4, 2020

The discriminator's in_shape parameter states the size of input images, your images are different from the 64x64 images the example uses so you'll have to adjust that value. The start_shape parameter of the generator is the shape of the tensor after the input latent vector is forwarded through the linear layer, this is going to be starting shape of the image which is upsampled by the layers specified in channels and strides. Since strides is [2, 2, 2, 1] this means the starting image will be upsampled by 3 transpose convolutions with strides of 2, so it will be 8 times the size of start_shape. You want to choose a size for start_shape which is 1/8th the size of the final output image you want in the spatial dimensions.

@carlpe
Copy link
Author

carlpe commented Aug 4, 2020

Hi Eric, thanks for reply.

I am not sure I understand everything yet. But does it make sense that because the inputs are 3D images with 400,400,400, I would need more to get it to work?

I assume that Discriminator in-shape should be 1,400,400 and start_shape 1,50,50 for the Generator. Given that I keep strides [2, 2, 2, 1]. I did try to run this with 1,400,400 and 1,50,50 .. and I got a new error:

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [8, 1, 5, 5], but got 5-dimensional input of size [1, 1, 1, 64, 64] instead.

@ericspod
Copy link
Member

ericspod commented Aug 4, 2020

If you have 3D inputs the discriminator and generator need to be given 3D sizes. The in_shape value should then be (1,400,400,400) which is a volume 400 cubed with a single channel. The start_shape for the generator should then be (1,50,50,50) or maybe even with a channel size larger than 1. I would also choose a much larger latent_size than what was in the original notebook, for a larger image size you want a larger latent size so that it has enough capacity to represent variation.

@carlpe
Copy link
Author

carlpe commented Aug 4, 2020

Good. I will try it tomorrow.

Thanks 👍

@carlpe
Copy link
Author

carlpe commented Aug 5, 2020

I did manage to get around that error now. Using 1,400,400,400 and 1,50,50,50

However, it is too demanding for video memory. How would I proceed if I wanted to preprocess the images to smaller sizes before I ship them in?

RuntimeError: CUDA out of memory. Tried to allocate 74.51 GiB (GPU 0; 23.65 GiB total capacity; 2.67 MiB already allocated; 22.79 GiB free; 4.00 MiB reserved in total by PyTorch)

@carlpe
Copy link
Author

carlpe commented Aug 5, 2020

So, I resized all inputs to 64,64,64 - and now everything seems to be working. Although I wish there was some way to do this with the original size.,.

Thanks for help

@ericspod
Copy link
Member

ericspod commented Aug 5, 2020

Memory is often a challenge with 3D inputs, if you can't fit a batch of 1 into memory the alternative is to train on patches. One workflow is https://github.com/Project-MONAI/MONAI/blob/master/examples/workflows/unet_training_dict.py which uses RandCropByPosNegLabeld uring training and SlidingWindowInferer during evaluation.

@carlpe
Copy link
Author

carlpe commented Aug 5, 2020

Would that work with GAN also?

@ericspod
Copy link
Member

ericspod commented Aug 5, 2020

Depends on the exact GAN configuration you're using. If you train the discriminator on patches I could see it learning to tell real from synthetic effectively, but a generator would have a hard time learning how to create a coherent image patch-wise.

@carlpe
Copy link
Author

carlpe commented Aug 5, 2020

Yes, I see...

I will try to find a balanced size that fits into memory for now I think..

@carlpe carlpe closed this as completed Aug 5, 2020
@mudithayaran
Copy link

It seems to be working now!
How you cope up with this prob??
However I have run into a new issue, this time it is to do with the size parameters in the code.

image

image

Thanks for suggestions and feedback on this.

@ericspod
Copy link
Member

I think this is from from the in_shape value of the Discriminator which needs to be the size of the input images, so I think that's (1,400,400) for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants