frgfm/torch-cam

Problem to get CAM with SqueezeNet

mchelali opened this issue · 1 comments

Hi,

I try to get CAM with SqueezeNet. But this model have a classifier composed with a convolution layer followed by a global average pooling one that replace the FC one.

The used code is

net = squeezenet1_1(pretrained=True).eval()

getcam = CAM(net, "features", "classifier.1")
out = net(img.unsqueeze(0).float())
saillancy_map = getcam(class_idx=out.argmax(1).item(), scores=out)
print(saillancy_map.shape)

Now the problem is on the shape of the obtained activation map saillancy_map which is torch.Size([1, 512, 13, 13]).

Need some help to solve this.

frgfm commented

Hello there 👋

Would you mind specifying how you installed torchcam? My best guess is that you installed the latest release via pip, which doesn't support CAM without FC layers, but following #69, this has been improved!

So if you do the following:

git clone https://github.com/frgfm/torch-cam.git
pip install -e torch-cam/. --upgrade

so get the last updates from the master branch, the following code

import torch
from torchvision.models import squeezenet1_1
from torchcam.cams import CAM

model = squeezenet1_1().eval()
cam_extractor = CAM(model, "features", "classifier.1")
out = model(torch.rand((1, 3, 224, 224)))
activation_map = cam_extractor(out.argmax(1).item(), out)
print(activation_map.shape)

yields

torch.Size([13, 13])

Let me know if that helps!