Skip to content

Instantly share code, notes, and snippets.

@Siyeong-Lee
Created November 15, 2019 07:17
Show Gist options
  • Select an option

  • Save Siyeong-Lee/8789d56b87de94cc7936c6ad49c21535 to your computer and use it in GitHub Desktop.

Select an option

Save Siyeong-Lee/8789d56b87de94cc7936c6ad49c21535 to your computer and use it in GitHub Desktop.
params = list(net.parameters())
# get only weight from last layer(linear)
weight_softmax = np.squeeze(params[-2].cpu().data.numpy())
def returnCAM(feature_conv, weight_softmax, class_idx):
size_upsample = (128, 128)
bz, nc, h, w = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = weight_softmax[class_idx].dot(feature_conv.reshape( (nc, h*w)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam/np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment