BhumikaMak commited on
Commit
978b355
·
1 Parent(s): afe9924

Debug: yolov8 target lyr

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. yolov8.py +53 -38
app.py CHANGED
@@ -26,7 +26,7 @@ interface = gr.Interface(
26
  inputs=[
27
  gr.Image(type="pil", label="Upload an Image"),
28
  gr.CheckboxGroup(
29
- choices=["yolov5", "yolov8", "yolov10"],
30
  value=["yolov5"], # Set default selection to YOLOv5
31
  label="Select Model(s)",
32
  )
 
26
  inputs=[
27
  gr.Image(type="pil", label="Upload an Image"),
28
  gr.CheckboxGroup(
29
+ choices=["yolov5", "yolov8n", "yolov10"],
30
  value=["yolov5"], # Set default selection to YOLOv5
31
  label="Select Model(s)",
32
  )
yolov8.py CHANGED
@@ -1,29 +1,32 @@
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
 
9
- # Set random colors for detection bounding boxes
10
  COLORS = np.random.uniform(0, 255, size=(80, 3))
11
 
12
- def parse_detections_yolov8(results):
 
13
  boxes, colors, names = [], [], []
14
- detections = results.boxes
15
- for box in detections:
16
- confidence = box.conf[0].item()
17
- if confidence < 0.2: # Filter out low-confidence detections
18
  continue
19
- xmin, ymin, xmax, ymax = map(int, box.xyxy[0].tolist())
20
- category = int(box.cls[0].item())
21
- name = results.names[category]
22
  boxes.append((xmin, ymin, xmax, ymax))
23
  colors.append(COLORS[category])
24
  names.append(name)
25
  return boxes, colors, names
26
 
 
27
  def draw_detections(boxes, colors, names, img):
28
  for box, color, name in zip(boxes, colors, names):
29
  xmin, ymin, xmax, ymax = box
@@ -33,35 +36,47 @@ def draw_detections(boxes, colors, names, img):
33
  lineType=cv2.LINE_AA)
34
  return img
35
 
36
- def xai_yolov8(image):
37
- # Load YOLOv8 model
38
- model = YOLO('yolov8n.pt') # Load YOLOv8 nano model
39
- model.to('cpu')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  model.eval()
 
 
 
41
 
42
  # Run YOLO detection
43
- results = model(image)
44
- boxes, colors, names = parse_detections_yolov8(results[0])
45
  detections_img = draw_detections(boxes, colors, names, image.copy())
46
 
47
- # Convert image to PyTorch tensor for Grad-CAM
48
- image_tensor = torch.tensor(np.array(image)).permute(2, 0, 1).unsqueeze(0).float() / 255.0
49
- image_tensor = image_tensor.to('cpu')
50
- model(image_tensor) # Forward pass
51
- print(model.model) # Output model layers to find the target layer
52
- target_layer = model.model.model[-1] # Target the last detection layer
53
-
54
- # Initialize Grad-CAM with the target layer
55
- grad_cam = GradCAM(model.model, target_layer=target_layer)
56
-
57
- # Perform Grad-CAM
58
- cam_map = grad_cam(image_tensor)
59
-
60
- # Overlay Grad-CAM mask onto original image
61
- cam_image = overlay_mask(image, cam_map.squeeze(0).cpu().numpy(), alpha=0.5)
62
-
63
- # Combine original image and Grad-CAM image
64
- final_image = np.hstack((np.array(image), cam_image))
65
- caption = "Results using YOLOv8 and Grad-CAM via torchcam"
66
-
67
  return Image.fromarray(final_image), caption
 
1
  from ultralytics import YOLO
2
+ import torch
3
  import cv2
4
  import numpy as np
5
  from PIL import Image
6
+ import torchvision.transforms as transforms
7
+ from pytorch_grad_cam import EigenCAM
8
+ from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
9
+ import gradio as gr
10
 
11
+ # Global Color Palette
12
  COLORS = np.random.uniform(0, 255, size=(80, 3))
13
 
14
+ def parse_detections(results):
15
+ detections = results.pandas().xyxy[0].to_dict()
16
  boxes, colors, names = [], [], []
17
+ for i in range(len(detections["xmin"])):
18
+ confidence = detections["confidence"][i]
19
+ if confidence < 0.2:
 
20
  continue
21
+ xmin, ymin = int(detections["xmin"][i]), int(detections["ymin"][i])
22
+ xmax, ymax = int(detections["xmax"][i]), int(detections["ymax"][i])
23
+ name, category = detections["name"][i], int(detections["class"][i])
24
  boxes.append((xmin, ymin, xmax, ymax))
25
  colors.append(COLORS[category])
26
  names.append(name)
27
  return boxes, colors, names
28
 
29
+
30
  def draw_detections(boxes, colors, names, img):
31
  for box, color, name in zip(boxes, colors, names):
32
  xmin, ymin, xmax, ymax = box
 
36
  lineType=cv2.LINE_AA)
37
  return img
38
 
39
+
40
+ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
41
+ cam = EigenCAM(model, target_layers)
42
+ grayscale_cam = cam(tensor)[0, :, :]
43
+ img_float = np.float32(rgb_img) / 255
44
+
45
+ # Generate Grad-CAM
46
+ cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
47
+
48
+ # Renormalize Grad-CAM inside bounding boxes
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())
52
+ renormalized_cam = scale_cam_image(renormalized_cam)
53
+ renormalized_cam_image = show_cam_on_image(img_float, renormalized_cam, use_rgb=True)
54
+
55
+ return cam_image, renormalized_cam_image
56
+
57
+
58
+ def xai_yolov8n(image):
59
+ # Load YOLOv8n model
60
+ model = YOLO('yolov8n.pt') # Load YOLOv8n pre-trained weights
61
  model.eval()
62
+ model.cpu()
63
+
64
+ target_layers = [model.model.model[-2]] # Grad-CAM target layer
65
 
66
  # Run YOLO detection
67
+ results = model([image])
68
+ boxes, colors, names = parse_detections(results)
69
  detections_img = draw_detections(boxes, colors, names, image.copy())
70
 
71
+ # Prepare input tensor for Grad-CAM
72
+ img_float = np.float32(image) / 255
73
+ transform = transforms.ToTensor()
74
+ tensor = transform(img_float).unsqueeze(0)
75
+
76
+ # Grad-CAM visualization
77
+ cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
78
+
79
+ # Combine results
80
+ final_image = np.hstack((image, cam_image, renormalized_cam_image))
81
+ caption = "Results using YOLOv8n"
 
 
 
 
 
 
 
 
 
82
  return Image.fromarray(final_image), caption