"...python/git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "6d55f60e7794de3859137340259782372236010f"
Commit d369931a authored by Sam Tsai's avatar Sam Tsai Committed by Facebook GitHub Bot
Browse files

add option to filter empty annotations

Summary: Add option for controlling empty annotation filtering.

Reviewed By: zhanghang1989

Differential Revision: D34365265

fbshipit-source-id: 261c6879636f19138de781098f47dee4909de9e7
parent cb41f780
...@@ -249,6 +249,7 @@ def convert_to_dict_list( ...@@ -249,6 +249,7 @@ def convert_to_dict_list(
anns: List[Dict], anns: List[Dict],
dataset_name: Optional[str] = None, dataset_name: Optional[str] = None,
image_direct_copy_keys: Optional[List[str]] = None, image_direct_copy_keys: Optional[List[str]] = None,
filter_empty_annotations: Optional[bool] = True,
) -> List[Dict]: ) -> List[Dict]:
ann_error_report = { ann_error_report = {
...@@ -263,7 +264,10 @@ def convert_to_dict_list( ...@@ -263,7 +264,10 @@ def convert_to_dict_list(
name: ErrorEntry(name, msg, 0) name: ErrorEntry(name, msg, 0)
for name, msg in [ for name, msg in [
("ignore_image_root", f"Image root ignored {image_root}"), ("ignore_image_root", f"Image root ignored {image_root}"),
("no_annotations", "Image filtered"), (
"no_annotations",
"Image filtered" if filter_empty_annotations else "Warning",
),
] ]
} }
ex_warning_fn = None ex_warning_fn = None
...@@ -299,7 +303,8 @@ def convert_to_dict_list( ...@@ -299,7 +303,8 @@ def convert_to_dict_list(
) )
if len(converted_anns) == 0: if len(converted_anns) == 0:
image_error_report["no_annotations"].cnt += 1 image_error_report["no_annotations"].cnt += 1
continue if filter_empty_annotations:
continue
record["annotations"] = converted_anns record["annotations"] = converted_anns
# Copy keys if additionally asked # Copy keys if additionally asked
...@@ -352,6 +357,7 @@ def extended_coco_load( ...@@ -352,6 +357,7 @@ def extended_coco_load(
dataset_name: Optional[str] = None, dataset_name: Optional[str] = None,
loaded_json: Optional[str] = None, loaded_json: Optional[str] = None,
image_direct_copy_keys: List[str] = None, image_direct_copy_keys: List[str] = None,
filter_empty_annotations: Optional[bool] = True,
) -> List[Dict]: ) -> List[Dict]:
""" """
Load a json file with COCO's annotation format. Load a json file with COCO's annotation format.
...@@ -419,6 +425,7 @@ def extended_coco_load( ...@@ -419,6 +425,7 @@ def extended_coco_load(
anns, anns,
dataset_name, dataset_name,
image_direct_copy_keys=image_direct_copy_keys, image_direct_copy_keys=image_direct_copy_keys,
filter_empty_annotations=filter_empty_annotations,
) )
......
...@@ -86,6 +86,7 @@ class TestD2GoDatasets(unittest.TestCase): ...@@ -86,6 +86,7 @@ class TestD2GoDatasets(unittest.TestCase):
img_list = [ img_list = [
{"id": 0, "width": 50, "height": 50, "file_name": "a.png"}, {"id": 0, "width": 50, "height": 50, "file_name": "a.png"},
{"id": 1, "width": 50, "height": 50, "file_name": "b.png"}, {"id": 1, "width": 50, "height": 50, "file_name": "b.png"},
{"id": 2, "width": 50, "height": 50, "file_name": "b.png"},
] ]
ann_list = [ ann_list = [
[ [
...@@ -140,17 +141,18 @@ class TestD2GoDatasets(unittest.TestCase): ...@@ -140,17 +141,18 @@ class TestD2GoDatasets(unittest.TestCase):
"bbox": [0, 0, 0, 0], "bbox": [0, 0, 0, 0],
}, },
], ],
[],
] ]
out_dict_list = extended_coco.convert_to_dict_list( out_dict_list = extended_coco.convert_to_dict_list("", [0], img_list, ann_list)
"",
[0],
img_list,
ann_list,
)
self.assertEqual(len(out_dict_list), 1) self.assertEqual(len(out_dict_list), 1)
self.assertEqual(len(out_dict_list[0]["annotations"]), 1) self.assertEqual(len(out_dict_list[0]["annotations"]), 1)
out_dict_list = extended_coco.convert_to_dict_list(
"", [0], img_list, ann_list, filter_empty_annotations=False
)
self.assertEqual(len(out_dict_list), 3)
@tempdir @tempdir
def test_coco_injection(self, tmp_dir): def test_coco_injection(self, tmp_dir):
image_dir, json_file = create_test_images_and_dataset_json(tmp_dir) image_dir, json_file = create_test_images_and_dataset_json(tmp_dir)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment