BhumikaMak commited on
Commit
383e8f6
·
1 Parent(s): 672c2bf

Debug: refactor src

Browse files
Files changed (1) hide show
  1. yolov8.py +15 -26
yolov8.py CHANGED
@@ -8,21 +8,19 @@ from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
8
  import gradio as gr
9
  from ultralytics import YOLO
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
 
@@ -53,27 +51,18 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
53
  return cam_image, renormalized_cam_image
54
 
55
  def xai_yolov8s(image):
56
- # Load YOLOv8 model
57
- model = YOLO('yolov8s.pt')
58
  model.eval()
59
- model.cpu()
60
-
61
- target_layers = [model.model.model[-2]] # Grad-CAM target layer
62
-
63
- # Run YOLO detection
64
- results = model([image])
65
- boxes, colors, names = parse_detections(results)
66
  detections_img = draw_detections(boxes, colors, names, image.copy())
67
-
68
- # Prepare input tensor for Grad-CAM
69
  img_float = np.float32(image) / 255
70
  transform = transforms.ToTensor()
71
  tensor = transform(img_float).unsqueeze(0)
72
-
73
- # Grad-CAM visualization
74
- cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
75
-
76
- # Combine results
77
  final_image = np.hstack((image, cam_image, renormalized_cam_image))
78
  caption = "Results using YOLOv8"
79
  return Image.fromarray(final_image), caption
 
8
  import gradio as gr
9
  from ultralytics import YOLO
10
 
 
 
11
 
12
+ COLORS = np.random.uniform(0, 255, size=(80, 3))
13
+ def parse_detections(detections, model):
14
  boxes, colors, names = [], [], []
15
+ for detection in detections.boxes:
16
+ xmin, ymin, xmax, ymax = map(int, detection.xyxy[0].tolist())
17
+ confidence = detection.conf.item()
18
  if confidence < 0.2:
19
  continue
20
+ class_id = int(detection.cls.item())
21
+ name = model.names[class_id]
 
22
  boxes.append((xmin, ymin, xmax, ymax))
23
+ colors.append(COLORS[class_id])
24
  names.append(name)
25
  return boxes, colors, names
26
 
 
51
  return cam_image, renormalized_cam_image
52
 
53
  def xai_yolov8s(image):
54
+ model = YOLO('yolov8s.pt') # Ensure the model weights are available
 
55
  model.eval()
56
+ results = model(image)
57
+ detections = results[0]
58
+
59
+ boxes, colors, names = parse_detections(detections, model)
 
 
 
60
  detections_img = draw_detections(boxes, colors, names, image.copy())
 
 
61
  img_float = np.float32(image) / 255
62
  transform = transforms.ToTensor()
63
  tensor = transform(img_float).unsqueeze(0)
64
+ target_layers = [model.model[-2]] # Adjust according to YOLOv8 architecture
65
+ cam_image, renormalized_cam_image = generate_cam_image(model.model, target_layers, tensor, image, boxes)
 
 
 
66
  final_image = np.hstack((image, cam_image, renormalized_cam_image))
67
  caption = "Results using YOLOv8"
68
  return Image.fromarray(final_image), caption