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

Gradient Bug in backprop.visualize #16

Closed
goldblum opened this issue Oct 16, 2019 · 7 comments
Closed

Gradient Bug in backprop.visualize #16

goldblum opened this issue Oct 16, 2019 · 7 comments
Labels
bug Something isn't working

Comments

@goldblum
Copy link

I tried generating a saliency map using the following code:

backprop = Backprop(net)
target_class = 4
backprop.visualize(image, target_class, guided=True)

My net is a standard PyTorch neural network, and the image is a (1,3,84,84) image. However, I get the following error:

File "visualizations.py", line 89, in test
backprop.visualize(image, target_class, guided=True)
File "/home/user/.local/lib/python3.6/site-packages/flashtorch/saliency/backprop.py", line 168, in visualize
guided=guided)
File "/home/user/.local/lib/python3.6/site-packages/flashtorch/saliency/backprop.py", line 120, in calculate_gradients
output.backward(gradient=target)
File "/home/user/.local/lib/python3.6/site-packages/torch/tensor.py", line 150, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/user/.local/lib/python3.6/site-packages/torch/autograd/init.py", line 99, in backward
allow_unreachable=True) # allow_unreachable flag
File "/home/user/.local/lib/python3.6/site-packages/flashtorch/saliency/backprop.py", line 214, in _record_gradients
if self.gradients.shape == grad_in[0].shape:
AttributeError: 'NoneType' object has no attribute 'shape'

Do I need to turn the image into a parameter with requires_grad = True?
Is this usually done by the load_image function? I found that this function was re-sizing my image to 224x224. Thanks for your help!

@goldblum goldblum added the bug Something isn't working label Oct 16, 2019
@goldblum goldblum changed the title [BUG] Gradient Bug in backprop.visualize Oct 16, 2019
@sumanyugupta
Copy link

Facing the same error. I tried using ResNet50 instead of AlexNet and got the same issue as you. However, using AlexNet works just fine.

@goldblum
Copy link
Author

I have avoided this error by turning on the requires_grad=True, but the packages still does not work fully for me, so I am not sure if this solved the problem.

@MisaOgura
Copy link
Owner

Hi @goldblum and @sumanyugupta ,

Apologies for the late reply.

I'll be happy to assist if you are still having problems using FlashTorch.

Many thanks,
Misa

@Lecanyu
Copy link

Lecanyu commented Nov 20, 2019

@MisaOgura
Hi, I face the same issue. The shape of my input image is (1,2,3,84,84), and I have some .view and concatenation operations in my model inference.

Can this library handle the special input case?

BTW,
The second dimension indicates the input is a pair of images.
The first dimension indicates that these two images belong to a category.

@MisaOgura
Copy link
Owner

Hi @Lecanyu,

Thanks for reporting the issue!

I will try to look into this in the coming weeks.

Many thanks,
Misa

@ptempczyk
Copy link

@MisaOgura
Hi. I have the same problem. Other network than alexnet doesn't work even on the owl example (e.g mobilenet_v2). Retain grad helps, but saliency maps doesn't make any sense.

@MisaOgura
Copy link
Owner

Apologies folks for the lateness of this reply - I hope everyone is well and staying safe.

@goldblum The error message indicates that the gradients that's flowing into the first 2D convolutional layer is None. I'm closing this for now, but If you are still experiencing problems, please reopen this issue with the architecture of your network.

@Lecanyu I haven't got around to add support for custom input shapes, so at the moment the Backprop class only supports for input images with a shape (N, C, H, W) as detailed here. I will try keep this feature in mind though - if you can create a feature request using this template that will help me keep track.

@sumanyugupta @ptempczyk I can confirm that the example notebook works with pre-trained ResNet50 & MobileNetv2 loaded from torchvision.models (see below).

ResNet50:
Screen Shot 2020-05-11 at 09 45 07

MobileNetv2:
Screen Shot 2020-05-11 at 09 45 28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants