Skip to content

Commit

Permalink
feat(dataset): add dataloader for CIHP dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmallows committed Aug 19, 2021
1 parent b51a4c3 commit f7c32f1
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tensorbay/opendataset/CIHP/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#
# pylint: disable=invalid-name

"""Dataloader of CIHP."""

from .loader import CIHP

__all__ = ["CIHP"]
27 changes: 27 additions & 0 deletions tensorbay/opendataset/CIHP/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"SEMANTIC_MASK": {
"categories": [
{ "name": "Background" },
{ "name": "Hat" },
{ "name": "Hair" },
{ "name": "Glove" },
{ "name": "Sunglasses" },
{ "name": "UpperClothes" },
{ "name": "Dress" },
{ "name": "Coat" },
{ "name": "Socks" },
{ "name": "Pants" },
{ "name": "Torso-skin" },
{ "name": "Scarf" },
{ "name": "Skirt" },
{ "name": "Face" },
{ "name": "Left-arm" },
{ "name": "Right-arm" },
{ "name": "Left-leg" },
{ "name": "Right-leg" },
{ "name": "Left-shoe" },
{ "name": "Right-shoe" }
]
},
"INSTANCE_MASK": {}
}
96 changes: 96 additions & 0 deletions tensorbay/opendataset/CIHP/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#
# pylint: disable=invalid-name, missing-module-docstring

import os

from ...dataset import Data, Dataset
from ...label import InstanceMask, SemanticMask

DATASET_NAME = "CIHP"
_SEGMENTS_INFO = (("train", "Training"), ("val", "Validation"), ("test", "Testing"))


def CIHP(path: str) -> Dataset:
"""Dataloader of the `CIHP`_ dataset.
.. _CIHP: https://github.com/Engineering-Course/CIHP_PGN
The file structure should be like::
<path>
instance-level_human_parsing/
Testing/
Images/
0000002.jpg
...
test_id.txt
Training/
Images/
0000006.jpg
...
Categories/
0000006.png
...
Human/
0000006.png
...
Instances/
0000006.png
...
train_id.txt
Validation/
Images/
0000001.jpg
...
Categories/
0000001.png
...
Human/
0000001.png
...
Instances/
0000001.png
...
val_id.txt
Arguments:
path: The root directory of the dataset.
Returns:
Loaded :class:`~tensorbay.dataset.dataset.Dataset` instance.
"""
root_path = os.path.join(
os.path.abspath(os.path.expanduser(path)), "instance-level_human_parsing"
)

dataset = Dataset(DATASET_NAME)
dataset.load_catalog(os.path.join(os.path.dirname(__file__), "catalog.json"))

for segment_name, segment_pathname in _SEGMENTS_INFO:
segment = dataset.create_segment(segment_name)
work_path = os.path.join(root_path, segment_pathname)
image_path = os.path.join(work_path, "Images")
if segment_name == "test":
with open(os.path.join(work_path, "test_id.txt"), "r") as fp:
for filename in fp:
segment.append(Data(os.path.join(image_path, f"{filename.strip()}.jpg")))
else:
category_path = os.path.join(work_path, "Category_ids")
instance_path = os.path.join(work_path, "Instance_ids")
with open(os.path.join(work_path, f"{segment_name}_id.txt"), "r") as fp:
for filename in fp:
filename = filename.strip()
data = Data(os.path.join(image_path, f"{filename}.jpg"))
label = data.label
label.semantic_mask = SemanticMask(
os.path.join(category_path, f"{filename}.png")
)
label.instance_mask = InstanceMask(
os.path.join(instance_path, f"{filename}.png")
)
segment.append(data)
return dataset
1 change: 1 addition & 0 deletions tensorbay/opendataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"CarConnection",
"CCPD",
"CCPDGreen",
"CIHP",
"CoinImage",
"COVIDChestXRay",
"CompCars",
Expand Down

0 comments on commit f7c32f1

Please sign in to comment.