Spaces:
Sleeping
Sleeping
Commit
·
2910097
1
Parent(s):
dc39201
Fix: resolve alternative conflicts
Browse files- requirements.txt +1 -1
- yolov8.py +18 -11
requirements.txt
CHANGED
@@ -7,4 +7,4 @@ opencv-python
|
|
7 |
grad-cam==1.4.8
|
8 |
gradio
|
9 |
ultralytics
|
10 |
-
|
|
|
7 |
grad-cam==1.4.8
|
8 |
gradio
|
9 |
ultralytics
|
10 |
+
torchcam
|
yolov8.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
from yolov8_explainer import YOLOv8Explainer
|
2 |
from ultralytics import YOLO
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
-
|
7 |
-
|
|
|
8 |
COLORS = np.random.uniform(0, 255, size=(80, 3))
|
9 |
|
10 |
def parse_detections_yolov8(results):
|
@@ -37,18 +37,25 @@ def xai_yolov8(image):
|
|
37 |
model.to('cpu')
|
38 |
model.eval()
|
39 |
|
40 |
-
# Initialize YOLOv8 Explainer
|
41 |
-
explainer = YOLOv8Explainer(model)
|
42 |
-
|
43 |
# Run YOLO detection
|
44 |
results = model(image)
|
45 |
boxes, colors, names = parse_detections_yolov8(results[0])
|
46 |
detections_img = draw_detections(boxes, colors, names, image.copy())
|
47 |
|
48 |
-
#
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Combine original
|
52 |
-
final_image = np.hstack((image, cam_image
|
53 |
-
caption = "Results using YOLOv8 and
|
54 |
return Image.fromarray(final_image), caption
|
|
|
|
|
1 |
from ultralytics import YOLO
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torchcam.methods import GradCAM
|
7 |
+
from torchcam.utils import overlay_mask
|
8 |
COLORS = np.random.uniform(0, 255, size=(80, 3))
|
9 |
|
10 |
def parse_detections_yolov8(results):
|
|
|
37 |
model.to('cpu')
|
38 |
model.eval()
|
39 |
|
|
|
|
|
|
|
40 |
# Run YOLO detection
|
41 |
results = model(image)
|
42 |
boxes, colors, names = parse_detections_yolov8(results[0])
|
43 |
detections_img = draw_detections(boxes, colors, names, image.copy())
|
44 |
|
45 |
+
# Convert image to PyTorch tensor for Grad-CAM
|
46 |
+
image_tensor = torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0).float() / 255.0
|
47 |
+
image_tensor = image_tensor.to('cpu')
|
48 |
+
|
49 |
+
# Initialize Grad-CAM
|
50 |
+
grad_cam = GradCAM(model.model, target_layer='backbone') # You can change the target_layer if needed
|
51 |
+
|
52 |
+
# Perform Grad-CAM
|
53 |
+
cam_map = grad_cam(image_tensor)
|
54 |
+
|
55 |
+
# Overlay Grad-CAM mask onto original image
|
56 |
+
cam_image = overlay_mask(image, cam_map.squeeze(0).cpu().numpy(), alpha=0.5)
|
57 |
|
58 |
+
# Combine original image and Grad-CAM image
|
59 |
+
final_image = np.hstack((np.array(image), cam_image))
|
60 |
+
caption = "Results using YOLOv8 and Grad-CAM via torchcam"
|
61 |
return Image.fromarray(final_image), caption
|