Closed asfandyarazhar13 closed 2 years ago
Hi, I'm looking into this.
Can you please confirm that there is an 1 in the shape, The shape is : torch.Size([64, 1, 128, 128]) Not torch.Size([64, 128, 128])
Right?
Thanks for the response! Just double checked and the shape actually torch.Size([16, 1, 128, 128])
. However, still 4 dims.
I need to reproduce this, this might take me another 2-3 days to look at it. I will update.
Appreciate it. Looking forward to your response.
Note when I try to reshape the image to torch.Size([16, 128, 128])
, the following error occurs:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-43-6d82ff2c42c8> in <module>()
13 print(img.shape)
14
---> 15 grayscale_cam = cam(input_tensor=img)
16
17 grayscale_cam = grayscale_cam[0, :]
11 frames
/usr/local/lib/python3.7/dist-packages/pytorch_grad_cam/base_cam.py in __call__(self, input_tensor, targets, aug_smooth, eigen_smooth)
183
184 return self.forward(input_tensor,
--> 185 targets, eigen_smooth)
186
187 def __del__(self):
/usr/local/lib/python3.7/dist-packages/pytorch_grad_cam/base_cam.py in forward(self, input_tensor, targets, eigen_smooth)
72 requires_grad=True)
73
---> 74 outputs = self.activations_and_grads(input_tensor)
75 if targets is None:
76 target_categories = np.argmax(outputs.cpu().data.numpy(), axis=-1)
/usr/local/lib/python3.7/dist-packages/pytorch_grad_cam/activations_and_gradients.py in __call__(self, x)
40 self.gradients = []
41 self.activations = []
---> 42 return self.model(x)
43
44 def release(self):
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
<ipython-input-28-838fcb9d22d5> in forward(self, x)
32
33 def forward(self, x):
---> 34 x = self.conv(x)
35 x = self.fc(x)
36 return x
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py in forward(self, input)
139 def forward(self, input):
140 for module in self:
--> 141 input = module(input)
142 return input
143
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/container.py in forward(self, input)
139 def forward(self, input):
140 for module in self:
--> 141 input = module(input)
142 return input
143
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
444
445 def forward(self, input: Tensor) -> Tensor:
--> 446 return self._conv_forward(input, self.weight, self.bias)
447
448 class Conv3d(_ConvNd):
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
441 _pair(0), self.dilation, self.groups)
442 return F.conv2d(input, weight, bias, self.stride,
--> 443 self.padding, self.dilation, self.groups)
444
445 def forward(self, input: Tensor) -> Tensor:
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 1, 3, 3], but got 3-dimensional input of size [16, 128, 128] instead
@jacobgil Hi again, just wanted to kindly follow up on this. Any luck?
Hi, what is 16 here, Is it a batch size or the image color channels?
Does the model run on 16 color channels ?
If it's the number of color channels, can you please try reshaping the input tensor into 1 x 16 x 128 x 128 ?
16 is the batch size. So should I still try to reshape it to (1, 16, 128, 128)?
No, I just wanted to make sure. So there is a batch of 16 different images we want to compute the CAM for, each is "grayscale" with only one color channels?
yes exactly
Ok thanks. I'm looking into it right now, will update.
I wrote a unit test to reproduce this, but couldn't reproduce the error so far. I modified a resnet18 model accept 1 color channel to test this.
Can you give a pointer on what model you're using - and what target layers? So I can test with it or something similar.
I just notice something - What is linear_layers ?
The target layers used need to have 2D outputs.
linear_layers
is a suspicious name so I want to make sure:)
Ah yes, that checks out. Thanks for the catch, it is working now!
Appreciate your time and effort :) Should've seen this myself!
I had the same error. Just for reference in my case the target layer was not set to trainable which caused this.
I had the same error. Just for reference in my case the target layer was not set to trainable which caused this.
Many thanks Dobatymo, this has helped me a lot!
Thanks a lot guys. I had the same error and I was facing this error due to choosing the wrong target_layers as well. I chose a 'classifier' layer in the EfficientNet_B0 model, and choosing the avgpool layer worked.
I had the same error , Did someone fix it? thanks
@arushsharma24 @Dobatymo Thx for the comment here. Finally got gradcam working.
Getting the following error when trying out the cam function on an image example. This might be an issue with how I have loaded in my data, but not sure how to debug it.
Code to reproduce:
Note the image is of shape
torch.Size([64, 1, 128, 128])
Here is the traceback: