-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_world_evalutor.py
143 lines (110 loc) · 5.33 KB
/
test_world_evalutor.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
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
#!/usr/bin/python3
import concurrent.futures
import copy
import itertools
import os
import unittest
from collections import defaultdict
import numpy as np
import faster_coco_eval.core.mask as mask_util
from faster_coco_eval import COCO, COCOeval_faster
def _evaluate(coco_eval_proc: COCOeval_faster, anns: list):
coco_eval_proc.params.imgIds = [ann["image_id"] for ann in anns]
coco_eval_proc.cocoDt = coco_eval_proc.cocoGt.loadRes(anns)
coco_eval_proc.evaluate()
# num_images * num_area_ranges * num_categories
return np.array(coco_eval_proc._evalImgs_cpp).reshape(
len(coco_eval_proc.params.catIds), len(coco_eval_proc.params.areaRng), len(coco_eval_proc.params.imgIds)
), coco_eval_proc.params.imgIds
class TestWorldCoco(unittest.TestCase):
"""Test basic rankX COCO functionality."""
def setUp(self):
self.gt_file = os.path.join("dataset", "gt_dataset.json")
self.dt_file = os.path.join("dataset", "dt_dataset.json")
self.gt_lvis_file = os.path.join("lvis_dataset", "lvis_val_100.json")
self.dt_lvis_file = os.path.join("lvis_dataset", "lvis_results_100.json")
if not os.path.exists(self.gt_file):
self.gt_file = os.path.join(os.path.dirname(__file__), self.gt_file)
self.dt_file = os.path.join(os.path.dirname(__file__), self.dt_file)
self.gt_lvis_file = os.path.join(os.path.dirname(__file__), self.gt_lvis_file)
self.dt_lvis_file = os.path.join(os.path.dirname(__file__), self.dt_lvis_file)
prepared_anns = COCO.load_json(self.dt_file)
cocoGt = COCO(self.gt_file)
self.prepared_anns_droped_bbox = [
{
"image_id": ann["image_id"],
"category_id": ann["category_id"],
"iscrowd": ann["iscrowd"],
"id": ann["id"],
"score": ann["score"],
"segmentation": mask_util.merge(
mask_util.frPyObjects(
ann["segmentation"],
cocoGt.imgs[ann["image_id"]]["height"],
cocoGt.imgs[ann["image_id"]]["width"],
)
),
}
for ann in prepared_anns
]
self.prepared_anns_droped_bbox_by_image_id = defaultdict(list)
for ann in self.prepared_anns_droped_bbox:
self.prepared_anns_droped_bbox_by_image_id[ann["image_id"]].append(copy.deepcopy(ann))
def test_world(self):
# MULTI
coco_eval_rank = COCOeval_faster(COCO(self.gt_file), iouType="segm", separate_eval=True)
eval_imgs = []
eval_img_ids = []
with concurrent.futures.ProcessPoolExecutor(len(self.prepared_anns_droped_bbox_by_image_id)) as executor:
for evalImgs, imgIds in executor.map(
_evaluate, itertools.repeat(coco_eval_rank), self.prepared_anns_droped_bbox_by_image_id.values()
):
eval_imgs.append(evalImgs)
eval_img_ids += imgIds
coco_eval_rank.params.imgIds = eval_img_ids
coco_eval_rank._paramsEval = copy.deepcopy(coco_eval_rank.params)
coco_eval_rank._evalImgs_cpp = np.concatenate(eval_imgs, axis=2).ravel().tolist()
coco_eval_rank.accumulate()
coco_eval_rank.summarize()
# SOLO
coco_eval = COCOeval_faster(COCO(self.gt_file), iouType="segm")
coco_eval.cocoDt = coco_eval.cocoGt.loadRes(self.prepared_anns_droped_bbox)
coco_eval.evaluate()
coco_eval.accumulate()
coco_eval.summarize()
self.assertAlmostEqual(coco_eval.stats_as_dict, coco_eval_rank.stats_as_dict, 12)
def test_world_lvis(self):
# MULTI
coco_eval_rank = COCOeval_faster(COCO(self.gt_lvis_file), iouType="bbox", lvis_style=True, separate_eval=True)
prepared_anns = defaultdict(list)
for ann in COCO.load_json(self.dt_lvis_file):
prepared_anns[ann["image_id"]].append(copy.deepcopy(ann))
all_images_ids = list(prepared_anns.keys())
rank2_prepared_anns = defaultdict(list)
for key in all_images_ids[::2]:
rank2_prepared_anns["rank1"] += prepared_anns[key]
for key in all_images_ids[1::2]:
rank2_prepared_anns["rank2"] += prepared_anns[key]
eval_imgs = []
eval_img_ids = []
with concurrent.futures.ProcessPoolExecutor(2) as executor:
for evalImgs, imgIds in executor.map(
_evaluate, itertools.repeat(coco_eval_rank), rank2_prepared_anns.values()
):
eval_imgs.append(evalImgs)
eval_img_ids += imgIds
coco_eval_rank.params.imgIds = eval_img_ids
coco_eval_rank._paramsEval = copy.deepcopy(coco_eval_rank.params)
coco_eval_rank.freq_groups = coco_eval_rank._prepare_freq_group()
coco_eval_rank._evalImgs_cpp = np.concatenate(eval_imgs, axis=2).ravel().tolist()
coco_eval_rank.accumulate()
coco_eval_rank.summarize()
# SOLO
coco_eval = COCOeval_faster(COCO(self.gt_lvis_file), iouType="bbox", lvis_style=True)
coco_eval.cocoDt = coco_eval.cocoGt.loadRes(self.dt_lvis_file)
coco_eval.evaluate()
coco_eval.accumulate()
coco_eval.summarize()
self.assertAlmostEqual(coco_eval.stats_as_dict, coco_eval_rank.stats_as_dict, 12)
if __name__ == "__main__":
unittest.main()