forked from jaidevd/booksort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_and_evaluate.py
More file actions
348 lines (314 loc) · 11.8 KB
/
detect_and_evaluate.py
File metadata and controls
348 lines (314 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import argparse
import json
import time
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional, Sequence
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image, ImageDraw
from ultralytics import YOLO
BOOK_CLASS_ID = 73 # COCO class index for books
DEFAULT_WEIGHTS = [
"yolo11n-seg.pt",
"yolo11s-seg.pt",
"yolo11m-seg.pt",
"yolo11l-seg.pt",
"yolo11x-seg.pt",
]
IOU_THRESHOLDS = np.arange(0.5, 0.96, 0.05)
DEFAULT_CONF_SWEEP = [round(float(x), 2) for x in np.linspace(0.05, 0.95, 10)]
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=(
"Run YOLO11 segmentation models on test images, save highlighted detections, "
"and compute mAP + inference timings."
)
)
parser.add_argument(
"--images-dir",
type=Path,
default=Path("test_images"),
help="Directory with evaluation images.",
)
parser.add_argument(
"--labels-dir",
type=Path,
default=Path("labels"),
help="Directory with polygon labels (JSON).",
)
parser.add_argument(
"--output-dir",
type=Path,
default=Path("book_detections"),
help="Root directory where highlighted detections are written.",
)
parser.add_argument(
"--weights",
nargs="+",
default=DEFAULT_WEIGHTS,
help="One or more YOLO11 segmentation weights to evaluate.",
)
parser.add_argument(
"--conf-threshold",
type=float,
default=0.1,
help="Minimum confidence to keep a detection.",
)
parser.add_argument(
"--mask-threshold",
type=float,
default=0.5,
help="Threshold applied to mask logits to obtain a binary mask.",
)
parser.add_argument(
"--map-conf-thresholds",
nargs="+",
type=float,
default=DEFAULT_CONF_SWEEP,
help="Confidence thresholds used to compute the mAP curve.",
)
return parser.parse_args()
def load_ground_truth_masks(labels_dir: Path, images_dir: Path) -> Dict[str, List[np.ndarray]]:
masks: Dict[str, List[np.ndarray]] = {}
for label_path in sorted(labels_dir.glob("*.json")):
image_name = label_path.stem
image_path = find_image_path(images_dir, image_name)
if image_path is None or not image_path.exists():
continue
with Image.open(image_path) as img:
width, height = img.size
with open(label_path, "r", encoding="utf-8") as f:
annotations = json.load(f)
polys = []
for obj in annotations:
points = [(pt["x"], pt["y"]) for pt in obj.get("content", [])]
if len(points) >= 3:
polys.append(points)
masks[image_name] = [polygon_to_mask(poly, height, width) for poly in polys]
return masks
def polygon_to_mask(points: Sequence[Sequence[float]], height: int, width: int) -> np.ndarray:
mask_image = Image.new("1", (width, height), 0)
draw = ImageDraw.Draw(mask_image)
draw.polygon(points, outline=1, fill=1)
return np.array(mask_image, dtype=bool)
def find_image_path(images_dir: Path, stem: str) -> Optional[Path]:
for ext in (".jpg", ".jpeg", ".png"):
candidate = images_dir / f"{stem}{ext}"
if candidate.exists():
return candidate
return None
def upscale_masks(mask_tensor: torch.Tensor, orig_shape: Sequence[int]) -> np.ndarray:
if mask_tensor.numel() == 0:
return np.empty((0, *orig_shape), dtype=np.float32)
resized = F.interpolate(
mask_tensor.unsqueeze(1),
size=orig_shape,
mode="bilinear",
align_corners=False,
)
return resized.squeeze(1).cpu().numpy()
def mask_iou(mask_a: np.ndarray, mask_b: np.ndarray) -> float:
intersection = np.logical_and(mask_a, mask_b).sum()
union = np.logical_or(mask_a, mask_b).sum()
if union == 0:
return 0.0
return float(intersection / union)
def compute_average_precision(predictions: List[dict], ground_truth: Dict[str, List[np.ndarray]]) -> float:
gt_count = sum(len(masks) for masks in ground_truth.values())
if gt_count == 0 or not predictions:
return 0.0
ap_scores = []
predictions = sorted(predictions, key=lambda x: x["score"], reverse=True)
for threshold in IOU_THRESHOLDS:
matched = {img_id: np.zeros(len(masks), dtype=bool) for img_id, masks in ground_truth.items()}
tps: List[int] = []
fps: List[int] = []
for pred in predictions:
image_id = pred["image_id"]
pred_mask = pred["mask"]
gt_masks = ground_truth.get(image_id, [])
if image_id not in matched:
matched[image_id] = np.zeros(len(gt_masks), dtype=bool)
best_iou = 0.0
best_idx = -1
for idx, gt_mask in enumerate(gt_masks):
if matched[image_id][idx]:
continue
iou = mask_iou(pred_mask, gt_mask)
if iou > best_iou:
best_iou = iou
best_idx = idx
if best_iou >= threshold and best_idx >= 0:
matched[image_id][best_idx] = True
tps.append(1)
fps.append(0)
else:
tps.append(0)
fps.append(1)
if not tps:
ap_scores.append(0.0)
continue
tps_cum = np.cumsum(tps)
fps_cum = np.cumsum(fps)
precisions = tps_cum / np.maximum(tps_cum + fps_cum, 1e-9)
recalls = tps_cum / max(gt_count, 1)
ap_scores.append(average_precision(recalls, precisions))
return float(np.mean(ap_scores))
def average_precision(recalls: np.ndarray, precisions: np.ndarray) -> float:
mrec = np.concatenate(([0.0], recalls, [1.0]))
mpre = np.concatenate(([0.0], precisions, [0.0]))
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
idx = np.where(mrec[1:] != mrec[:-1])[0]
return float(np.sum((mrec[idx + 1] - mrec[idx]) * mpre[idx + 1]))
def highlight_detection(base_image: np.ndarray, mask: np.ndarray, color=(255, 0, 0), alpha: float = 0.5) -> np.ndarray:
highlighted = base_image.copy()
if not mask.any():
return highlighted
overlay = np.zeros_like(highlighted, dtype=np.float32)
overlay[:, :] = np.array(color, dtype=np.float32)
highlighted = highlighted.astype(np.float32)
highlighted[mask] = (
highlighted[mask] * (1 - alpha) + overlay[mask] * alpha
)
return highlighted.astype(np.uint8)
def predict_image(
model: YOLO,
image_path: Path,
mask_threshold: float,
record_threshold: float,
highlight_threshold: float,
highlight_root: Path,
) -> tuple[List[dict], float]:
start = time.perf_counter()
result = model(image_path)[0]
inference_time = time.perf_counter() - start
detections: List[dict] = []
image_output_dir = highlight_root / image_path.stem
image_output_dir.mkdir(parents=True, exist_ok=True)
if result.masks is None or result.boxes is None or not len(result.boxes):
return detections, inference_time
mask_tensor = result.masks.data.to(dtype=torch.float32, device="cpu")
masks = upscale_masks(mask_tensor, result.masks.orig_shape)
classes = result.boxes.cls.cpu().numpy()
scores = result.boxes.conf.cpu().numpy()
base_image: Optional[np.ndarray] = None
saved = 0
for idx, (cls_id, score) in enumerate(zip(classes, scores)):
if cls_id != BOOK_CLASS_ID or score < record_threshold:
continue
binary_mask = masks[idx] > mask_threshold
if not binary_mask.any():
continue
detections.append(
{
"image_id": image_path.stem,
"score": float(score),
"mask": binary_mask,
}
)
if score >= highlight_threshold:
if base_image is None:
base_image = np.array(Image.open(image_path).convert("RGB"))
highlighted = highlight_detection(base_image, binary_mask)
output_path = image_output_dir / f"{image_path.stem}_det{saved:02d}.png"
Image.fromarray(highlighted).save(output_path)
saved += 1
return detections, inference_time
def evaluate_model(
weight: str,
image_paths: Iterable[Path],
ground_truth: Dict[str, List[np.ndarray]],
args: argparse.Namespace,
) -> Dict[str, Any]:
model = YOLO(weight)
highlight_root = args.output_dir / Path(weight).stem
highlight_root.mkdir(parents=True, exist_ok=True)
detections: List[dict] = []
inference_times: List[float] = []
conf_thresholds = sorted(
set(
float(x)
for x in (args.map_conf_thresholds + [args.conf_threshold])
if 0.0 <= x <= 1.0
)
)
if not conf_thresholds:
raise ValueError("map_conf_thresholds must contain at least one value between 0 and 1.")
record_threshold = conf_thresholds[0]
for image_path in image_paths:
preds, infer_time = predict_image(
model,
image_path,
args.mask_threshold,
record_threshold,
args.conf_threshold,
highlight_root,
)
detections.extend(preds)
inference_times.append(infer_time)
map_by_conf = {}
for threshold in conf_thresholds:
filtered = [pred for pred in detections if pred["score"] >= threshold]
map_by_conf[threshold] = compute_average_precision(filtered, ground_truth)
model_map = map_by_conf[args.conf_threshold]
avg_infer = float(np.mean(inference_times)) if inference_times else 0.0
return {
"weight": weight,
"mAP50-95": model_map,
"avg_inference_time_sec": avg_infer,
"total_detections": len(detections),
"map_curve": map_by_conf,
"conf_thresholds": conf_thresholds,
}
def plot_map_vs_confidence(summary: List[dict], output_dir: Path) -> None:
if not summary:
return
output_path = output_dir / "map_vs_confidence.png"
plt.figure(figsize=(10, 6))
for row in summary:
thresholds = row["conf_thresholds"]
curve = [row["map_curve"].get(th, 0.0) for th in thresholds]
label = Path(row["weight"]).stem
plt.plot(thresholds, curve, marker="o", label=label)
plt.xlabel("Confidence threshold")
plt.ylabel("mAP50-95 (IOU 0.5:0.95)")
plt.title("mAP vs Confidence Threshold")
plt.grid(True, linestyle="--", alpha=0.4)
plt.legend()
plt.tight_layout()
plt.savefig(output_path)
print(f"Saved mAP vs confidence plot to {output_path}")
def main() -> None:
args = parse_args()
image_paths = sorted(
p for p in args.images_dir.iterdir() if p.suffix.lower() in {".jpg", ".jpeg", ".png"}
)
if not image_paths:
raise FileNotFoundError(f"No images found in {args.images_dir}")
ground_truth = load_ground_truth_masks(args.labels_dir, args.images_dir)
args.output_dir.mkdir(parents=True, exist_ok=True)
summary = []
for weight in args.weights:
if not weight:
continue
result = evaluate_model(weight, image_paths, ground_truth, args)
summary.append(result)
print(
f"{result['weight']}: mAP50-95={result['mAP50-95']:.3f}, "
f"avg inference={result['avg_inference_time_sec']:.3f}s, "
f"detections={result['total_detections']}"
)
if summary:
print("\n=== Summary ===")
for row in summary:
print(
f"{row['weight']:>15} | mAP50-95: {row['mAP50-95']:.3f} | "
f"avg inference: {row['avg_inference_time_sec']:.3f}s | "
f"detections: {row['total_detections']}"
)
plot_map_vs_confidence(summary, args.output_dir)
if __name__ == "__main__":
main()