import numpy as np import cv2 from pytorch_grad_cam import GradCAM from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image from PIL import Image def grad_cam(model, img, cls): model.eval() img = np.array(img) img = cv2.resize(img, (224, 224)) img = np.float32(img) / 255 input_tensor = preprocess_image(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # The target for the CAM is the Bear category. # As usual for classication, the target is the logit output # before softmax, for that category. targets = [ClassifierOutputTarget(1)] target_layers = [model.layer4[-1]] with GradCAM(model=model, target_layers=target_layers) as cam: grayscale_cams = cam(input_tensor=input_tensor, targets=targets) cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True) cam = np.uint8(255*grayscale_cams[0, :]) cam = cv2.merge([cam, cam, cam]) return Image.fromarray(cam_image)