Closed adamxyang closed 5 years ago
Hey,
Yeah, you can either use that or any other way to loop through residual layers.
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.
How to solve a size mismatch error?
Traceback (most recent call last):
File "guided_gradcam.py", line 37, in
@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
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?
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' inforward_pass_on_convolutions
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?
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?Error: