Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

easy-explain: Explainable AI for YoloV8
Computer Vision   Latest   Machine Learning

easy-explain: Explainable AI for YoloV8

Author(s): Stavros Theocharis

Originally published on Towards AI.

(Left) Photo by Pawel Czerwinski on Unsplash U+007C (Right) Unsplash Image adjusted by the showcased algorithm

Introduction

It’s been a while since I created this package ‘easy-explain’ and published on Pypi. I also wrote a Medium article about this package in the past to illustrate its use with image classification models. A few weeks ago, I needed an explainability algorithm for a YoloV8 model.

The truth is, I couldn’t find anything. After further digging, I found a repo. which included an LRP method for generating explanations for such models. I needed it to be slightly different, so I made some adjustments and decided to include it in my ‘easy-explain’ package, as there was no other available package offering the same functionality. Since this package was not originally intended for various use cases when I created it, I had to refactor many things, leading to some breaking changes in the new release.

A quick reference for what is a YOLO model. You Only Look Once (YOLO) is one of the most popular model architectures and object detection algorithms. It uses one of the best neural network architectures to produce high accuracy and overall processing speed, which is the main reason for its popularity.

Loading & Pre-processing of an image

Here, I use an image sourced from Unsplash, which I retrieve directly through a request. Afterwards, it needs to be opened with the Image class from the PIL library.

from PIL import Image
from io import BytesIO
import requests

response = requests.get("https://images.unsplash.com/photo-1530652101053-8c0db4fbb5de?q=80&w=2787&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D")
image = Image.open(BytesIO(response.content))

In addition, the loaded image will undergo a basic transformation with the help of the torchvision library to be compatible with the model.

import torchvision

desired_size = (512, 640)
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(desired_size),
torchvision.transforms.ToTensor(),
])
image = transform(image)

Loading YoloV8 model

You are free to use any YoloV8 model you prefer. Here, I am using a model from Ultralytics. I have also tested other models from Hugging Face, and the easy-explain package is working correctly!

from ultralytics import YOLO

model = YOLO('ultralyticsplus/yolov8s')

I will not delve into the details of how to make predictions using this model in this article. We are ready to utilize the easy-explain package…

Simple use of easy-explain package for a YoloV8 model

Here, we simply need to import the package and instantiate the corresponding class. The good news is that this explainer can be used with both CPU and GPU, so I will use the CPU. We also need to specify the class for which we seek to understand the prediction. Feel free to experiment with the arguments and check the results.

from easy_explain import YOLOv8LRP

lrp = YOLOv8LRP(model, power=2, eps=1, device='cpu')
explanation_lrp = lrp.explain(image, cls='traffic light', contrastive=False).cpu()

Now that we have our explanations, let’s use the appropriate function to visualize them in various ways. We’ll utilize the visualization functionality provided by the package:

lrp.plot_explanation(frame=image, explanation = explanation_lrp, contrastive=False, cmap='seismic', title='Explanation for Class "traffic light"')
(Left) Photo by Pawel Czerwinski on Unsplash U+007C (Right) Unsplash Image adjusted by the showcased algorithm

Here, I used contrastive=False to create a different visualization from the first image (which was produced with contrastive=True).

Let’s try it with another image and a different class (zebra). We’ll assume that we follow the same process as before for the new image. Then we have:

explanation_lrp = lrp.explain(image, cls='zebra', contrastive=False).cpu()
lrp.plot_explanation(frame=image, explanation = explanation_lrp, contrastive=False, cmap='Reds', title='Explanation for Class "zebra"')

and we get:

(Left) Photo by Jeff Griffith on Unsplash U+007C (Right) Unsplash Image adjusted by the showcased algorithm

For more information and ideas you can also check the example notebooks inside the github repositoty.

How does Layer-Wise Relevance Propagation (LRP) work?

Layer-wise Relevance Propagation (LRP) is a method used for explaining decisions made by models structured as neural networks, where inputs might include images, videos, or text. LRP works by backward propagating the prediction f(x) through the neural network, utilizing specifically tailored local propagation rules. The propagation process applied by LRP adheres to a conservation principle, ensuring that what a neuron has received is equally redistributed to the preceding layer.

The LRP procedure [2]

Conclusion

Generally, the field of explainable artificial intelligence (XAI) plays a crucial role in ensuring that artificial intelligence systems are interpretable, transparent, and reliable. By employing XAI techniques in the creation of image classification models, we can achieve a deeper comprehension of their functionality and make informed decisions regarding their application.

In this article, I showcased the new functionality of my easy-explain package. Through it, someone can easily and quickly explain and check the predictions of the YoloV8 trained models.

I encourage you to experiment with this new feature of my easy-explain package for explaining easily YoloV8 models.

Possible feedback is more than welcome! If you want to contribute to the project, please read the contribution guide.

U+1F31F If you enjoyed this article, follow me to read more!

References

[1] Bach, S., Binder, A., Montavon, G., Klauschen, F., Müller, K.R., Samek, W.: On pixel-wise explanations for non-linear classifier decisions by layer-wise relevance propagation. PLoS ONE 10(7), e0130140 (2015)

[2] Montavon, G., Binder, A., Lapuschkin, S., Samek, W., Müller, KR. (2019). Layer-Wise Relevance Propagation: An Overview. In: Samek, W., Montavon, G., Vedaldi, A., Hansen, L., Müller, KR. (eds) Explainable AI: Interpreting, Explaining and Visualizing Deep Learning. Lecture Notes in Computer Science(), vol 11700. Springer, Cham. https://doi.org/10.1007/978-3-030-28954-6_10

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓