Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a folder for unlabeled items in VggFace2 dataset format #89

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions datumaro/plugins/vgg_face2_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class VggFace2Path:
BBOXES_FILE = 'loose_bb_'
LANDMARKS_FILE = 'loose_landmark_'
LABELS_FILE = 'labels.txt'
IMAGES_DIR_NO_LABEL = 'no_label'

class VggFace2Extractor(SourceExtractor):
def __init__(self, path):
Expand Down Expand Up @@ -50,8 +51,9 @@ def _load_categories(self):
subset_path = osp.join(self._dataset_dir, self._subset)
if osp.isdir(subset_path):
for images_dir in sorted(os.listdir(subset_path)):
if osp.isdir(osp.join(subset_path, images_dir)):
label_cat.add(images_dir)
if osp.isdir(osp.join(subset_path, images_dir)) and \
images_dir != VggFace2Path.IMAGES_DIR_NO_LABEL:
label_cat.add(images_dir)
return { AnnotationType.label: label_cat }

def _load_items(self, path):
Expand All @@ -64,9 +66,10 @@ def _load_items(self, path):
label = None
if '/' in item_id:
label_name = item_id.split('/')[0]
label = self._categories[AnnotationType.label].find(label_name)[0]
if label is not None:
item_id = item_id[len(label_name) + 1:]
if label_name != VggFace2Path.IMAGES_DIR_NO_LABEL:
label = \
self._categories[AnnotationType.label].find(label_name)[0]
item_id = item_id[len(label_name) + 1:]
if item_id not in items:
image_path = osp.join(self._dataset_dir, self._subset,
row['NAME_ID'] + VggFace2Path.IMAGE_EXT)
Expand All @@ -90,9 +93,10 @@ def _load_items(self, path):
label = None
if '/' in item_id:
label_name = item_id.split('/')[0]
label = self._categories[AnnotationType.label].find(label_name)[0]
if label is not None:
item_id = item_id[len(label_name) + 1:]
if label_name != VggFace2Path.IMAGES_DIR_NO_LABEL:
label = \
self._categories[AnnotationType.label].find(label_name)[0]
item_id = item_id[len(label_name) + 1:]
if item_id not in items:
image_path = osp.join(self._dataset_dir, self._subset,
row['NAME_ID'] + VggFace2Path.IMAGE_EXT)
Expand Down Expand Up @@ -146,16 +150,19 @@ def apply(self):
+ item.id + VggFace2Path.IMAGE_EXT))
else:
self._save_image(item, osp.join(subset_dir,
VggFace2Path.IMAGES_DIR_NO_LABEL,
item.id + VggFace2Path.IMAGE_EXT))

landmarks = [a for a in item.annotations
if a.type == AnnotationType.points]
for landmark in landmarks:
name_id = item.id
if landmark.label is not None \
and label_categories[landmark.label].name:
if landmark.label is not None and \
label_categories[landmark.label].name:
name_id = label_categories[landmark.label].name \
+ '/' + item.id
else:
name_id = VggFace2Path.IMAGES_DIR_NO_LABEL \
+ '/' + item.id
points = landmark.points
landmarks_table.append({'NAME_ID': name_id,
'P1X': points[0], 'P1Y': points[1],
Expand All @@ -167,26 +174,31 @@ def apply(self):
bboxes = [a for a in item.annotations
if a.type == AnnotationType.bbox]
for bbox in bboxes:
name_id = item.id
if bbox.label is not None and \
label_categories[bbox.label].name:
name_id = label_categories[bbox.label].name \
+ '/' + item.id
else:
name_id = VggFace2Path.IMAGES_DIR_NO_LABEL \
+ '/' + item.id
bboxes_table.append({'NAME_ID': name_id, 'X': bbox.x,
'Y': bbox.y, 'W': bbox.w, 'H': bbox.h})

labels = [a for a in item.annotations
if a.type == AnnotationType.label]
for label in labels:
name_id = item.id
if label.label is not None and \
label_categories[label.label].name:
name_id = label_categories[label.label].name \
+ '/' + item.id
else:
name_id = VggFace2Path.IMAGES_DIR_NO_LABEL \
+ '/' + item.id
landmarks_table.append({'NAME_ID': name_id})

if not landmarks and not bboxes and not labels:
landmarks_table.append({'NAME_ID': item.id})
landmarks_table.append({'NAME_ID':
VggFace2Path.IMAGES_DIR_NO_LABEL + '/' + item.id})

landmarks_path = osp.join(save_dir, VggFace2Path.ANNOTATION_DIR,
VggFace2Path.LANDMARKS_FILE + subset_name + '.csv')
Expand Down
22 changes: 22 additions & 0 deletions tests/test_vgg_face2_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ def test_can_save_dataset_with_no_save_images(self):

compare_datasets(self, source_dataset, parsed_dataset)

def test_can_save_dataset_with_no_labels(self):
source_dataset = Dataset.from_iterable([
DatasetItem(id='1', image=np.ones((8, 8, 3)),
annotations=[
Bbox(0, 2, 4, 2, group=1),
Points([4.23, 4.32, 5.34, 4.45, 3.54,
3.56, 4.52, 3.51, 4.78, 3.34], group=1),
]
),
DatasetItem(id='2', image=np.ones((8, 8, 3)),
annotations=[
Bbox(2, 2, 4, 2, group=1),
]
),
], categories=[])

with TestDir() as test_dir:
VggFace2Converter.convert(source_dataset, test_dir, save_images=False)
parsed_dataset = Dataset.import_from(test_dir, 'vgg_face2')

compare_datasets(self, source_dataset, parsed_dataset)

DUMMY_DATASET_DIR = osp.join(osp.dirname(__file__), 'assets', 'vgg_face2_dataset')

class VggFace2ImporterTest(TestCase):
Expand Down