BhumikaMak commited on
Commit
93fea1b
·
1 Parent(s): 83c6e0c

Debug: parsing detections

Browse files
Files changed (1) hide show
  1. yolov8.py +10 -3
yolov8.py CHANGED
@@ -52,16 +52,23 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
52
  def xai_yolov8n(image):
53
  model = YOLO('yolov8n.pt') # Load YOLOv8n pre-trained weights
54
  model.eval()
55
- model.cpu()
56
 
 
 
 
57
  target_layers = [model.model.model[-2]] # Grad-CAM target layer
58
  results = model([image])
59
- boxes, colors, names = parse_detections(results)
 
 
 
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
  cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
65
  final_image = np.hstack((image, cam_image, renormalized_cam_image))
 
 
66
  caption = "Results using YOLOv8n"
67
  return Image.fromarray(final_image), caption
 
52
  def xai_yolov8n(image):
53
  model = YOLO('yolov8n.pt') # Load YOLOv8n pre-trained weights
54
  model.eval()
 
55
 
56
+ # Check if GPU is available and use it
57
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
58
+ model.to(device)
59
  target_layers = [model.model.model[-2]] # Grad-CAM target layer
60
  results = model([image])
61
+ if isinstance(results, list):
62
+ results = results[0] # Extracting the first result (if list)
63
+
64
+ boxes, colors, names = parse_detections([results]) # Ensure results are passed as a list
65
  detections_img = draw_detections(boxes, colors, names, image.copy())
66
  img_float = np.float32(image) / 255
67
  transform = transforms.ToTensor()
68
+ tensor = transform(img_float).unsqueeze(0).to(device) # Ensure tensor is on the right device
69
  cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
70
  final_image = np.hstack((image, cam_image, renormalized_cam_image))
71
+
72
+ # Return final image and a caption
73
  caption = "Results using YOLOv8n"
74
  return Image.fromarray(final_image), caption