rigvedrs / YOLO-V8-CAM

Wanna know what your model sees? Here's a package for applying EigenCAM on the new YOLO V8 model
MIT License
131 stars 26 forks source link

Same input as yours but different results #11

Closed msadoun closed 11 months ago

msadoun commented 11 months ago

I tried to implement your exact GRAD-CAM on the "puppies" image with your attached "yolov8n.pt".

But some how I seem to get different heatmap. Here is my implementation script

`if name == 'main': from ultralytics import YOLO from yolo_cam.eigen_cam import EigenCAM from yolo_cam.utils.image import show_cam_on_image, scale_cam_image import warnings warnings.filterwarnings('ignore') warnings.simplefilter('ignore') import torch
import cv2 import numpy as np import matplotlib.pyplot as plt import requests import torchvision.transforms as transforms from PIL import Image import io

test = r"D:\GRAD-CAM\images\puppies.jpg"
img = cv2.imread(test)
#img = cv2.resize(img, (640, 640))
rgb_img = img.copy()
img = np.float32(img) / 255

model = YOLO(r"D:\GRAD-CAM\models\yolov8n.pt")  # pretrained YOLOv8n model
#results = model(source = test ,device = 0, line_width = 3, save = True)  # return a list of Results objects
target_layers =[model.model.model[-2]]

cam = EigenCAM(model, target_layers,task='od')
grayscale_cam = cam(rgb_img)[0, :, :]
cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True)
plt.imshow(cam_image)
plt.show()`

this code generates this image

grad

When I uncomment results = model(source = test ,device = 0, line_width = 3, save = True) # return a list of Results objects

the generated image is

grad2

in both cases "yolov8n.pt" detects the image as follows

puppies

so my question: what went wrong here?

rigvedrs commented 11 months ago

Yes you will have this issue since the output will come wrong on every first run. Since you are not running this in a notebook, a temporary fix for this can be by running the model twice on the image and outputting the second output generated. The first output will always be inaccurate, but all the subsequent outputs will be the same and accurate.

# Run this twice
grayscale_cam = cam(rgb_img)[0, :, :]
grayscale_cam = cam(rgb_img)[0, :, :]

# Rest remains the same
cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True)
plt.imshow(cam_image)
plt.show()