Skip to content

Commit

Permalink
Update test_pycoco_equivalence.py
Browse files Browse the repository at this point in the history
  • Loading branch information
EMalagoli92 committed Apr 4, 2024
1 parent 0bf39e1 commit fb0abf8
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions tests/test_pycoco_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import unittest
import copy
from typing import Any
from typing import Any, Literal
import numpy as np
from parameterized import parameterized_class
from parameterized import parameterized, parameterized_class

from src.od_metrics import ODMetrics, iou
from src.od_metrics.constants import DEFAULT_COCO
from tests.utils import annotations_generator, pycoco_converter,\
from tests.utils import annotations_generator, pycoco_converter, \
test_equality, rename_dict, xywh_to
from tests.config import TESTS

Expand Down Expand Up @@ -295,28 +295,37 @@ def is_default_coco(od_metrics_obj) -> bool:
)


@unittest.skipUnless(
_PYCOCOTOOLS_AVAILABLE,
"This unittest needs `pycocotools`. Please intall by "
"running `pip install pycocotools`"
)
class TestPyCocoEquivalenceIoU(unittest.TestCase):
"""Test equivalence: od-metrics and `pycocotools` iou function."""
class TestIoU(unittest.TestCase):
"""Test IoU metric."""

HIGH = 100000
SIZE = 3000

def test_iou(self) -> None:
"""Test IoU."""
iscrowd = list(map(
bool,
np.random.randint(
low=0,
high=2,
size=[self.SIZE]
).tolist()
)
@unittest.skipUnless(
_PYCOCOTOOLS_AVAILABLE,
"This unittest needs `pycocotools`. Please intall by "
"running `pip install pycocotools`"
)
@parameterized.expand([(None), ("random")])
def test_pycoco_equivalence(
self,
iscrowd_mode: Literal["random", None],
) -> None:
"""Test equivalence: `od_metrics` and `pycocotools` iou function."""
if iscrowd_mode == "random":
iscrowd = list(map(
bool,
np.random.randint(
low=0,
high=2,
size=[self.SIZE]
).tolist()
)
)
iscrowd_pycoco = iscrowd
else:
iscrowd = None
iscrowd_pycoco = [False] * self.SIZE
y_pred = np.random.randint(
low=1,
high=self.HIGH,
Expand All @@ -333,10 +342,39 @@ def test_iou(self) -> None:
y_pred=y_pred,
iscrowd=iscrowd
)
pycoco_ious = maskUtils.iou(y_pred, y_true, iscrowd)
pycoco_ious = maskUtils.iou(y_pred, y_true, iscrowd_pycoco)

self.assertTrue(test_equality(od_metrics_ious, pycoco_ious))

def test_length_exception(self) -> None:
"""Test exception `iscrowd` and `y_true` different length."""
iscrowd = list(map(
bool,
np.random.randint(
low=0,
high=2,
size=[self.SIZE + 1]
).tolist()
)
)
y_pred = np.random.randint(
low=1,
high=self.HIGH,
size=[self.SIZE, 4]
)
y_true = np.random.randint(
low=1,
high=self.HIGH,
size=[self.SIZE, 4]
)

with self.assertRaises(ValueError):
iou(
y_true=y_true,
y_pred=y_pred,
iscrowd=iscrowd,
)


if __name__ == "__main__":
unittest.main()

0 comments on commit fb0abf8

Please sign in to comment.