Explainable YOLOv8 with GRADCAM
Loving YOLOv8 ? It’s great but lacks built-in Explainable AI features like GRAD-CAM. Found a GitHub repo to integrate EigenCAM for better transparency.
I am a big fan of YOLO (You Only Look Once) models, especially version 8. It is easy to train and deploy, even in the medical imaging domain. I recently finished a classification problem using YOLOv8, and it worked quite well. However, the main issue was its lack of an inbuilt Explainable results function like GRAD-CAM or Eigen-CAM. After searching on the internet for hours, I found a GitHub repository that does exactly what I wanted, and even more. It is also easy to use. So, let’s see how to use:
- Introduction
- Step cloning github repository
- Importing libraries
- Initializing EigenCAM
The integration of Eigen-CAM into YOLO (You Only Look Once) object detectors has marked a significant advancement in explainable Artificial Intelligence (XAI) for visual object detection. This integration is particularly notable given the complexity of contemporary computer vision models and the need to explain their behavior more transparently. Eigen-CAM (Eigen Class Activation Mapping) is instrumental in making sense of how these models arrive at specific predictions.
To create a tutorial based on the GitHub repository for integrating EigenCAM with the YOLO V8 model, follow these steps:
Step 1: Cloning the Repository
Clone the repository from [GitHub](https://github.com/rigvedrs/YOLO-V8-CAM) or download the `yolo_cam` folder directly.
Step 2: Importing Libraries
- Import necessary libraries from the cloned package:
from yolo_cam.eigen_cam import EigenCAM
from yolo_cam.utils.image import show_cam_on_image, scale_cam_image
Step 3: Initializing EigenCAM
- Initialize EigenCAM with the YOLO V8 model and target layers. Specify the task (`’cls’` for classification or `’od’` for object detection). The default task is `’od’`.
cam = EigenCAM(model, target_layers,task='cls')
Step 4: Generating and Visualizing Heatmaps
- Generate a grayscale heatmap and overlay it on the input image.
grayscale_cam = cam(rgb_img)[0, :, :]
cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True) # it will generate colored image
plt.imshow(cam_image)
plt.show()
Some times you need to run the above two steps again (a small glitch in code).
Step 5: Results
Below are the results when we use YOLO for two different tasks: classification and object detection.
Check out the provided Jupyter Notebook titled “YOLO v8n EigenCAM” for a detailed understanding.
Now, please try it by yourself 😃. Happy Learning.
References
- https://github.com/rigvedrs/YOLO-V8-CAM/blob/main/YOLO%20v8n%20EigenCAM.ipynb
- https://arxiv.org/abs/2008.00299
If you enjoy my article, please clap and follow me here on Medium.