Skip to content

Commit bb8b293

Browse files
add extra_annotation_keys parameter to lvis json loader
Summary: Thanks for your contribution! If you're sending a large PR (e.g., >100 lines), please open an issue first about the feature / bug, and indicate how you want to contribute. We do not always accept features. See https://detectron2.readthedocs.io/notes/contributing.html#pull-requests about how we handle PRs. Before submitting a PR, please run `dev/linter.sh` to lint the code. Pull Request resolved: fairinternal/detectron2#556 Reviewed By: ppwwyyxx Differential Revision: D29925800 Pulled By: alexander-kirillov fbshipit-source-id: 8e8b7452da98d73f2fd6a7f936658cfcb9ed98f4
1 parent 5b53d8a commit bb8b293

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

detectron2/data/datasets/lvis.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def register_lvis_instances(name, metadata, json_file, image_root):
3737
)
3838

3939

40-
def load_lvis_json(json_file, image_root, dataset_name=None):
40+
def load_lvis_json(json_file, image_root, dataset_name=None, extra_annotation_keys=None):
4141
"""
4242
Load a json file in LVIS's annotation format.
4343
@@ -47,6 +47,9 @@ def load_lvis_json(json_file, image_root, dataset_name=None):
4747
dataset_name (str): the name of the dataset (e.g., "lvis_v0.5_train").
4848
If provided, this function will put "thing_classes" into the metadata
4949
associated with this dataset.
50+
extra_annotation_keys (list[str]): list of per-annotation keys that should also be
51+
loaded into the dataset dict (besides "bbox", "bbox_mode", "category_id",
52+
"segmentation"). The values for these keys will be returned as-is.
5053
5154
Returns:
5255
list[dict]: a list of dicts in Detectron2 standard format. (See
@@ -106,6 +109,13 @@ def load_lvis_json(json_file, image_root, dataset_name=None):
106109

107110
logger.info("Loaded {} images in the LVIS format from {}".format(len(imgs_anns), json_file))
108111

112+
if extra_annotation_keys:
113+
logger.info(
114+
"The following extra annotation keys will be loaded: {} ".format(extra_annotation_keys)
115+
)
116+
else:
117+
extra_annotation_keys = []
118+
109119
def get_file_name(img_root, img_dict):
110120
# Determine the path including the split folder ("train2017", "val2017", "test2017") from
111121
# the coco_url field. Example:
@@ -145,6 +155,8 @@ def get_file_name(img_root, img_dict):
145155
), "Annotation contains an invalid polygon with < 3 points"
146156
assert len(segm) > 0
147157
obj["segmentation"] = segm
158+
for extra_ann_key in extra_annotation_keys:
159+
obj[extra_ann_key] = anno[extra_ann_key]
148160
objs.append(obj)
149161
record["annotations"] = objs
150162
dataset_dicts.append(record)

0 commit comments

Comments
 (0)