How To Visualize Image Classification (ResnetV2)

I am using the modelzoo resnetv2 model for training a classification problem. There are some misclassified cases and I would like to see what areas of the image are most strongly related to the misclassification.

Does anything exist within Mxnet or Gluon for determining this? I’ve found some github examples in other frames, such as:

There is an mxnet implement for gradcam available here

This site might be helpful too:

Sorry, this site is more helpful, I think.

Try this code.
This is a mxnet code for CAM (not for Gradcam).

import numpy as np
import mxnet as mx
from mxnet import image
from gluoncv import utils
from gluoncv.model_zoo import get_model
from import transforms
import cv2

url = ''
filename = 'example.jpg', filename)

#net = get_model('resnet18_v1', pretrained=True)
net = get_model('resnet18_v2', pretrained=True)

params_list = list(net.collect_params())
weight_softmax = net.collect_params()[params_list[-2]].data().asnumpy()

def returnCAM(feature_conv, weight_softmax, idx):
    # generate the class activation maps upsample to 256x256
    size_upsample = (256, 256)
    bz, nc, h, w = feature_conv.shape
    cam = weight_softmax[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)
    return cv2.resize(cam_img, size_upsample)
transform = transforms.Compose([
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

img = image.imread(filename)
img = transform(img)
img = img.expand_dims(0)

output = net(img)

classes = net.classes

idx = mx.nd.argmax(output).asscalar().astype('int32')

features_blob = net.features[:-4](img).asnumpy()

CAMs = returnCAM(features_blob, weight_softmax, idx)

print('output CAM.jpg for the top1 prediction: %s'%classes[idx])
img = cv2.imread(filename)
height, width, _ = img.shape
heatmap = cv2.applyColorMap(cv2.resize(CAMs,(width, height)), cv2.COLORMAP_JET)
result = heatmap * 0.3 + img * 0.5
cv2.imwrite('CAM.jpg', result)

output CAM.jpg for the top1 prediction: beer bottle

1 Like

Thanks for all the resources! I’ve been working on another problem but will try this out next week!