pytorch-grad-cam
pytorch-grad-cam copied to clipboard
can I get cam from some networks may cannot calculate loss
some networks is trained to extract features so that they have no classifier module (like arcface, it is based on iresnet, but pretrained model is trained without classfier), their output may be a feature which size is (1, 512). whether I can use this method to get cam.
arcface's forward mothod:
def forward(self, x):
with torch.cuda.amp.autocast(self.fp16):
x = self.conv1(x)
x = self.bn1(x)
x = self.prelu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.bn2(x)
x = torch.flatten(x, 1)
x = self.dropout(x)
x = self.fc(x.float() if self.fp16 else x)
x = self.features(x) # 1*512
return x
Yes.
I think you can set one of the layers that does have a spatial output.
target_layers = [model.bn2]
Yes. I think you can set one of the layers that does have a spatial output.
target_layers = [model.bn2]
thanks a lot for your reply! and i have some questions else. i think the final loss in the code is the result of the fc layer specially for classfication. The most possible result is assumed to be a classsification target. but my network(like arcface) does not have a fc layer for classification, only a fc layer for feature extraction. will the assumed category abtained from this fc layer be meaningless(compare with a special fc layer).
So you want to only visualize the features you have in general, but not how they correspond which some classification categories, since there is no classifier attached.
You can use either eigen-cam for that,
from pytorch_grad_cam import EigenCAM,
or Deep Feature Factorization for better detail: https://jacobgil.github.io/pytorch-gradcam-book/Deep%20Feature%20Factorizations.html
So you want to only visualize the features you have in general, but not how they correspond which some classification categories, since there is no classifier attached.
You can use either eigen-cam for that,
from pytorch_grad_cam import EigenCAM,or Deep Feature Factorization for better detail: https://jacobgil.github.io/pytorch-gradcam-book/Deep%20Feature%20Factorizations.html
thank you for your patient explanation!