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.

Model does not have 'feature' attribute

See original GitHub issue

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'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mburaksayicicommented, Aug 20, 2019

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.

1reaction
CescMessicommented, Jul 2, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

'GoogLeNet' object has no attribute 'features' - Stack Overflow
I've checked the source code of GoogleNet provided by torchvision.models. It doesn't have an attribute called features .
Read more >
Problem with Select Layer by Attribute in ModelBuilder
I have an expression like: "field" LIKE '% value%' which does not work, there is no selected features in map window and table....
Read more >
Enter attributes for new features—ArcGIS Pro | Documentation
In the template attribute table, right-click the field, click Get Unique Values, and click a value. The value is added to the field....
Read more >
QGIS modeler: Error when extracting by non existing attributes ...
The error occurs because the Extract by location requires atleast one feature for it to run. If the output of the Extract by...
Read more >
Working with the Attribute Table - QGIS Documentation
Have a closer look at the edit widget in section Fields Properties to find ... Filtering records out of the attribute table does...
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