Skip to content

Commit

Permalink
feat(opendataset): add dataloader for the CityscapesGTFine dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
watsonvv committed Dec 16, 2021
1 parent 9a58767 commit 1b9af5d
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 127 deletions.
1 change: 1 addition & 0 deletions docs/source/reference/api/opendataset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tensorbay.opendataset
CCPD
CCPDGreen
CIHP
CityscapesGTFine
COCO2017
COVIDChestXRay
COVID_CT
Expand Down
11 changes: 11 additions & 0 deletions tensorbay/opendataset/Cityscapes/__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 the CityscapesGTFine dataset."""

from tensorbay.opendataset.Cityscapes.loader import CityscapesGTFine

__all__ = ["CityscapesGTFine"]
56 changes: 56 additions & 0 deletions tensorbay/opendataset/Cityscapes/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"SEMANTIC_MASK": {
"categories": [
{ "name": "void", "categoryId": 0 },
{ "name": "flat", "categoryId": 1 },
{ "name": "construction", "categoryId": 2 },
{ "name": "object", "categoryId": 3 },
{ "name": "nature", "categoryId": 4 },
{ "name": "sky", "categoryId": 5 },
{ "name": "human", "categoryId": 6 },
{ "name": "vehicle", "categoryId": 7 }
]
},
"INSTANCE_MASK": {
"categories": [{ "name": "Background", "categoryId": 0 }]
},
"POLYGON": {
"categories": [
{ "name": "unlabeled" },
{ "name": "ego vehicle" },
{ "name": "rectification border" },
{ "name": "out of roi" },
{ "name": "static" },
{ "name": "dynamic" },
{ "name": "ground" },
{ "name": "road" },
{ "name": "sidewalk" },
{ "name": "parking" },
{ "name": "rail track" },
{ "name": "building" },
{ "name": "wall" },
{ "name": "fence" },
{ "name": "guard rail" },
{ "name": "bridge" },
{ "name": "tunnel" },
{ "name": "pole" },
{ "name": "polegroup" },
{ "name": "traffic light" },
{ "name": "traffic sign" },
{ "name": "vegetation" },
{ "name": "terrain" },
{ "name": "sky" },
{ "name": "person" },
{ "name": "rider" },
{ "name": "car" },
{ "name": "truck" },
{ "name": "bus" },
{ "name": "caravan" },
{ "name": "trailer" },
{ "name": "train" },
{ "name": "motorcycle" },
{ "name": "bicycle" },
{ "name": "license plate" }
]
}
}
107 changes: 107 additions & 0 deletions tensorbay/opendataset/Cityscapes/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3
#
# Copyright 2021 Graviti. Licensed under MIT License.
#
# pylint: disable=invalid-name

"""Dataloader of the CityscapesGTFine dataset."""

import json
import os
from glob import glob
from typing import List

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

DATASET_NAME = "CityscapesGTFine"
_SEGMENT_NAMES = {"test", "train", "val"}


def CityscapesGTFine(path: str) -> Dataset:
"""`CityscapesGTFine <https://www.cityscapes-dataset.com/>`_ dataset.
The file structure should be like::
<path>
leftImg8bit/
test/
berlin/
berlin_000000_000019_leftImg8bit.png
...
...
train/
aachen/
aachen_000000_000019_leftImg8bit.png
...
...
val/
frankfurt/
frankfurt_000000_000019_leftImg8bit.png
...
...
...
gtFine/
test/
berlin/
berlin_000000_000019_gtFine_instanceIds.png
berlin_000000_000019_gtFine_labelIds.png
berlin_000000_000019_gtFine_polygons.json
...
...
train/
aachen/
aachen_000000_000019_gtFine_instanceIds.png
aachen_000000_000019_gtFine_labelIds.png
aachen_000000_000019_gtFine_polygons.json
...
...
val/
frankfurt/
frankfurt_000000_000019_gtFine_instanceIds.png
frankfurt_000000_000019_gtFine_labelIds.png
frankfurt_000000_000019_gtFine_polygons.json
...
...
...
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)))

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

for segment_name in _SEGMENT_NAMES:
segment = dataset.create_segment(segment_name)
for image_path in glob(os.path.join(root_path, "leftImg8bit", segment_name, "*", "*.png")):
city = image_path.split("_", 1)[0]
image_prefix = image_path.rsplit("_", 1)[0]
label_dir = os.path.join(root_path, "gtFine", segment_name, city)
data = Data(image_path)
# get semantic mask and instance mask
label = data.label
label.semantic_mask = SemanticMask(
os.path.join(label_dir, f"{image_prefix}_gtFine_labelIds.png")
)
label.instance_mask = InstanceMask(
os.path.join(label_dir, f"{image_prefix}_gtFine_instanceIds.png")
)
# get polygons
polygons: List[LabeledPolygon] = []
with open(
os.path.join(label_dir, f"{image_prefix}_gtFine_polygons.json"),
encoding="utf-8",
) as fp:
objects = json.load(fp)["objects"]
for obj in objects:
polygons.append(LabeledPolygon(obj["polygon"], category=obj["label"]))
label.polygon = polygons

segment.append(data)
return dataset
11 changes: 0 additions & 11 deletions tensorbay/opendataset/CityscapesGTFine/__init__.py

This file was deleted.

29 changes: 0 additions & 29 deletions tensorbay/opendataset/CityscapesGTFine/catalog.json

This file was deleted.

87 changes: 0 additions & 87 deletions tensorbay/opendataset/CityscapesGTFine/loader.py

This file was deleted.

2 changes: 2 additions & 0 deletions tensorbay/opendataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from tensorbay.opendataset.CarConnection import CarConnection
from tensorbay.opendataset.CCPD import CCPD, CCPDGreen
from tensorbay.opendataset.CIHP import CIHP
from tensorbay.opendataset.Cityscapes import CityscapesGTFine
from tensorbay.opendataset.COCO2017 import COCO2017
from tensorbay.opendataset.CoinImage import CoinImage
from tensorbay.opendataset.CompCars import CompCars
Expand Down Expand Up @@ -80,6 +81,7 @@
"CCPD",
"CCPDGreen",
"CIHP",
"CityscapesGTFine",
"COCO2017",
"CoinImage",
"COVIDChestXRay",
Expand Down

0 comments on commit 1b9af5d

Please sign in to comment.