utkuozbulak / pytorch-cnn-visualizations

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

Model does not have 'feature' attribute #50

Closed adamxyang closed 5 years ago

adamxyang commented 5 years ago

Hi thanks for the code, I'm trying to visualise a trained ResNet18 model with the GradCam function. But ResNet models do not have the feature attribute as in VGGs. Should we use model.named_children() instead?

from gradcam import GradCam
grad_cam = GradCam(model, target_layer=7)

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-0a85f7f4c865> in <module>
      8 grad_cam = GradCam(model, target_layer=0)
      9 # Generate cam mask
---> 10 cam = grad_cam.generate_cam(x, target_class=None)
     11 # Save mask
     12 save_class_activation_images(original_image, cam, file_name_to_export)

/mnt/sdh/adam/visualisation/gradcam.py in generate_cam(self, input_image, target_class)
     56         # conv_output is the output of convolutions at specified layer
     57         # model_output is the final output of the model (1, 1000)
---> 58         conv_output, model_output = self.extractor.forward_pass(input_image)
     59         if target_class is None:
     60             target_class = np.argmax(model_output.data.numpy())

/mnt/sdh/adam/visualisation/gradcam.py in forward_pass(self, x)
     35         """
     36         # Forward pass on the convolutions
---> 37         conv_output, x = self.forward_pass_on_convolutions(x)
     38         x = x.view(x.size(0), -1)  # Flatten
     39         # Forward pass on the classifier

/mnt/sdh/adam/visualisation/gradcam.py in forward_pass_on_convolutions(self, x)
     23         """
     24         conv_output = None
---> 25         for module_pos, module in self.model.features._modules.items():
     26             x = module(x)  # Forward
     27             if int(module_pos) == self.target_layer:

/mnt/sdh/adam/adam_env/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    589                 return modules[name]
    590         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 591             type(self).__name__, name))
    592 
    593     def __setattr__(self, name, value):

AttributeError: 'ResNet' object has no attribute 'features'
utkuozbulak commented 5 years ago

Hey,

Yeah, you can either use that or any other way to loop through residual layers.

mburaksayici commented 5 years ago

Hey, For Resnet, what I do is:

`
def forward_pass_on_convolutions(self, x):

    conv_output = None
    i = 0
    for module_pos, module in self.model._modules.items():

        if module_pos == "avgpool":
            x.register_hook(self.save_gradient)
            conv_output = x  # Save the convolution output on that layer
            x = module(x)

        else:
            x = module(x)  # Forward
    return conv_output, x

` Also:

`

 def forward_pass(self, x):
         # Forward pass on the convolutions
         conv_output, x = self.forward_pass_on_convolutions(x)
         x = x.view(x.size(0), -1)  # Flatten
         # Forward pass on the classifier
         x = self.model.fc(x)
         return conv_output, x

`

Also deleting alexnet. Very good library btw. Extremely good. I'm thinking that people hadn't applied GRADCAM to their work because original is implemented in Lua. However, your work now will lead to better works. Hate to see "novel" architectures on CNN that every work outperforms Imagenet ranks by "a large margin" but you can't reproduce it or especially "interpret". Why original work is not implemented to Python also, you've seen a very good gap.

Yerichen commented 5 years ago

How to solve a size mismatch error?

Traceback (most recent call last): File "guided_gradcam.py", line 37, in cam = gcv2.generate_cam(prep_img, target_class) File "/home/yrc/visualizations/src/gradcam.py", line 68, in generate_cam conv_output, model_output = self.extractor.forward_pass(input_image) File "/home/yrc/visualizations/src/gradcam.py", line 47, in forward_pass conv_output, x = self.forward_pass_on_convolutions(x) File "/home/yrc/visualizations/src/gradcam.py", line 39, in forward_pass_on_convolutions x = module(x) # Forward File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 67, in forward return F.linear(input, self.weight, self.bias) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 1354, in linear output = input.matmul(weight.t()) RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1549635019666/work/aten/src/TH/generic/THTensorMath.cpp:940

xorb0ss commented 5 years ago

@Iron4dam also, I haven't tested this, but since new PyTorch releases, you might want to try replacing self.model.features._modules.items with self.model._modules.items

varshakishore commented 4 years ago

How to solve a size mismatch error?

Traceback (most recent call last): File "guided_gradcam.py", line 37, in cam = gcv2.generate_cam(prep_img, target_class) File "/home/yrc/visualizations/src/gradcam.py", line 68, in generate_cam conv_output, model_output = self.extractor.forward_pass(input_image) File "/home/yrc/visualizations/src/gradcam.py", line 47, in forward_pass conv_output, x = self.forward_pass_on_convolutions(x) File "/home/yrc/visualizations/src/gradcam.py", line 39, in forward_pass_on_convolutions x = module(x) # Forward File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 67, in forward return F.linear(input, self.weight, self.bias) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 1354, in linear output = input.matmul(weight.t()) RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1549635019666/work/aten/src/TH/generic/THTensorMath.cpp:940

Was this issue solved?

CescMessi commented 4 years ago

How to solve a size mismatch error? Traceback (most recent call last): File "guided_gradcam.py", line 37, in cam = gcv2.generate_cam(prep_img, target_class) File "/home/yrc/visualizations/src/gradcam.py", line 68, in generate_cam conv_output, model_output = self.extractor.forward_pass(input_image) File "/home/yrc/visualizations/src/gradcam.py", line 47, in forward_pass conv_output, x = self.forward_pass_on_convolutions(x) File "/home/yrc/visualizations/src/gradcam.py", line 39, in forward_pass_on_convolutions x = module(x) # Forward File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 67, in forward return F.linear(input, self.weight, self.bias) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 1354, in linear output = input.matmul(weight.t()) RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1549635019666/work/aten/src/TH/generic/THTensorMath.cpp:940

Was this issue solved?

def forward_pass_on_convolutions(self, x):
"""
Does a forward pass on convolutions, hooks the function at given layer
"""
conv_output = None
for module_pos, module in self.model._modules.items():
x = module(x)  # Forward
if module_pos == 'avgpool':
x.register_hook(self.save_gradient)
conv_output = x  # Save the convolution output on that layer
return conv_output, x

'fc' is after 'avgpool' , but it should be in forward_pass, so return here can skip 'fc' in forward_pass_on_convolutions

rashhkk commented 2 years ago

How to solve a size mismatch error?

Traceback (most recent call last): File "guided_gradcam.py", line 37, in cam = gcv2.generate_cam(prep_img, target_class) File "/home/yrc/visualizations/src/gradcam.py", line 68, in generate_cam conv_output, model_output = self.extractor.forward_pass(input_image) File "/home/yrc/visualizations/src/gradcam.py", line 47, in forward_pass conv_output, x = self.forward_pass_on_convolutions(x) File "/home/yrc/visualizations/src/gradcam.py", line 39, in forward_pass_on_convolutions x = module(x) # Forward File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in call result = self.forward(*input, **kwargs) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 67, in forward return F.linear(input, self.weight, self.bias) File "/home/yrc/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 1354, in linear output = input.matmul(weight.t()) RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1549635019666/work/aten/src/TH/generic/THTensorMath.cpp:940

How to solve this kind of issue?