Spaces:
Sleeping
Sleeping
Commit
·
175164a
1
Parent(s):
f6aa311
Update: class lables
Browse files
yolov5.py
CHANGED
@@ -45,11 +45,7 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
|
|
45 |
cam = EigenCAM(model, target_layers)
|
46 |
grayscale_cam = cam(tensor)[0, :, :]
|
47 |
img_float = np.float32(rgb_img) / 255
|
48 |
-
|
49 |
-
# Generate Grad-CAM
|
50 |
cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
|
51 |
-
|
52 |
-
# Renormalize Grad-CAM inside bounding boxes
|
53 |
renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
|
54 |
for x1, y1, x2, y2 in boxes:
|
55 |
renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
|
@@ -60,7 +56,6 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
|
|
60 |
|
61 |
|
62 |
def xai_yolov5(image):
|
63 |
-
# Load YOLOv5 model
|
64 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
65 |
model.eval()
|
66 |
model.cpu()
|
@@ -81,6 +76,9 @@ def xai_yolov5(image):
|
|
81 |
cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
|
82 |
|
83 |
# Combine results
|
84 |
-
final_image = np.hstack((image,
|
85 |
caption = "Results using YOLOv5"
|
86 |
-
return Image.fromarray(final_image), caption
|
|
|
|
|
|
|
|
45 |
cam = EigenCAM(model, target_layers)
|
46 |
grayscale_cam = cam(tensor)[0, :, :]
|
47 |
img_float = np.float32(rgb_img) / 255
|
|
|
|
|
48 |
cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
|
|
|
|
|
49 |
renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
|
50 |
for x1, y1, x2, y2 in boxes:
|
51 |
renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
|
|
|
56 |
|
57 |
|
58 |
def xai_yolov5(image):
|
|
|
59 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
60 |
model.eval()
|
61 |
model.cpu()
|
|
|
76 |
cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
|
77 |
|
78 |
# Combine results
|
79 |
+
final_image = np.hstack((image, detections_img, renormalized_cam_image))
|
80 |
caption = "Results using YOLOv5"
|
81 |
+
return Image.fromarray(final_image), caption
|
82 |
+
|
83 |
+
|
84 |
+
|