Skip to content

Commit

Permalink
more yolo v8 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eldies committed Jul 30, 2024
1 parent fb2f5d4 commit 576c790
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
4 changes: 4 additions & 0 deletions datumaro/plugins/yolo_format/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ def _check_is_rectangle(p1, p2, p3, p4):
raise InvalidAnnotationError(
"Given points do not form a rectangle: opposite sides have different slope angles."
)
if abs((p12_angle - p23_angle) % math.pi - math.pi / 2) > 0.001:
raise InvalidAnnotationError(
"Given points do not form a rectangle: adjacent sides are not orthogonal."
)

def _load_one_annotation(
self, parts: List[str], image_height: int, image_width: int
Expand Down
58 changes: 46 additions & 12 deletions tests/cli/test_yolo_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@


class YoloIntegrationScenarios(TestCase):
ASSET_PATH = ["yolo_dataset", "yolo"]
FORMAT_NAME = "yolo"

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_can_save_and_load_yolo_dataset(self):
target_dataset = Dataset.from_iterable(
Expand All @@ -35,11 +38,14 @@ def test_can_save_and_load_yolo_dataset(self):

with TestDir() as test_dir:
yolo_dir = osp.join(
__file__[: __file__.rfind(osp.join("tests", ""))], "tests", "assets", "yolo_dataset"
__file__[: __file__.rfind(osp.join("tests", ""))],
"tests",
"assets",
*self.ASSET_PATH,
)

run(self, "create", "-o", test_dir)
run(self, "import", "-p", test_dir, "-f", "yolo", yolo_dir)
run(self, "import", "-p", test_dir, "-f", self.FORMAT_NAME, yolo_dir)

export_dir = osp.join(test_dir, "export_dir")
run(
Expand All @@ -50,12 +56,12 @@ def test_can_save_and_load_yolo_dataset(self):
"-o",
export_dir,
"-f",
"yolo",
self.FORMAT_NAME,
"--",
"--save-media",
)

parsed_dataset = Dataset.import_from(export_dir, format="yolo")
parsed_dataset = Dataset.import_from(export_dir, format=self.FORMAT_NAME)
compare_datasets(self, target_dataset, parsed_dataset)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
Expand All @@ -82,9 +88,20 @@ def test_can_export_mot_as_yolo(self):
run(self, "import", "-p", test_dir, "-f", "mot_seq", mot_dir)

yolo_dir = osp.join(test_dir, "yolo_dir")
run(self, "export", "-p", test_dir, "-o", yolo_dir, "-f", "yolo", "--", "--save-media")
run(
self,
"export",
"-p",
test_dir,
"-o",
yolo_dir,
"-f",
self.FORMAT_NAME,
"--",
"--save-media",
)

parsed_dataset = Dataset.import_from(yolo_dir, format="yolo")
parsed_dataset = Dataset.import_from(yolo_dir, format=self.FORMAT_NAME)
compare_datasets(self, target_dataset, parsed_dataset)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
Expand Down Expand Up @@ -126,14 +143,14 @@ def test_can_convert_voc_to_yolo(self):
"-i",
voc_dir,
"-f",
"yolo",
self.FORMAT_NAME,
"-o",
yolo_dir,
"--",
"--save-media",
)

parsed_dataset = Dataset.import_from(yolo_dir, format="yolo")
parsed_dataset = Dataset.import_from(yolo_dir, format=self.FORMAT_NAME)
compare_datasets(self, target_dataset, parsed_dataset, require_media=True)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
Expand All @@ -152,11 +169,14 @@ def test_can_delete_labels_from_yolo_dataset(self):

with TestDir() as test_dir:
yolo_dir = osp.join(
__file__[: __file__.rfind(osp.join("tests", ""))], "tests", "assets", "yolo_dataset"
__file__[: __file__.rfind(osp.join("tests", ""))],
"tests",
"assets",
*self.ASSET_PATH,
)

run(self, "create", "-o", test_dir)
run(self, "import", "-p", test_dir, "-f", "yolo", yolo_dir)
run(self, "import", "-p", test_dir, "-f", self.FORMAT_NAME, yolo_dir)

run(
self,
Expand Down Expand Up @@ -185,8 +205,22 @@ def test_can_delete_labels_from_yolo_dataset(self):

export_dir = osp.join(test_dir, "export")
run(
self, "export", "-p", test_dir, "-o", export_dir, "-f", "yolo", "--", "--save-image"
self,
"export",
"-p",
test_dir,
"-o",
export_dir,
"-f",
self.FORMAT_NAME,
"--",
"--save-image",
)

parsed_dataset = Dataset.import_from(export_dir, format="yolo")
parsed_dataset = Dataset.import_from(export_dir, format=self.FORMAT_NAME)
compare_datasets(self, target_dataset, parsed_dataset)


class YOLOv8IntegrationScenarios(YoloIntegrationScenarios):
ASSET_PATH = ["yolo_dataset", "yolov8"]
FORMAT_NAME = "yolov8"
11 changes: 11 additions & 0 deletions tests/unit/data_formats/test_yolo_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,17 @@ def test_can_report_invalid_shape(self, test_dir):
assert isinstance(capture.value.__cause__, InvalidAnnotationError)
assert "Given points do not form a rectangle" in str(capture.value.__cause__)

@mark_requirement(Requirements.DATUM_ERROR_REPORTING)
def test_can_report_invalid_shape_parallelogram(self, test_dir):
self._prepare_dataset(test_dir)
with open(osp.join(test_dir, self._get_annotation_dir(), "a.txt"), "w") as f:
f.write("0 0.1 0.1 0.5 0.1 0.6 0.5 0.2 0.5")

with pytest.raises(AnnotationImportError) as capture:
Dataset.import_from(test_dir, self.IMPORTER.NAME).init_cache()
assert isinstance(capture.value.__cause__, InvalidAnnotationError)
assert "adjacent sides are not orthogonal" in str(capture.value.__cause__)


class YOLOv8PoseExtractorTest(YOLOv8ExtractorTest):
IMPORTER = YOLOv8PoseImporter
Expand Down

0 comments on commit 576c790

Please sign in to comment.