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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
- import math
- import unittest
- import torch
- import random
- from typing import List, Optional, Tuple, Dict
- from super_gradients.training.metrics.detection_metrics import DetectionMetricsDistanceBased
- from super_gradients.training.utils.detection_utils import EuclideanDistance, ManhattanDistance
- class TestDetectionMetricsDistanceBased(unittest.TestCase):
- def setUp(self):
- # Set random seeds for reproducibility
- torch.manual_seed(42)
- random.seed(42)
- self.num_classes = 3
- self.predefined_correct_class = 1 # random.randint(0, self.num_classes - 1)
- self.using_predefined_class = True
- self.verbose = True
- self.distance_thresholds = [5.0]
- self.score_thres = 0.1
- self.img_width = 640
- self.img_height = 480
- # Mock input image tensor (1 batch, 1 image, 640x480 size)
- self.img_tensor = (torch.randint(0, 256, (1, 1, self.img_height, self.img_width), dtype=torch.uint8)).int()
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=self.distance_thresholds,
- score_thres=self.score_thres,
- distance_metric=EuclideanDistance(),
- )
- def mock_post_prediction_callback(self, batch_preds: List[torch.Tensor], device="cpu") -> List[torch.Tensor]:
- batch_transformed_preds = []
- for preds in batch_preds: # Iterate over each image's raw predictions in the batch
- transformed_preds = []
- for i in range(preds.size(0)): # Iterate over each prediction for the current image
- # Get the bounding box coordinates (cx, cy, w, h) from raw preds
- cx, cy, w, h = preds[i]
- # Generate a random confidence score (for testing)
- confidence = random.uniform(0.1, 0.9)
- # Generate a random class label (for testing)
- if self.using_predefined_class:
- class_label = self.predefined_correct_class
- else:
- class_label = random.randint(0, self.num_classes - 1)
- # Calculate absolute coordinates (x1, y1, x2, y2)
- x1 = cx - w / 2.0
- y1 = cy - h / 2.0
- x2 = cx + w / 2.0
- y2 = cy + h / 2.0
- # Store the transformed prediction in a tensor
- transformed_pred = torch.tensor([x1, y1, x2, y2, confidence, class_label], device=device)
- # Append the tensor to the list
- transformed_preds.append(transformed_pred)
- # Convert list of tensors to a single tensor for this image
- transformed_preds = torch.stack(transformed_preds)
- # Add this image's transformed predictions to the batch list
- batch_transformed_preds.append(transformed_preds)
- return batch_transformed_preds
- def validate_results(self, results: Dict, precision, recall, mAP, F1, places=4, verbose=False, description=None):
- if verbose and description:
- test_name = self.id().split(".")[-1]
- print(f"\n{test_name}():")
- print(f"Description: {description}")
- results = dict((k.split("@")[0].lower().replace("distance_based_", ""), v) for k, v in results.items())
- self.assertAlmostEqual(results["precision"].item(), precision, places=places)
- self.assertAlmostEqual(results["recall"].item(), recall, places=places)
- if mAP is not None:
- self.assertAlmostEqual(results["map"].item(), mAP, places=places)
- self.assertAlmostEqual(results["f1"].item(), F1, places=places)
- def generate_targets(self, img_width, img_height, num_classes, num_targets):
- targets = []
- for index in range(num_targets):
- # Generate random coordinates and dimensions for the target
- cx, cy = random.randint(0, img_width - 1), random.randint(0, img_height - 1)
- max_w = min(cx, img_width - cx) * 2
- max_h = min(cy, img_height - cy) * 2
- w = random.randint(1, max_w)
- h = random.randint(1, max_h)
- # Pick label
- if self.using_predefined_class:
- label = self.predefined_correct_class
- else:
- label = random.randint(0, num_classes - 1)
- # Normalize target coordinates and dimensions to [0, 1]
- target_x1, target_y1, target_x2, target_y2 = self.normalize_coordinates(cx, cy, w, h, img_width, img_height)
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Append target data in LABEL_CXCYWH format
- targets.append([index, label, target_cx, target_cy, target_w, target_h])
- targets = torch.tensor(targets, dtype=torch.float32).reshape(num_targets, 6)
- return targets
- @staticmethod
- def generate_predictions(distance_thresholds, img_height, img_width, num_correct_preds, num_targets, num_total_predictions, targets):
- predictions = []
- for _ in range(num_correct_preds):
- # Generate predictions close to some targets
- target_idx = random.randint(0, num_targets - 1)
- _, _, x_center, y_center, _, _ = targets[target_idx]
- dist_idx = random.randint(0, len(distance_thresholds) - 1)
- distance = random.randint(0, distance_thresholds[dist_idx])
- angle = random.uniform(0, 2 * math.pi)
- x_center_scalar = int(x_center.item() * img_width) # As denormalized
- y_center_scalar = int(y_center.item() * img_height) # As denormalized
- pred_x = int(round(x_center_scalar + distance * math.cos(angle)))
- pred_y = int(round(y_center_scalar + distance * math.sin(angle)))
- max_w = min(pred_x, img_width - pred_x) * 2
- max_h = min(pred_y, img_height - pred_y) * 2
- w = random.randint(1, max_w)
- h = random.randint(1, max_h)
- # Append prediction data in CXCYWH format
- prediction = torch.tensor([pred_x, pred_y, w, h])
- predictions.append(prediction)
- for _ in range(num_total_predictions - num_correct_preds):
- # Generate predictions far from any target
- pred_x, pred_y = random.randint(0, img_width - 1), random.randint(0, img_height - 1)
- max_w = min(pred_x, img_width - pred_x) * 2
- max_h = min(pred_y, img_height - pred_y) * 2
- w = random.randint(1, max_w)
- h = random.randint(1, max_h)
- # Append prediction data in CXCYWH format
- prediction = torch.tensor([pred_x, pred_y, w, h])
- predictions.append(prediction)
- # Convert the list of tensors into a single tensor and reshape it
- predictions = torch.stack(predictions)
- return [predictions]
- @staticmethod
- def normalize_coordinates(cx: int, cy: int, w: int, h: int, img_width: int, img_height: int) -> Tuple[float, float, float, float]:
- """
- Normalize coordinates and dimensions to [0, 1] range.
- Args:
- cx (int): Center x-coordinate.
- cy (int): Center y-coordinate.
- w (int): Width.
- h (int): Height.
- img_width (int): Width of the image.
- img_height (int): Height of the image.
- Returns:
- Tuple[float, float, float, float]: Normalized coordinates and dimensions (x1, y1, x2, y2).
- """
- x1 = max(0, (cx - w / 2) / img_width)
- y1 = max(0, (cy - h / 2) / img_height)
- x2 = min(1, (cx + w / 2) / img_width)
- y2 = min(1, (cy + h / 2) / img_height)
- return x1, y1, x2, y2
- @staticmethod
- def generate_mock_data(
- self,
- img_width: int,
- img_height: int,
- num_classes: int,
- num_targets: int,
- distance_thresholds: List[float],
- target_precision: float,
- target_recall: float,
- crowd_targets: bool = False,
- ) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:
- """
- Generate mock data for testing object detection metrics.
- Args:
- img_width (int): Width of the image.
- img_height (int): Height of the image.
- num_classes (int): Number of classes.
- num_targets (int): Number of mock target objects.
- distance_thresholds: (List[float]): List of distance thresholds.
- target_precision (float): Desired precision value (between 0 and 1).
- target_recall (float): Desired recall value (between 0 and 1).
- crowd_targets (bool, optional): Whether to create crowded targets. Default is False.
- Returns:
- Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]:
- - Mock targets with shape (num_targets, 6) in LABEL_CXCYWH format.
- - Mock predictions with shape (num_total_predictions, 4) in CXCYWH format.
- - Crowd targets with shape (num_crowd_targets, 6) in LABEL_CXCYWH format, or None if crowd_targets is False.
- """
- # Generate targets
- targets = self.generate_targets(img_width, img_height, num_classes, num_targets)
- # Calculate TP, FP, and FN
- TP = num_targets
- FP = int((TP / target_precision) - TP)
- # Calculate the total number of predictions you'll need to generate
- num_total_predictions = TP + FP # Because TP + FP = total predictions
- num_correct_preds = math.ceil(num_total_predictions * target_precision)
- # Generate predictions accordingly to scenario
- predictions = self.generate_predictions(distance_thresholds, img_height, img_width, num_correct_preds, num_targets, num_total_predictions, targets)
- crowd_targets_data = None
- if crowd_targets:
- # Create crowded targets (similar to targets)
- crowd_targets_data = self.generate_targets(img_width, img_height, num_classes, num_targets)
- return targets, predictions, crowd_targets_data
- @staticmethod
- def calculate_expected_metrics(self, precision, recall):
- # Calculate expected mAP (simplified in this context)
- expected_mAP = precision
- # Calculate expected F1-score
- if precision + recall == 0:
- expected_f1 = 0.0
- else:
- expected_f1 = (2 * precision * recall) / (precision + recall)
- return expected_mAP, expected_f1
- # Test Scenario: Single target in the image, single match out of three total predictions.
- # Desired precision: 0.33 (1 out of 3 predictions should match).
- # Desired recall: 0.33 (1 out of 3 targets should be detected).
- # Total predictions: 3 (to meet the precision and recall requirements).
- # Crowd targets: None
- def test_random_case_generation_and_verification(self):
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Test configuration
- num_targets = 1 # Number of mock target objects
- target_precision = 0.3333
- target_recall = 1
- crowd_targets = False # Set to True to generate crowd targets
- # Generate mock data
- targets, predictions, crowd_targets_data = self.generate_mock_data(
- self, self.img_width, self.img_height, self.num_classes, num_targets, self.distance_thresholds, target_precision, target_recall, crowd_targets
- )
- # Call the update and compute methods with generated data
- self.metric.update(preds=predictions, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=None)
- results = self.metric.compute()
- # Calculate expected mAP and F1-score
- expected_mAP, expected_f1 = self.calculate_expected_metrics(self, target_precision, target_recall)
- # Validate the results
- self.validate_results(results, precision=target_precision, recall=target_recall, mAP=None, F1=expected_f1)
- # checks whether a single prediction that matches a single target will yield a perfect score
- # (Precision, Recall, F1 score, and mAP all set to 1.0). Using Manhattan distance.
- def test_distance_based_score_l1_norm_distance_single_target_single_prediction_match(self):
- scenario = "a single prediction that matches a single target using L1 Norm as a metric"
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is 5px away from the center of the target
- raw_preds = [torch.tensor([[15, 15, 10, 10]])] # Close to target
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 10, 10, 20, 20
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=[5.0],
- score_thres=self.score_thres,
- distance_metric=ManhattanDistance(),
- )
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=None)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=1.0, recall=1.0, mAP=1.0, F1=1.0, verbose=self.verbose, description=scenario)
- # checks whether a single prediction that matches a single crowd target will yield a perfect score
- # (Precision, Recall, F1 score, and mAP all set to 1.0). Using Manhattan distance.
- def test_distance_based_score_euclidean_distance_single_crowd_target_single_prediction_target_miss(self):
- scenario = "A single prediction, single target and a single crowd target. Prediction close to crowd target and far from target."
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is 5px away from the center of the crowd target
- raw_preds = [torch.tensor([[15, 15, 10, 10]])] # Close to crowd target
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 100, 100, 200, 200
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- # Define crowd target (unnormalized) coordinates within image dimensions
- crowd_target_x1, crowd_target_y1, crowd_target_x2, crowd_target_y2 = 10, 10, 20, 20
- # Normalize crowd target coordinates
- crowd_target_x1, crowd_target_x2 = crowd_target_x1 / self.img_width, crowd_target_x2 / self.img_width
- crowd_target_y1, crowd_target_y2 = crowd_target_y1 / self.img_height, crowd_target_y2 / self.img_height
- # Calculate normalized width and height for crowd target
- crowd_target_w = crowd_target_x2 - crowd_target_x1
- crowd_target_h = crowd_target_y2 - crowd_target_y1
- # Calculate normalized center coordinates for crowd target
- crowd_target_cx = crowd_target_x1 + crowd_target_w / 2
- crowd_target_cy = crowd_target_y1 + crowd_target_h / 2
- # Mock crowd targets
- # Create a single crowd target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- crowd_targets = torch.tensor(
- [[0, self.predefined_correct_class, crowd_target_cx, crowd_target_cy, crowd_target_w, crowd_target_h]], dtype=torch.float32
- ).reshape(1, 6)
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=[5.0],
- score_thres=self.score_thres,
- distance_metric=ManhattanDistance(),
- )
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=crowd_targets)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=0, recall=0, mAP=0, F1=0, verbose=self.verbose, description=scenario)
- def test_distance_based_score_euclidean_distance_single_crowd_target_single_prediction_target_match(self):
- scenario = "A single prediction, single target and a single crowd target. Prediction match target and far from the crowd target."
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is 5px away from the center of the crowd target
- raw_preds = [torch.tensor([[15, 15, 10, 10]])] # Close to crowd target
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 10, 10, 20, 20
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- # Define crowd target (unnormalized) coordinates within image dimensions
- crowd_target_x1, crowd_target_y1, crowd_target_x2, crowd_target_y2 = 100, 100, 200, 200
- # Normalize crowd target coordinates
- crowd_target_x1, crowd_target_x2 = crowd_target_x1 / self.img_width, crowd_target_x2 / self.img_width
- crowd_target_y1, crowd_target_y2 = crowd_target_y1 / self.img_height, crowd_target_y2 / self.img_height
- # Calculate normalized width and height for crowd target
- crowd_target_w = crowd_target_x2 - crowd_target_x1
- crowd_target_h = crowd_target_y2 - crowd_target_y1
- # Calculate normalized center coordinates for crowd target
- crowd_target_cx = crowd_target_x1 + crowd_target_w / 2
- crowd_target_cy = crowd_target_y1 + crowd_target_h / 2
- # Mock crowd targets
- # Create a single crowd target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- crowd_targets = torch.tensor(
- [[0, self.predefined_correct_class, crowd_target_cx, crowd_target_cy, crowd_target_w, crowd_target_h]], dtype=torch.float32
- ).reshape(1, 6)
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=[5.0],
- score_thres=self.score_thres,
- distance_metric=ManhattanDistance(),
- )
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=crowd_targets)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=1, recall=1, mAP=1, F1=1, verbose=self.verbose, description=scenario)
- # checks whether a single prediction that matches a single target will yield a perfect score
- # (Precision, Recall, F1 score, and mAP all set to 1.0). Using Euclidean distance.
- def test_distance_based_score_euclidean_distance_single_target_single_prediction_match(self):
- scenario = "a single prediction that matches a single target using Euclidean distance as a metric"
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is 5px away from the center of the target
- raw_preds = [torch.tensor([[15, 15, 10, 10]])] # Close to target
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 10, 10, 20, 20
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=[5.0],
- score_thres=self.score_thres,
- distance_metric=EuclideanDistance(),
- )
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=None)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=1.0, recall=1.0, mAP=1.0, F1=1.0, verbose=self.verbose, description=scenario)
- # checks whether a single prediction that doesn't match the target will yield zero for all metrics.
- def test_distance_based_score_euclidean_distance_single_target_single_prediction_miss(self):
- scenario = "a single prediction that doesn't match the target using Euclidean distance as a metric"
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is more than 5px away from the center of the target
- raw_preds = [torch.tensor([[15, 15, 10, 10]])] # Far from target
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 40, 40, 80, 80
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- self.metric = DetectionMetricsDistanceBased(
- num_cls=self.num_classes,
- post_prediction_callback=self.mock_post_prediction_callback,
- distance_thresholds=[5.0],
- score_thres=self.score_thres,
- distance_metric=EuclideanDistance(),
- )
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=None)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=0, recall=0, mAP=0, F1=0, verbose=self.verbose, description=scenario)
- # checks whether the metrics are calculated correctly when there are multiple
- # predictions but only one matches with a single target.
- def test_distance_based_score_euclidean_distance_single_target_few_predictions(self):
- scenario = "a few predictions 1 - match, 2 - don't, single target using Euclidean distance as a metric"
- # Set random seeds for reproducibility (to ensure the seeds even at this level)
- torch.manual_seed(42)
- random.seed(42)
- # Mock raw_preds (model's output) in format cx, cy, w, h; coordinates within image dimensions
- # One prediction is 5px away from the center of the target
- # The other two predictions are placed randomly
- raw_preds = [torch.tensor([[15, 15, 10, 10], [100, 50, 10, 10], [200, 300, 20, 15]])] # Close to target # Randomly placed # Randomly placed
- # Define target (unnormalized) coordinates within image dimensions
- target_x1, target_y1, target_x2, target_y2 = 10, 10, 20, 20
- # Normalize target coordinates
- target_x1, target_x2 = target_x1 / self.img_width, target_x2 / self.img_width
- target_y1, target_y2 = target_y1 / self.img_height, target_y2 / self.img_height
- # Calculate normalized width and height
- target_w = target_x2 - target_x1
- target_h = target_y2 - target_y1
- # Calculate normalized center coordinates
- target_cx = target_x1 + target_w / 2
- target_cy = target_y1 + target_h / 2
- # Mock targets
- # Create a single target for one image with shape (1, 6).
- # Format: (index, label, cx, cy, w, h)
- targets = torch.tensor([[0, self.predefined_correct_class, target_cx, target_cy, target_w, target_h]], dtype=torch.float32).reshape(1, 6)
- # Call the update method
- self.metric.update(preds=raw_preds, target=targets, device="cpu", inputs=self.img_tensor, crowd_targets=None)
- # Call the compute method to get the results
- results = self.metric.compute()
- # Validate the results
- self.validate_results(results, precision=0.3333, recall=1.0, mAP=1, F1=0.5, verbose=self.verbose, description=scenario)
- if __name__ == "__main__":
- unittest.main()
|