Class Activation Mapping
论文:CVPR2016《Learning Deep Features for Discriminative Localization》
代码:https://github.com/acheketa/pytorch-CAM/blob/master/update.py
1、首先定义并训练好CNN网络,很重要的一点是网络的最后一个卷积层必须只有一个通道,并且紧跟着全连接层(最后一层),可以参考github上inception-v3网络结构,下面是我自己的网络结构最后两层。
这是我的网络里最后的设计,其中conv3是要观察的热力图,fcl1是最后dense到类数。
假设网路训练好,得到一个best_net。
2、CAM代码
# generate class activation mapping for the top1 prediction
def returnCAM(feature_conv, weight_softmax, class_idx):
# generate the class activation maps upsample to 256x256
size_upsample = (256, 256)
bz, nc, h, w = feature_conv.shape
output_cam = []