Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
MiXaiLL76 committed Nov 3, 2024
1 parent 88e8911 commit c337f25
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions tests/test_world_evalutor.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#!/usr/bin/python3
import os
import unittest
import faster_coco_eval.core.mask as mask_util
from faster_coco_eval import COCO, COCOeval_faster
import concurrent.futures
import copy
import itertools
import os
import unittest
from collections import defaultdict

import numpy as np
import itertools

def _evaluate(coco_eval_proc : COCOeval_faster, anns : list):
coco_eval_proc.params.imgIds = [ann['image_id'] for ann in anns]
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()
return coco_eval_proc._evalImgs_cpp, coco_eval_proc.params.imgIds


class TestWorldCoco(unittest.TestCase):
"""Test basic rankX COCO functionality."""

Expand Down Expand Up @@ -58,20 +62,22 @@ def setUp(self):
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()):
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.array(eval_imgs).T.ravel().tolist()

coco_eval_rank.accumulate()
coco_eval_rank.summarize()

Expand All @@ -87,33 +93,35 @@ def test_world(self):
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]
rank2_prepared_anns["rank1"] += prepared_anns[key]

for key in all_images_ids[1::2]:
rank2_prepared_anns['rank2'] += prepared_anns[key]
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()):
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.array(eval_imgs).T.ravel().tolist()

coco_eval_rank.accumulate()
coco_eval_rank.summarize()

Expand All @@ -124,8 +132,8 @@ def test_world_lvis(self):
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()

0 comments on commit c337f25

Please sign in to comment.