BhumikaMak commited on
Commit
979f955
·
1 Parent(s): abce576

Debug: param extraction issue

Browse files
Files changed (1) hide show
  1. yolov8.py +2 -14
yolov8.py CHANGED
@@ -41,11 +41,7 @@ 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())
@@ -56,27 +52,19 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
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
 
41
  cam = EigenCAM(model, target_layers)
42
  grayscale_cam = cam(tensor)[0, :, :]
43
  img_float = np.float32(rgb_img) / 255
 
 
44
  cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
 
 
45
  renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
46
  for x1, y1, x2, y2 in boxes:
47
  renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
 
52
 
53
 
54
  def xai_yolov8n(image):
 
55
  model = YOLO('yolov8n.pt') # Load YOLOv8n pre-trained weights
56
  model.eval()
57
  model.cpu()
58
 
59
  target_layers = [model.model.model[-2]] # Grad-CAM target layer
 
 
60
  results = model([image])
61
+ print(results)
62
+ boxes, colors, names = parse_detections(results[0])
63
  detections_img = draw_detections(boxes, colors, names, image.copy())
 
 
64
  img_float = np.float32(image) / 255
65
  transform = transforms.ToTensor()
66
  tensor = transform(img_float).unsqueeze(0)
 
 
67
  cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
 
 
68
  final_image = np.hstack((image, cam_image, renormalized_cam_image))
69
  caption = "Results using YOLOv8n"
70
  return Image.fromarray(final_image), caption