BhumikaMak commited on
Commit
eb18a04
·
1 Parent(s): 43bd3f4

Debug: yolov8 target lyr

Browse files
Files changed (1) hide show
  1. yolov8.py +9 -1
yolov8.py CHANGED
@@ -47,8 +47,15 @@ def xai_yolov8(image):
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
  print(model.model) # Output model layers to find the target layer
51
- grad_cam = GradCAM(model.model, target_layer='model.model[-1]')
 
 
 
 
 
52
 
53
  # Perform Grad-CAM
54
  cam_map = grad_cam(image_tensor)
@@ -59,4 +66,5 @@ def xai_yolov8(image):
59
  # Combine original image and Grad-CAM image
60
  final_image = np.hstack((np.array(image), cam_image))
61
  caption = "Results using YOLOv8 and Grad-CAM via torchcam"
 
62
  return Image.fromarray(final_image), caption
 
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
+
51
+ # Inspect the model and print layer structure
52
  print(model.model) # Output model layers to find the target layer
53
+
54
+ # Find the correct target layer
55
+ target_layer = model.model.model[-1] # Target the last detection layer
56
+
57
+ # Initialize Grad-CAM with the target layer
58
+ grad_cam = GradCAM(model.model, target_layer=target_layer)
59
 
60
  # Perform Grad-CAM
61
  cam_map = grad_cam(image_tensor)
 
66
  # Combine original image and Grad-CAM image
67
  final_image = np.hstack((np.array(image), cam_image))
68
  caption = "Results using YOLOv8 and Grad-CAM via torchcam"
69
+
70
  return Image.fromarray(final_image), caption