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.06k stars 1.52k forks source link

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn #250

Closed akashlp27 closed 2 years ago

akashlp27 commented 2 years ago

Hi, same issue while running the ipynb file though, My torch version 1.8.1 and gradcam version is 1.3.7

attached is the code:

import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')
import torch
import cv2
import numpy as np
import requests
import torchvision.transforms as transforms
from pytorch_grad_cam import EigenCAM
from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
from PIL import Image

COLORS = np.random.uniform(0, 255, size=(80, 3))

def parse_detections(results):
    detections = results.pandas().xyxy[0]
    detections = detections.to_dict()
    boxes, colors, names = [], [], []

    for i in range(len(detections["xmin"])):
        confidence = detections["confidence"][i]
        if confidence < 0.2:
            continue
        xmin = int(detections["xmin"][i])
        ymin = int(detections["ymin"][i])
        xmax = int(detections["xmax"][i])
        ymax = int(detections["ymax"][i])
        name = detections["name"][i]
        category = int(detections["class"][i])
        color = COLORS[category]

        boxes.append((xmin, ymin, xmax, ymax))
        colors.append(color)
        names.append(name)
    return boxes, colors, names

def draw_detections(boxes, colors, names, img):
    for box, color, name in zip(boxes, colors, names):
        xmin, ymin, xmax, ymax = box
        cv2.rectangle(
            img,
            (xmin, ymin),
            (xmax, ymax),
            color,
            2)

        cv2.putText(img, name, (xmin, ymin - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2,
                    lineType=cv2.LINE_AA)
    return img

# image_url = "https://upload.wikimedia.org/wikipedia/commons/f/f1/Puppies_%284984818141%29.jpg"
imh_path = r'C:\Users\user\PycharmProjects\yolov5_u\yolov5\data\images\bus.jpg'
img = np.array(Image.open(imh_path))
img = cv2.resize(img, (640, 640))
rgb_img = img.copy()
img = np.float32(img) / 255
transform = transforms.ToTensor()
tensor = transform(img).unsqueeze(0)

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()
model.cpu()
target_layers = [model.model.model.model[-2]]

results = model([rgb_img])
boxes, colors, names = parse_detections(results)
detections = draw_detections(boxes, colors, names, rgb_img.copy())
Image.fromarray(detections)

cam = EigenCAM(model, target_layers, use_cuda=False)
grayscale_cam = cam(tensor)[0, :, :]
cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True)
Image.fromarray(cam_image)

Any inputs would help a lot.

sadbhawnathakur commented 2 years ago

use the complete github repository, pip install grad-cam is not updated yet. Worked for me

jacobgil commented 2 years ago

Hi I updated the pip version now. Can you please upgrade the version with pip install grad-cam --upgrade, and check if it works ?

zpyuan6 commented 2 years ago

Hi, It does not work. I meet the same error when I run tutorials/EigenCAM for YOLO5.ipynb. I try pip uninstall grad-cam and pip install grad-cam for upgrade. I use python 3.7.13, torch 1.10.2, grad-cam 1.3.7

akashlp27 commented 2 years ago

Hi, I used pip install git+https://github.com/jacobgil/pytorch-grad-cam.git

and it worked closing the issue, thanks

zpyuan6 commented 2 years ago

Great, it worked for me. Thanks