From 7416833b36010457b840ad2b1398e95bb9c791f1 Mon Sep 17 00:00:00 2001 From: Ievgen Popovych Date: Wed, 29 May 2024 14:48:11 +0300 Subject: [PATCH] tests: data: coco_evaluation: Fix test ...by deep copying results before passing to coco. The results dictionary will be attached as 'annotations' to the dataset and will be changed, making the use of the same structure impossible for the second time. Previously the test would fail on second call to `coco_api.loadRes()` with an exception (`KeyError: 'precision'` when trying to calculate difference between API results). Also, handle the case when one API produces an exception and the other one does not. Previously, the test code would ignore that and try to calculate the difference. Signed-off-by: Ievgen Popovych --- tests/data/test_coco_evaluation.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/data/test_coco_evaluation.py b/tests/data/test_coco_evaluation.py index 964f00284d..47303fc385 100644 --- a/tests/data/test_coco_evaluation.py +++ b/tests/data/test_coco_evaluation.py @@ -84,7 +84,7 @@ def test_fast_eval(self): api_exception = None try: with contextlib.redirect_stdout(io.StringIO()): - coco_dt = coco_api.loadRes(dt) + coco_dt = coco_api.loadRes(copy.deepcopy(dt)) coco_eval = COCOeval(coco_api, coco_dt, iou_type) for p, v in params.items(): setattr(coco_eval.params, p, v) @@ -98,7 +98,7 @@ def test_fast_eval(self): opt_exception = None try: with contextlib.redirect_stdout(io.StringIO()): - coco_dt = coco_api.loadRes(dt) + coco_dt = coco_api.loadRes(copy.deepcopy(dt)) coco_eval_opt = COCOeval_opt(coco_api, coco_dt, iou_type) for p, v in params.items(): setattr(coco_eval_opt.params, p, v) @@ -115,6 +115,11 @@ def test_fast_eval(self): opt_error = "" if opt_exception is None else type(opt_exception).__name__ msg = "%s: comparing COCO APIs, '%s' != '%s'" % (name, api_error, opt_error) self.assertTrue(api_error == opt_error, msg=msg) + elif (api_exception is None) ^ (opt_exception is None): + self.fail( + f"One of the APIs threw an exception while the other did not (api != opt): " + f"{api_exception} != {opt_exception}" + ) else: # Original API and optimized API should produce the same precision/recalls for k in ["precision", "recall"]: