forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_detection_coco_metrics.py
69 lines (55 loc) · 2.78 KB
/
test_detection_coco_metrics.py
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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import random
import unittest
import numpy as np
import torch
from monai.apps.detection.metrics.coco import COCOMetric
from monai.apps.detection.metrics.matching import matching_batch
from monai.data.box_utils import box_iou
class TestCOCOMetrics(unittest.TestCase):
def test_coco_run(self):
coco_metric = COCOMetric(classes=["c0", "c1", "c2"], iou_list=[0.1], max_detection=[10])
num_images = 10
val_outputs_all = []
val_targets_all = []
for _ in range(num_images):
# randomly generate gt boxes and pred boxes
num_gt_boxes = random.randint(1, 3)
num_pred_boxes = random.randint(0, 3)
box_start = torch.randint(3, (num_pred_boxes, 3))
box_stop = box_start + torch.randint(1, 32, (num_pred_boxes, 3))
boxes = torch.cat((box_start, box_stop), dim=1).to(torch.float16)
val_outputs_all.append(
{
"boxes": boxes,
"labels": torch.randint(3, (num_pred_boxes,)),
"scores": torch.randn((num_pred_boxes,)).absolute(),
}
)
box_start = torch.randint(3, (num_gt_boxes, 3))
box_stop = box_start + torch.randint(1, 32, (num_gt_boxes, 3))
boxes = torch.cat((box_start, box_stop), dim=1).to(torch.float16)
val_targets_all.append({"boxes": boxes, "labels": torch.randint(3, (num_gt_boxes,))})
results_metric = matching_batch(
iou_fn=box_iou,
iou_thresholds=coco_metric.iou_thresholds,
pred_boxes=[val_data_i["boxes"].numpy() for val_data_i in val_outputs_all],
pred_classes=[val_data_i["labels"].numpy() for val_data_i in val_outputs_all],
pred_scores=[val_data_i["scores"].numpy() for val_data_i in val_outputs_all],
gt_boxes=[val_data_i["boxes"].numpy() for val_data_i in val_targets_all],
gt_classes=[val_data_i["labels"].numpy() for val_data_i in val_targets_all],
)
val_epoch_metric_dict = coco_metric(results_metric)[0]
np.testing.assert_array_less([-16.01], [sum(val_epoch_metric_dict.values())])
if __name__ == "__main__":
unittest.main()