utkuozbulak / pytorch-cnn-visualizations

Pytorch implementation of convolutional neural network visualization techniques
MIT License
7.81k stars 1.49k forks source link

None type gradients with ResNet #57

Closed adamxyang closed 4 years ago

adamxyang commented 4 years ago

Hi, I have modified the code to be compatible with ResNets (changing model.features._modules to model.modules), but I got this error when using the GuidedBackprop class:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-abab40b06bf1> in <module>
----> 1 guided_grads = GBP.generate_gradients(x, y)
      2 guided_grads.shape

/mnt/sdh/adam/test/guided_backprop.py in generate_gradients(self, input_image, target_class)
    102         # Convert Pytorch variable to numpy array
    103         # [0] to get rid of the first channel (1,3,224,224)
--> 104         gradients_as_arr = self.gradients.data.numpy()[0]
    105         return gradients_as_arr

AttributeError: 'NoneType' object has no attribute 'data'

My model is ResNet34 with an extra channel in kernels in the conv1 layer, it's defined as:

model = models.resnet34(pretrained=True)

kernel = model.conv1.weight
new_conv = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
with torch.no_grad():
    new_conv.weight[:,:] = torch.stack([torch.mean(kernel, 1)]*4, dim=1)
model.conv1 = new_conv

model.fc = torch.nn.Linear(512, 2)
utkuozbulak commented 4 years ago

Hello,

You need to modify the code in such a way that it gets the gradients from the correct layer(s). The problem with ResNets is that they have nested layers (residual) and the code assumes the first layer is the target one when in fact this is not true. Try to understand how hook functions and how selecting the target layer works.

adamxyang commented 4 years ago

Thanks for the reply! I solved the issue with inputs.requires_grad=True.