Model does not have 'feature' attribute
See original GitHub issueHi 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:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey, For Resnet, what I do is:
`
def forward_pass_on_convolutions(self, x):
` Also:
`
`
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.
‘fc’ is after ‘avgpool’ , but it should be in
forward_pass
, so return here can skip ‘fc’ inforward_pass_on_convolutions