eye-cv 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. eye/__init__.py +115 -0
  2. eye/__init___supervision_original.py +120 -0
  3. eye/annotators/__init__.py +0 -0
  4. eye/annotators/base.py +22 -0
  5. eye/annotators/core.py +2699 -0
  6. eye/annotators/line.py +107 -0
  7. eye/annotators/modern.py +529 -0
  8. eye/annotators/trace.py +142 -0
  9. eye/annotators/utils.py +177 -0
  10. eye/assets/__init__.py +2 -0
  11. eye/assets/downloader.py +95 -0
  12. eye/assets/list.py +83 -0
  13. eye/classification/__init__.py +0 -0
  14. eye/classification/core.py +188 -0
  15. eye/config.py +2 -0
  16. eye/core/__init__.py +0 -0
  17. eye/core/trackers/__init__.py +1 -0
  18. eye/core/trackers/botsort_tracker.py +336 -0
  19. eye/core/trackers/bytetrack_tracker.py +284 -0
  20. eye/core/trackers/sort_tracker.py +200 -0
  21. eye/core/tracking.py +146 -0
  22. eye/dataset/__init__.py +0 -0
  23. eye/dataset/core.py +919 -0
  24. eye/dataset/formats/__init__.py +0 -0
  25. eye/dataset/formats/coco.py +258 -0
  26. eye/dataset/formats/pascal_voc.py +279 -0
  27. eye/dataset/formats/yolo.py +272 -0
  28. eye/dataset/utils.py +259 -0
  29. eye/detection/__init__.py +0 -0
  30. eye/detection/auto_convert.py +155 -0
  31. eye/detection/core.py +1529 -0
  32. eye/detection/detections_enhanced.py +392 -0
  33. eye/detection/line_zone.py +859 -0
  34. eye/detection/lmm.py +184 -0
  35. eye/detection/overlap_filter.py +270 -0
  36. eye/detection/tools/__init__.py +0 -0
  37. eye/detection/tools/csv_sink.py +181 -0
  38. eye/detection/tools/inference_slicer.py +288 -0
  39. eye/detection/tools/json_sink.py +142 -0
  40. eye/detection/tools/polygon_zone.py +202 -0
  41. eye/detection/tools/smoother.py +123 -0
  42. eye/detection/tools/smoothing.py +179 -0
  43. eye/detection/tools/smoothing_config.py +202 -0
  44. eye/detection/tools/transformers.py +247 -0
  45. eye/detection/utils.py +1175 -0
  46. eye/draw/__init__.py +0 -0
  47. eye/draw/color.py +154 -0
  48. eye/draw/utils.py +374 -0
  49. eye/filters.py +112 -0
  50. eye/geometry/__init__.py +0 -0
  51. eye/geometry/core.py +128 -0
  52. eye/geometry/utils.py +47 -0
  53. eye/keypoint/__init__.py +0 -0
  54. eye/keypoint/annotators.py +442 -0
  55. eye/keypoint/core.py +687 -0
  56. eye/keypoint/skeletons.py +2647 -0
  57. eye/metrics/__init__.py +21 -0
  58. eye/metrics/core.py +72 -0
  59. eye/metrics/detection.py +843 -0
  60. eye/metrics/f1_score.py +648 -0
  61. eye/metrics/mean_average_precision.py +628 -0
  62. eye/metrics/mean_average_recall.py +697 -0
  63. eye/metrics/precision.py +653 -0
  64. eye/metrics/recall.py +652 -0
  65. eye/metrics/utils/__init__.py +0 -0
  66. eye/metrics/utils/object_size.py +158 -0
  67. eye/metrics/utils/utils.py +9 -0
  68. eye/py.typed +0 -0
  69. eye/quick.py +104 -0
  70. eye/tracker/__init__.py +0 -0
  71. eye/tracker/byte_tracker/__init__.py +0 -0
  72. eye/tracker/byte_tracker/core.py +386 -0
  73. eye/tracker/byte_tracker/kalman_filter.py +205 -0
  74. eye/tracker/byte_tracker/matching.py +69 -0
  75. eye/tracker/byte_tracker/single_object_track.py +178 -0
  76. eye/tracker/byte_tracker/utils.py +18 -0
  77. eye/utils/__init__.py +0 -0
  78. eye/utils/conversion.py +132 -0
  79. eye/utils/file.py +159 -0
  80. eye/utils/image.py +794 -0
  81. eye/utils/internal.py +200 -0
  82. eye/utils/iterables.py +84 -0
  83. eye/utils/notebook.py +114 -0
  84. eye/utils/video.py +307 -0
  85. eye/utils_eye/__init__.py +1 -0
  86. eye/utils_eye/geometry.py +71 -0
  87. eye/utils_eye/nms.py +55 -0
  88. eye/validators/__init__.py +140 -0
  89. eye/web.py +271 -0
  90. eye_cv-1.0.0.dist-info/METADATA +319 -0
  91. eye_cv-1.0.0.dist-info/RECORD +94 -0
  92. eye_cv-1.0.0.dist-info/WHEEL +5 -0
  93. eye_cv-1.0.0.dist-info/licenses/LICENSE +21 -0
  94. eye_cv-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,697 @@
1
+ from __future__ import annotations
2
+
3
+ from copy import deepcopy
4
+ from dataclasses import dataclass
5
+ from typing import TYPE_CHECKING, List, Optional, Tuple, Union
6
+
7
+ import numpy as np
8
+ from matplotlib import pyplot as plt
9
+
10
+ from eye.config import ORIENTED_BOX_COORDINATES
11
+ from eye.detection.core import Detections
12
+ from eye.detection.utils import (
13
+ box_iou_batch,
14
+ mask_iou_batch,
15
+ oriented_box_iou_batch,
16
+ )
17
+ from eye.draw.color import LEGACY_COLOR_PALETTE
18
+ from eye.metrics.core import Metric, MetricTarget
19
+ from eye.metrics.utils.object_size import (
20
+ ObjectSizeCategory,
21
+ get_detection_size_category,
22
+ )
23
+ from eye.metrics.utils.utils import ensure_pandas_installed
24
+
25
+ if TYPE_CHECKING:
26
+ import pandas as pd
27
+
28
+
29
+ class MeanAverageRecall(Metric):
30
+ """
31
+ Mean Average Recall (mAR) measures how well the model detects
32
+ and retrieves relevant objects by averaging recall over multiple
33
+ IoU thresholds, classes and detection limits.
34
+
35
+ Intuitively, while Recall measures the ability to find all relevant
36
+ objects, mAR narrows down how many detections are considered for each
37
+ class. For example, mAR @ 100 considers the top 100 highest confidence
38
+ detections for each class. mAR @ 1 considers only the highest
39
+ confidence detection for each class.
40
+
41
+ Example:
42
+ ```python
43
+ import eye as sv
44
+ from eye.metrics import MeanAverageRecall
45
+
46
+ predictions = sv.Detections(...)
47
+ targets = sv.Detections(...)
48
+
49
+ map_metric = MeanAverageRecall()
50
+ map_result = map_metric.update(predictions, targets).compute()
51
+
52
+ print(mar_results.mar_at_100)
53
+ # 0.5241
54
+
55
+ print(mar_results)
56
+ # MeanAverageRecallResult:
57
+ # Metric target: MetricTarget.BOXES
58
+ # mAR @ 1: 0.1362
59
+ # mAR @ 10: 0.4239
60
+ # mAR @ 100: 0.5241
61
+ # max detections: [1 10 100]
62
+ # IoU thresh: [0.5 0.55 0.6 ...]
63
+ # mAR per class:
64
+ # 0: [0.78571 0.78571 0.78571 ...]
65
+ # ...
66
+ # Small objects: ...
67
+ # Medium objects: ...
68
+ # Large objects: ...
69
+
70
+ mar_results.plot()
71
+ ```
72
+
73
+ ![example_plot](\
74
+ https://media.roboflow.com/eye-docs/metrics/mAR_plot_example.png\
75
+ ){ align=center width="800" }
76
+ """
77
+
78
+ def __init__(
79
+ self,
80
+ metric_target: MetricTarget = MetricTarget.BOXES,
81
+ ):
82
+ """
83
+ Initialize the Mean Average Recall metric.
84
+
85
+ Args:
86
+ metric_target (MetricTarget): The type of detection data to use.
87
+ """
88
+ self._metric_target = metric_target
89
+
90
+ self._predictions_list: List[Detections] = []
91
+ self._targets_list: List[Detections] = []
92
+
93
+ self.max_detections = np.array([1, 10, 100])
94
+
95
+ def reset(self) -> None:
96
+ """
97
+ Reset the metric to its initial state, clearing all stored data.
98
+ """
99
+ self._predictions_list = []
100
+ self._targets_list = []
101
+
102
+ def update(
103
+ self,
104
+ predictions: Union[Detections, List[Detections]],
105
+ targets: Union[Detections, List[Detections]],
106
+ ) -> MeanAverageRecall:
107
+ """
108
+ Add new predictions and targets to the metric, but do not compute the result.
109
+
110
+ Args:
111
+ predictions (Union[Detections, List[Detections]]): The predicted detections.
112
+ targets (Union[Detections, List[Detections]]): The target detections.
113
+
114
+ Returns:
115
+ (Recall): The updated metric instance.
116
+ """
117
+ if not isinstance(predictions, list):
118
+ predictions = [predictions]
119
+ if not isinstance(targets, list):
120
+ targets = [targets]
121
+
122
+ if len(predictions) != len(targets):
123
+ raise ValueError(
124
+ f"The number of predictions ({len(predictions)}) and"
125
+ f" targets ({len(targets)}) during the update must be the same."
126
+ )
127
+
128
+ self._predictions_list.extend(predictions)
129
+ self._targets_list.extend(targets)
130
+
131
+ return self
132
+
133
+ def compute(self) -> MeanAverageRecallResult:
134
+ """
135
+ Calculate the Mean Average Recall metric based on the stored predictions
136
+ and ground-truth, at different IoU thresholds and maximum detection counts.
137
+
138
+ Returns:
139
+ (MeanAverageRecallResult): The Mean Average Recall metric result.
140
+ """
141
+ result = self._compute(self._predictions_list, self._targets_list)
142
+
143
+ small_predictions, small_targets = self._filter_predictions_and_targets_by_size(
144
+ self._predictions_list, self._targets_list, ObjectSizeCategory.SMALL
145
+ )
146
+ result.small_objects = self._compute(small_predictions, small_targets)
147
+
148
+ medium_predictions, medium_targets = (
149
+ self._filter_predictions_and_targets_by_size(
150
+ self._predictions_list, self._targets_list, ObjectSizeCategory.MEDIUM
151
+ )
152
+ )
153
+ result.medium_objects = self._compute(medium_predictions, medium_targets)
154
+
155
+ large_predictions, large_targets = self._filter_predictions_and_targets_by_size(
156
+ self._predictions_list, self._targets_list, ObjectSizeCategory.LARGE
157
+ )
158
+ result.large_objects = self._compute(large_predictions, large_targets)
159
+
160
+ return result
161
+
162
+ def _compute(
163
+ self, predictions_list: List[Detections], targets_list: List[Detections]
164
+ ) -> MeanAverageRecallResult:
165
+ iou_thresholds = np.linspace(0.5, 0.95, 10)
166
+ stats = []
167
+
168
+ for predictions, targets in zip(predictions_list, targets_list):
169
+ prediction_contents = self._detections_content(predictions)
170
+ target_contents = self._detections_content(targets)
171
+
172
+ if len(targets) > 0:
173
+ if len(predictions) == 0:
174
+ stats.append(
175
+ (
176
+ np.zeros((0, iou_thresholds.size), dtype=bool),
177
+ np.zeros((0,), dtype=np.float32),
178
+ np.zeros((0,), dtype=int),
179
+ targets.class_id,
180
+ )
181
+ )
182
+
183
+ else:
184
+ if self._metric_target == MetricTarget.BOXES:
185
+ iou = box_iou_batch(target_contents, prediction_contents)
186
+ elif self._metric_target == MetricTarget.MASKS:
187
+ iou = mask_iou_batch(target_contents, prediction_contents)
188
+ elif self._metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
189
+ iou = oriented_box_iou_batch(
190
+ target_contents, prediction_contents
191
+ )
192
+ else:
193
+ raise ValueError(
194
+ "Unsupported metric target for IoU calculation"
195
+ )
196
+
197
+ matches = self._match_detection_batch(
198
+ predictions.class_id, targets.class_id, iou, iou_thresholds
199
+ )
200
+ stats.append(
201
+ (
202
+ matches,
203
+ predictions.confidence,
204
+ predictions.class_id,
205
+ targets.class_id,
206
+ )
207
+ )
208
+
209
+ if not stats:
210
+ return MeanAverageRecallResult(
211
+ metric_target=self._metric_target,
212
+ recall_scores=np.zeros(iou_thresholds.shape[0]),
213
+ recall_per_class=np.zeros((0, iou_thresholds.shape[0])),
214
+ max_detections=self.max_detections,
215
+ iou_thresholds=iou_thresholds,
216
+ matched_classes=np.array([], dtype=int),
217
+ small_objects=None,
218
+ medium_objects=None,
219
+ large_objects=None,
220
+ )
221
+
222
+ concatenated_stats = [np.concatenate(items, 0) for items in zip(*stats)]
223
+ recall_scores_per_k, recall_per_class, unique_classes = (
224
+ self._compute_average_recall_for_classes(*concatenated_stats)
225
+ )
226
+
227
+ return MeanAverageRecallResult(
228
+ metric_target=self._metric_target,
229
+ recall_scores=recall_scores_per_k,
230
+ recall_per_class=recall_per_class,
231
+ max_detections=self.max_detections,
232
+ iou_thresholds=iou_thresholds,
233
+ matched_classes=unique_classes,
234
+ small_objects=None,
235
+ medium_objects=None,
236
+ large_objects=None,
237
+ )
238
+
239
+ def _compute_average_recall_for_classes(
240
+ self,
241
+ matches: np.ndarray,
242
+ prediction_confidence: np.ndarray,
243
+ prediction_class_ids: np.ndarray,
244
+ true_class_ids: np.ndarray,
245
+ ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
246
+ sorted_indices = np.argsort(-prediction_confidence)
247
+ matches = matches[sorted_indices]
248
+ prediction_class_ids = prediction_class_ids[sorted_indices]
249
+ unique_classes, class_counts = np.unique(true_class_ids, return_counts=True)
250
+
251
+ recalls_at_k = []
252
+ for max_detections in self.max_detections:
253
+ # Shape: PxTh,P,C,C -> CxThx3
254
+ confusion_matrix = self._compute_confusion_matrix(
255
+ matches,
256
+ prediction_class_ids,
257
+ unique_classes,
258
+ class_counts,
259
+ max_detections=max_detections,
260
+ )
261
+
262
+ # Shape: CxThx3 -> CxTh
263
+ recall_per_class = self._compute_recall(confusion_matrix)
264
+ recalls_at_k.append(recall_per_class)
265
+
266
+ # Shape: KxCxTh -> KxC
267
+ recalls_at_k = np.array(recalls_at_k)
268
+ average_recall_per_class = np.mean(recalls_at_k, axis=2)
269
+
270
+ # Shape: KxC -> K
271
+ recall_scores = np.mean(average_recall_per_class, axis=1)
272
+
273
+ return recall_scores, recall_per_class, unique_classes
274
+
275
+ @staticmethod
276
+ def _match_detection_batch(
277
+ predictions_classes: np.ndarray,
278
+ target_classes: np.ndarray,
279
+ iou: np.ndarray,
280
+ iou_thresholds: np.ndarray,
281
+ ) -> np.ndarray:
282
+ num_predictions, num_iou_levels = (
283
+ predictions_classes.shape[0],
284
+ iou_thresholds.shape[0],
285
+ )
286
+ correct = np.zeros((num_predictions, num_iou_levels), dtype=bool)
287
+ correct_class = target_classes[:, None] == predictions_classes
288
+
289
+ for i, iou_level in enumerate(iou_thresholds):
290
+ matched_indices = np.where((iou >= iou_level) & correct_class)
291
+
292
+ if matched_indices[0].shape[0]:
293
+ combined_indices = np.stack(matched_indices, axis=1)
294
+ iou_values = iou[matched_indices][:, None]
295
+ matches = np.hstack([combined_indices, iou_values])
296
+
297
+ if matched_indices[0].shape[0] > 1:
298
+ matches = matches[matches[:, 2].argsort()[::-1]]
299
+ matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
300
+ matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
301
+
302
+ correct[matches[:, 1].astype(int), i] = True
303
+
304
+ return correct
305
+
306
+ @staticmethod
307
+ def _compute_confusion_matrix(
308
+ sorted_matches: np.ndarray,
309
+ sorted_prediction_class_ids: np.ndarray,
310
+ unique_classes: np.ndarray,
311
+ class_counts: np.ndarray,
312
+ max_detections: Optional[int] = None,
313
+ ) -> np.ndarray:
314
+ """
315
+ Compute the confusion matrix for each class and IoU threshold.
316
+
317
+ Assumes the matches and prediction_class_ids are sorted by confidence
318
+ in descending order.
319
+
320
+ Args:
321
+ sorted_matches: np.ndarray, bool, shape (P, Th), that is True
322
+ if the prediction is a true positive at the given IoU threshold.
323
+ sorted_prediction_class_ids: np.ndarray, int, shape (P,), containing
324
+ the class id for each prediction.
325
+ unique_classes: np.ndarray, int, shape (C,), containing the unique
326
+ class ids.
327
+ class_counts: np.ndarray, int, shape (C,), containing the number
328
+ of true instances for each class.
329
+ max_detections: Optional[int], the maximum number of detections to
330
+ consider for each class. Extra detections are considered false
331
+ positives. By default, all detections are considered.
332
+
333
+ Returns:
334
+ np.ndarray, shape (C, Th, 3), containing the true positives, false
335
+ positives, and false negatives for each class and IoU threshold.
336
+ """
337
+ num_thresholds = sorted_matches.shape[1]
338
+ num_classes = unique_classes.shape[0]
339
+
340
+ confusion_matrix = np.zeros((num_classes, num_thresholds, 3))
341
+ for class_idx, class_id in enumerate(unique_classes):
342
+ is_class = sorted_prediction_class_ids == class_id
343
+ num_true = class_counts[class_idx]
344
+ num_predictions = is_class.sum()
345
+
346
+ if num_predictions == 0:
347
+ true_positives = np.zeros(num_thresholds)
348
+ false_positives = np.zeros(num_thresholds)
349
+ false_negatives = np.full(num_thresholds, num_true)
350
+ elif num_true == 0:
351
+ true_positives = np.zeros(num_thresholds)
352
+ false_positives = np.full(num_thresholds, num_predictions)
353
+ false_negatives = np.zeros(num_thresholds)
354
+ else:
355
+ limited_matches = sorted_matches[is_class][slice(max_detections)]
356
+ true_positives = limited_matches.sum(0)
357
+
358
+ false_positives = (1 - limited_matches).sum(0)
359
+ false_negatives = num_true - true_positives
360
+ false_negatives = num_true - true_positives
361
+ confusion_matrix[class_idx] = np.stack(
362
+ [true_positives, false_positives, false_negatives], axis=1
363
+ )
364
+
365
+ return confusion_matrix
366
+
367
+ @staticmethod
368
+ def _compute_recall(confusion_matrix: np.ndarray) -> np.ndarray:
369
+ """
370
+ Broadcastable function, computing the recall from the confusion matrix.
371
+
372
+ Arguments:
373
+ confusion_matrix: np.ndarray, shape (N, ..., 3), where the last dimension
374
+ contains the true positives, false positives, and false negatives.
375
+
376
+ Returns:
377
+ np.ndarray, shape (N, ...), containing the recall for each element.
378
+ """
379
+ if not confusion_matrix.shape[-1] == 3:
380
+ raise ValueError(
381
+ f"Confusion matrix must have shape (..., 3), got "
382
+ f"{confusion_matrix.shape}"
383
+ )
384
+ true_positives = confusion_matrix[..., 0]
385
+ false_negatives = confusion_matrix[..., 2]
386
+
387
+ denominator = true_positives + false_negatives
388
+ recall = np.where(denominator == 0, 0, true_positives / denominator)
389
+
390
+ return recall
391
+
392
+ def _detections_content(self, detections: Detections) -> np.ndarray:
393
+ """Return boxes, masks or oriented bounding boxes from detections."""
394
+ if self._metric_target == MetricTarget.BOXES:
395
+ return detections.xyxy
396
+ if self._metric_target == MetricTarget.MASKS:
397
+ return (
398
+ detections.mask
399
+ if detections.mask is not None
400
+ else self._make_empty_content()
401
+ )
402
+ if self._metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
403
+ obb = detections.data.get(ORIENTED_BOX_COORDINATES)
404
+ if obb is not None and len(obb) > 0:
405
+ return np.array(obb, dtype=np.float32)
406
+ return self._make_empty_content()
407
+ raise ValueError(f"Invalid metric target: {self._metric_target}")
408
+
409
+ def _make_empty_content(self) -> np.ndarray:
410
+ if self._metric_target == MetricTarget.BOXES:
411
+ return np.empty((0, 4), dtype=np.float32)
412
+ if self._metric_target == MetricTarget.MASKS:
413
+ return np.empty((0, 0, 0), dtype=bool)
414
+ if self._metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
415
+ return np.empty((0, 4, 2), dtype=np.float32)
416
+ raise ValueError(f"Invalid metric target: {self._metric_target}")
417
+
418
+ def _filter_detections_by_size(
419
+ self, detections: Detections, size_category: ObjectSizeCategory
420
+ ) -> Detections:
421
+ """Return a copy of detections with contents filtered by object size."""
422
+ new_detections = deepcopy(detections)
423
+ if detections.is_empty() or size_category == ObjectSizeCategory.ANY:
424
+ return new_detections
425
+
426
+ sizes = get_detection_size_category(new_detections, self._metric_target)
427
+ size_mask = sizes == size_category.value
428
+
429
+ new_detections.xyxy = new_detections.xyxy[size_mask]
430
+ if new_detections.mask is not None:
431
+ new_detections.mask = new_detections.mask[size_mask]
432
+ if new_detections.class_id is not None:
433
+ new_detections.class_id = new_detections.class_id[size_mask]
434
+ if new_detections.confidence is not None:
435
+ new_detections.confidence = new_detections.confidence[size_mask]
436
+ if new_detections.tracker_id is not None:
437
+ new_detections.tracker_id = new_detections.tracker_id[size_mask]
438
+ if new_detections.data is not None:
439
+ for key, value in new_detections.data.items():
440
+ new_detections.data[key] = np.array(value)[size_mask]
441
+
442
+ return new_detections
443
+
444
+ def _filter_predictions_and_targets_by_size(
445
+ self,
446
+ predictions_list: List[Detections],
447
+ targets_list: List[Detections],
448
+ size_category: ObjectSizeCategory,
449
+ ) -> Tuple[List[Detections], List[Detections]]:
450
+ new_predictions_list = []
451
+ new_targets_list = []
452
+ for predictions, targets in zip(predictions_list, targets_list):
453
+ new_predictions_list.append(
454
+ self._filter_detections_by_size(predictions, size_category)
455
+ )
456
+ new_targets_list.append(
457
+ self._filter_detections_by_size(targets, size_category)
458
+ )
459
+ return new_predictions_list, new_targets_list
460
+
461
+
462
+ @dataclass
463
+ class MeanAverageRecallResult:
464
+ # """
465
+ # The results of the recall metric calculation.
466
+
467
+ # Defaults to `0` if no detections or targets were provided.
468
+
469
+ # Attributes:
470
+ # metric_target (MetricTarget): the type of data used for the metric -
471
+ # boxes, masks or oriented bounding boxes.
472
+ # averaging_method (AveragingMethod): the averaging method used to compute the
473
+ # recall. Determines how the recall is aggregated across classes.
474
+ # recall_at_50 (float): the recall at IoU threshold of `0.5`.
475
+ # recall_at_75 (float): the recall at IoU threshold of `0.75`.
476
+ # recall_scores (np.ndarray): the recall scores at each IoU threshold.
477
+ # Shape: `(num_iou_thresholds,)`
478
+ # recall_per_class (np.ndarray): the recall scores per class and IoU threshold.
479
+ # Shape: `(num_target_classes, num_iou_thresholds)`
480
+ # iou_thresholds (np.ndarray): the IoU thresholds used in the calculations.
481
+ # matched_classes (np.ndarray): the class IDs of all matched classes.
482
+ # Corresponds to the rows of `recall_per_class`.
483
+ # small_objects (Optional[RecallResult]): the Recall metric results
484
+ # for small objects.
485
+ # medium_objects (Optional[RecallResult]): the Recall metric results
486
+ # for medium objects.
487
+ # large_objects (Optional[RecallResult]): the Recall metric results
488
+ # for large objects.
489
+ # """
490
+ """
491
+ The results of the Mean Average Recall metric calculation.
492
+
493
+ Defaults to `0` if no detections or targets were provided.
494
+
495
+ Attributes:
496
+ metric_target (MetricTarget): the type of data used for the metric -
497
+ boxes, masks or oriented bounding boxes.
498
+ mAR_at_1 (float): the Mean Average Recall, when considering only the top
499
+ highest confidence detection for each class.
500
+ mAR_at_10 (float): the Mean Average Recall, when considering top 10
501
+ highest confidence detections for each class.
502
+ mAR_at_100 (float): the Mean Average Recall, when considering top 100
503
+ highest confidence detections for each class.
504
+ recall_per_class (np.ndarray): the recall scores per class and IoU threshold.
505
+ Shape: `(num_target_classes, num_iou_thresholds)`
506
+ max_detections (np.ndarray): the array with maximum number of detections
507
+ considered.
508
+ iou_thresholds (np.ndarray): the IoU thresholds used in the calculations.
509
+ matched_classes (np.ndarray): the class IDs of all matched classes.
510
+ Corresponds to the rows of `recall_per_class`.
511
+ small_objects (Optional[MeanAverageRecallResult]): the Mean Average Recall
512
+ metric results for small objects (area < 32²).
513
+ medium_objects (Optional[MeanAverageRecallResult]): the Mean Average Recall
514
+ metric results for medium objects (32² ≤ area < 96²).
515
+ large_objects (Optional[MeanAverageRecallResult]): the Mean Average Recall
516
+ metric results for large objects (area ≥ 96²).
517
+ """
518
+
519
+ metric_target: MetricTarget
520
+
521
+ @property
522
+ def mAR_at_1(self) -> float:
523
+ return self.recall_scores[0]
524
+
525
+ @property
526
+ def mAR_at_10(self) -> float:
527
+ return self.recall_scores[1]
528
+
529
+ @property
530
+ def mAR_at_100(self) -> float:
531
+ return self.recall_scores[2]
532
+
533
+ recall_scores: np.ndarray
534
+ recall_per_class: np.ndarray
535
+ max_detections: np.ndarray
536
+ iou_thresholds: np.ndarray
537
+ matched_classes: np.ndarray
538
+
539
+ small_objects: Optional[MeanAverageRecallResult]
540
+ medium_objects: Optional[MeanAverageRecallResult]
541
+ large_objects: Optional[MeanAverageRecallResult]
542
+
543
+ def __str__(self) -> str:
544
+ """
545
+ Format as a pretty string.
546
+
547
+ Example:
548
+ ```python
549
+ # MeanAverageRecallResult:
550
+ # Metric target: MetricTarget.BOXES
551
+ # mAR @ 1: 0.1362
552
+ # mAR @ 10: 0.4239
553
+ # mAR @ 100: 0.5241
554
+ # max detections: [1 10 100]
555
+ # IoU thresh: [0.5 0.55 0.6 ...]
556
+ # mAR per class:
557
+ # 0: [0.78571 0.78571 0.78571 ...]
558
+ # ...
559
+ # Small objects: ...
560
+ # Medium objects: ...
561
+ # Large objects: ...
562
+ ```
563
+ """
564
+ out_str = (
565
+ f"{self.__class__.__name__}:\n"
566
+ f"Metric target: {self.metric_target}\n"
567
+ f"mAR @ 1: {self.mAR_at_1:.4f}\n"
568
+ f"mAR @ 10: {self.mAR_at_10:.4f}\n"
569
+ f"mAR @ 100: {self.mAR_at_100:.4f}\n"
570
+ f"max detections: {self.max_detections}\n"
571
+ f"IoU thresh: {self.iou_thresholds}\n"
572
+ f"mAR per class:\n"
573
+ )
574
+ if self.recall_per_class.size == 0:
575
+ out_str += " No results\n"
576
+ for class_id, recall_of_class in zip(
577
+ self.matched_classes, self.recall_per_class
578
+ ):
579
+ out_str += f" {class_id}: {recall_of_class}\n"
580
+
581
+ indent = " "
582
+ if self.small_objects is not None:
583
+ indented = indent + str(self.small_objects).replace("\n", f"\n{indent}")
584
+ out_str += f"\nSmall objects:\n{indented}"
585
+ if self.medium_objects is not None:
586
+ indented = indent + str(self.medium_objects).replace("\n", f"\n{indent}")
587
+ out_str += f"\nMedium objects:\n{indented}"
588
+ if self.large_objects is not None:
589
+ indented = indent + str(self.large_objects).replace("\n", f"\n{indent}")
590
+ out_str += f"\nLarge objects:\n{indented}"
591
+
592
+ return out_str
593
+
594
+ def to_pandas(self) -> "pd.DataFrame":
595
+ """
596
+ Convert the result to a pandas DataFrame.
597
+
598
+ Returns:
599
+ (pd.DataFrame): The result as a DataFrame.
600
+ """
601
+ ensure_pandas_installed()
602
+ import pandas as pd
603
+
604
+ pandas_data = {
605
+ "mAR @ 1": self.mAR_at_1,
606
+ "mAR @ 10": self.mAR_at_10,
607
+ "mAR @ 100": self.mAR_at_100,
608
+ }
609
+
610
+ if self.small_objects is not None:
611
+ small_objects_df = self.small_objects.to_pandas()
612
+ for key, value in small_objects_df.items():
613
+ pandas_data[f"small_objects_{key}"] = value
614
+ if self.medium_objects is not None:
615
+ medium_objects_df = self.medium_objects.to_pandas()
616
+ for key, value in medium_objects_df.items():
617
+ pandas_data[f"medium_objects_{key}"] = value
618
+ if self.large_objects is not None:
619
+ large_objects_df = self.large_objects.to_pandas()
620
+ for key, value in large_objects_df.items():
621
+ pandas_data[f"large_objects_{key}"] = value
622
+
623
+ return pd.DataFrame(pandas_data, index=[0])
624
+
625
+ def plot(self):
626
+ """
627
+ Plot the Mean Average Recall results.
628
+
629
+ ![example_plot](\
630
+ https://media.roboflow.com/eye-docs/metrics/mAR_plot_example.png\
631
+ ){ align=center width="800" }
632
+ """
633
+ labels = ["mAR @ 1", "mAR @ 10", "mAR @ 100"]
634
+ values = [self.mAR_at_1, self.mAR_at_10, self.mAR_at_100]
635
+ colors = [LEGACY_COLOR_PALETTE[0]] * 3
636
+
637
+ if self.small_objects is not None:
638
+ small_objects = self.small_objects
639
+ labels += ["Small: mAR @ 1", "Small: mAR @ 10", "Small: mAR @ 100"]
640
+ values += [
641
+ small_objects.mAR_at_1,
642
+ small_objects.mAR_at_10,
643
+ small_objects.mAR_at_100,
644
+ ]
645
+ colors += [LEGACY_COLOR_PALETTE[3]] * 3
646
+
647
+ if self.medium_objects is not None:
648
+ medium_objects = self.medium_objects
649
+ labels += ["Medium: mAR @ 1", "Medium: mAR @ 10", "Medium: mAR @ 100"]
650
+ values += [
651
+ medium_objects.mAR_at_1,
652
+ medium_objects.mAR_at_10,
653
+ medium_objects.mAR_at_100,
654
+ ]
655
+ colors += [LEGACY_COLOR_PALETTE[2]] * 3
656
+
657
+ if self.large_objects is not None:
658
+ large_objects = self.large_objects
659
+ labels += ["Large: mAR @ 1", "Large: mAR @ 10", "Large: mAR @ 100"]
660
+ values += [
661
+ large_objects.mAR_at_1,
662
+ large_objects.mAR_at_10,
663
+ large_objects.mAR_at_100,
664
+ ]
665
+ colors += [LEGACY_COLOR_PALETTE[4]] * 3
666
+
667
+ plt.rcParams["font.family"] = "monospace"
668
+
669
+ _, ax = plt.subplots(figsize=(10, 6))
670
+ ax.set_ylim(0, 1)
671
+ ax.set_ylabel("Value", fontweight="bold")
672
+ title = (
673
+ f"Mean Average Recall, by Object Size"
674
+ f"\n(target: {self.metric_target.value})"
675
+ )
676
+ ax.set_title(title, fontweight="bold")
677
+
678
+ x_positions = range(len(labels))
679
+ bars = ax.bar(x_positions, values, color=colors, align="center")
680
+
681
+ ax.set_xticks(x_positions)
682
+ ax.set_xticklabels(labels, rotation=45, ha="right")
683
+
684
+ for bar in bars:
685
+ y_value = bar.get_height()
686
+ ax.text(
687
+ bar.get_x() + bar.get_width() / 2,
688
+ y_value + 0.02,
689
+ f"{y_value:.2f}",
690
+ ha="center",
691
+ va="bottom",
692
+ )
693
+
694
+ plt.rcParams["font.family"] = "sans-serif"
695
+
696
+ plt.tight_layout()
697
+ plt.show()