jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
10.55k stars 1.56k forks source link

RuntimeError: grad can be implicitly created only for scalar outputs #407

Open battulaharish2 opened 1 year ago

battulaharish2 commented 1 year ago

`import warnings warnings.filterwarnings('ignore') from torchvision import models import numpy as np import torch import cv2 import yaml import requests from models.experimental import attempt_load from pytorch_grad_cam import GradCAM, EigenGradCAM from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget from pytorch_grad_cam.utils.image import show_cam_on_image, \ deprocess_image, \ preprocess_image from PIL import Image

device = torch.device("cuda:0" if torch.cuda.isavailable() else "cpu") weigths = torch.load('yolov7.pt') model = weigths['model'] model = model.half().to(device) = model.eval()

image = cv2.imread(r"/home/harish/yolov7/figure/25.jpg")

image_url = r"/home/harish/yolov7/figure/25.jpg" img = np.array(Image.open(image_url)) 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]) input_tensor = input_tensor.half().to(device)

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.model] 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(255grayscale_cams[0, :]) cam = cv2.merge([cam, cam, cam]) images = np.hstack((np.uint8(255img), cam , cam_image)) cv2.imwrite("/home/harish/yolov7/figure/heatmap.jpg", images)`

ERROR Message:

Traceback (most recent call last): File "visulaization.py", line 39, in grayscale_cams = cam(input_tensor=input_tensor, targets=targets) File "/home/harish/.local/lib/python3.8/site-packages/pytorch_grad_cam/base_cam.py", line 188, in call return self.forward(input_tensor, File "/home/harish/.local/lib/python3.8/site-packages/pytorch_grad_cam/base_cam.py", line 84, in forward loss.backward(retain_graph=True) File "/home/harish/.local/lib/python3.8/site-packages/torch/_tensor.py", line 488, in backward torch.autograd.backward( File "/home/harish/.local/lib/python3.8/site-packages/torch/autograd/init.py", line 190, in backward gradtensors = _make_grads(tensors, gradtensors, is_grads_batched=False) File "/home/harish/.local/lib/python3.8/site-packages/torch/autograd/init.py", line 85, in _make_grads raise RuntimeError("grad can be implicitly created only for scalar outputs") RuntimeError: grad can be implicitly created only for scalar outputs

Error is with loss.backward i think. help is highly appreciated.

jacobgil commented 1 year ago

We can't use the Classifier Output Target, since it's an object detection network with multiple outputs. I'm also not sure if Yolo7 gives you a way to compute the gradients.

The documentation has examples for other YOLO version, I would start from there and then try to adapt to YOLO7.

sirius-cky commented 1 year ago

Have the same problem but still don‘t know how to solve it yet.