Skip to content

Commit

Permalink
tests: data: coco_evaluation: Fix test
Browse files Browse the repository at this point in the history
...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 <[email protected]>
  • Loading branch information
Jmennius committed May 30, 2024
1 parent 0df2d73 commit 7416833
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tests/data/test_coco_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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"]:
Expand Down

0 comments on commit 7416833

Please sign in to comment.