BhumikaMak commited on
Commit
e1e2e01
·
1 Parent(s): 8252ece

Update: class lables

Browse files
Files changed (2) hide show
  1. yolov5.py +15 -21
  2. yolov8.py +8 -26
yolov5.py CHANGED
@@ -7,10 +7,13 @@ from pytorch_grad_cam import EigenCAM
7
  from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
8
  import gradio as gr
9
 
 
10
  COLORS = np.random.uniform(0, 255, size=(80, 3))
 
 
11
  def parse_detections(results):
12
  detections = results.pandas().xyxy[0].to_dict()
13
- boxes, colors, names, classes = [], [], [], []
14
  for i in range(len(detections["xmin"])):
15
  confidence = detections["confidence"][i]
16
  if confidence < 0.2:
@@ -21,8 +24,7 @@ def parse_detections(results):
21
  boxes.append((xmin, ymin, xmax, ymax))
22
  colors.append(COLORS[category])
23
  names.append(name)
24
- classes.append(category) # Add class ID
25
- return boxes, colors, names, classes
26
 
27
 
28
  def draw_detections(boxes, colors, names, classes, img):
@@ -42,12 +44,17 @@ def generate_cam_image(model, target_layers, tensor, rgb_img, boxes):
42
  cam = EigenCAM(model, target_layers)
43
  grayscale_cam = cam(tensor)[0, :, :]
44
  img_float = np.float32(rgb_img) / 255
 
 
45
  cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
 
 
46
  renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
47
  for x1, y1, x2, y2 in boxes:
48
  renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
49
  renormalized_cam = scale_cam_image(renormalized_cam)
50
  renormalized_cam_image = show_cam_on_image(img_float, renormalized_cam, use_rgb=True)
 
51
  return cam_image, renormalized_cam_image
52
 
53
 
@@ -61,8 +68,8 @@ def xai_yolov5(image):
61
 
62
  # Run YOLO detection
63
  results = model([image])
64
- boxes, colors, names, classes = parse_detections(results) # Extract classes
65
- detections_img = draw_detections(boxes, colors, names, classes, image.copy())
66
 
67
  # Prepare input tensor for Grad-CAM
68
  img_float = np.float32(image) / 255
@@ -73,19 +80,6 @@ def xai_yolov5(image):
73
  cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
74
 
75
  # Combine results
76
- final_image = np.hstack((
77
- cv2.cvtColor(image, cv2.COLOR_RGB2BGR),
78
- detections_img,
79
- cv2.cvtColor(cam_image, cv2.COLOR_RGB2BGR)
80
- ))
81
- caption = "Results using YOLOv5 with Class Labels and Bounding Boxes"
82
- return Image.fromarray(final_image), caption
83
-
84
-
85
- # Example Gradio interface
86
- gr.Interface(
87
- fn=xai_yolov5,
88
- inputs=gr.Image(type="numpy"),
89
- outputs=[gr.Image(type="pil"), gr.Text()],
90
- title="YOLOv5 with XAI and Class Labels"
91
- ).launch()
 
7
  from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image
8
  import gradio as gr
9
 
10
+ # Global Color Palette
11
  COLORS = np.random.uniform(0, 255, size=(80, 3))
12
+
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:
 
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, classes, img):
 
44
  cam = EigenCAM(model, target_layers)
45
  grayscale_cam = cam(tensor)[0, :, :]
46
  img_float = np.float32(rgb_img) / 255
47
+
48
+ # Generate Grad-CAM
49
  cam_image = show_cam_on_image(img_float, grayscale_cam, use_rgb=True)
50
+
51
+ # Renormalize Grad-CAM inside bounding boxes
52
  renormalized_cam = np.zeros(grayscale_cam.shape, dtype=np.float32)
53
  for x1, y1, x2, y2 in boxes:
54
  renormalized_cam[y1:y2, x1:x2] = scale_cam_image(grayscale_cam[y1:y2, x1:x2].copy())
55
  renormalized_cam = scale_cam_image(renormalized_cam)
56
  renormalized_cam_image = show_cam_on_image(img_float, renormalized_cam, use_rgb=True)
57
+
58
  return cam_image, renormalized_cam_image
59
 
60
 
 
68
 
69
  # Run YOLO detection
70
  results = model([image])
71
+ boxes, colors, names = parse_detections(results)
72
+ detections_img = draw_detections(boxes, colors, names, image.copy())
73
 
74
  # Prepare input tensor for Grad-CAM
75
  img_float = np.float32(image) / 255
 
80
  cam_image, renormalized_cam_image = generate_cam_image(model, target_layers, tensor, image, boxes)
81
 
82
  # Combine results
83
+ final_image = np.hstack((image, cam_image, renormalized_cam_image))
84
+ caption = "Results using YOLOv5"
85
+ return Image.fromarray(final_image), caption
 
 
 
 
 
 
 
 
 
 
 
 
 
yolov8.py CHANGED
@@ -10,9 +10,8 @@ from ultralytics import YOLO
10
 
11
 
12
  COLORS = np.random.uniform(0, 255, size=(80, 3))
13
-
14
  def parse_detections(detections, model):
15
- boxes, colors, names, classes = [], [], [], []
16
  for detection in detections.boxes:
17
  xmin, ymin, xmax, ymax = map(int, detection.xyxy[0].tolist())
18
  confidence = detection.conf.item()
@@ -23,8 +22,7 @@ def parse_detections(detections, model):
23
  boxes.append((xmin, ymin, xmax, ymax))
24
  colors.append(COLORS[class_id])
25
  names.append(name)
26
- classes.append(class_id) # Add class ID
27
- return boxes, colors, names, classes
28
 
29
  def draw_detections(boxes, colors, names, classes, img):
30
  for box, color, name, cls in zip(boxes, colors, names, classes):
@@ -53,33 +51,17 @@ 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
- model = YOLO('yolov8s.pt')
57
  model.eval()
58
  results = model(image)
59
  detections = results[0]
60
- boxes, colors, names, classes = parse_detections(detections, model)
61
- detections_img = draw_detections(boxes, colors, names, classes, image.copy())
62
  img_float = np.float32(image) / 255
63
  transform = transforms.ToTensor()
64
  tensor = transform(img_float).unsqueeze(0)
65
  target_layers = [model.model.model[-2]] # Adjust to YOLOv8 architecture
66
  cam_image, renormalized_cam_image = generate_cam_image(model.model, target_layers, tensor, image, boxes)
67
-
68
- # Combine the original, detection, and CAM images for visualization
69
- combined_image = np.hstack((
70
- cv2.cvtColor(image, cv2.COLOR_RGB2BGR),
71
- detections_img,
72
- cv2.cvtColor(cam_image, cv2.COLOR_RGB2BGR)
73
- ))
74
-
75
- caption = "Results using YOLOv8 with Class Labels and Bounding Boxes"
76
- return Image.fromarray(combined_image), caption
77
-
78
-
79
- # Example Gradio interface
80
- gr.Interface(
81
- fn=xai_yolov8s,
82
- inputs=gr.Image(type="numpy"),
83
- outputs=[gr.Image(type="pil"), gr.Text()],
84
- title="YOLOv8 with XAI and Class Labels"
85
- ).launch()
 
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()
 
22
  boxes.append((xmin, ymin, xmax, ymax))
23
  colors.append(COLORS[class_id])
24
  names.append(name)
25
+ return boxes, colors, names
 
26
 
27
  def draw_detections(boxes, colors, names, classes, img):
28
  for box, color, name, cls in zip(boxes, colors, names, classes):
 
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
+ boxes, colors, names = parse_detections(detections, model)
59
+ detections_img = draw_detections(boxes, colors, names, image.copy())
60
  img_float = np.float32(image) / 255
61
  transform = transforms.ToTensor()
62
  tensor = transform(img_float).unsqueeze(0)
63
  target_layers = [model.model.model[-2]] # Adjust to YOLOv8 architecture
64
  cam_image, renormalized_cam_image = generate_cam_image(model.model, target_layers, tensor, image, boxes)
65
+ final_image = np.hstack((image, cam_image, renormalized_cam_image))
66
+ caption = "Results using YOLOv8"
67
+ return Image.fromarray(final_image), caption