Skip to content

Commit

Permalink
feat(opendataset): add dataloader for VOC2012Segmentation dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
linjiX committed Aug 19, 2021
1 parent aec7675 commit 7f92c92
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tensorbay/opendataset/VOC2012Segmentation/__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 VOC2012Segmentation."""

from .loader import VOC2012Segmentation

__all__ = ["VOC2012Segmentation"]
95 changes: 95 additions & 0 deletions tensorbay/opendataset/VOC2012Segmentation/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"SEMANTIC_MASK": {
"categories": [
{
"name": "background",
"categoryId": 0
},
{
"name": "aeroplane",
"categoryId": 1
},
{
"name": "bicycle",
"categoryId": 2
},
{
"name": "bird",
"categoryId": 3
},
{
"name": "boat",
"categoryId": 4
},
{
"name": "bottle",
"categoryId": 5
},
{
"name": "bus",
"categoryId": 6
},
{
"name": "car",
"categoryId": 7
},
{
"name": "cat",
"categoryId": 8
},
{
"name": "chair",
"categoryId": 9
},
{
"name": "cow",
"categoryId": 10
},
{
"name": "diningtable",
"categoryId": 11
},
{
"name": "dog",
"categoryId": 12
},
{
"name": "horse",
"categoryId": 13
},
{
"name": "motorbike",
"categoryId": 14
},
{
"name": "person",
"categoryId": 15
},
{
"name": "pottedplant",
"categoryId": 16
},
{
"name": "sheep",
"categoryId": 17
},
{
"name": "sofa",
"categoryId": 18
},
{
"name": "train",
"categoryId": 19
},
{
"name": "tvmonitor",
"categoryId": 20
},
{
"name": "void",
"categoryId": 255
}
]
},
"INSTANCE_MASK": {}
}
70 changes: 70 additions & 0 deletions tensorbay/opendataset/VOC2012Segmentation/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/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

_SEGMENT_NAMES = ("train", "val")
DATASET_NAME = "VOC2012Segmentation"


def VOC2012Segmentation(path: str) -> Dataset:
"""Dataloader of the 'VOC2012Segmentation'_ dataset.
.. _VOC2012Segmentation: http://host.robots.ox.ac.uk/pascal/VOC/voc2012/
The file structure should be like::
<path>/
JPEGImages/
<image_name>.jpg
...
SegmentationClass/
<mask_name>.png
...
SegmentationObject/
<mask_name>.png
...
ImageSets/
Segmentation/
train.txt
val.txt
...
...
...
Arguments:
path: The root directory of the dataset.
Returns:
Loaded :class: `~tensorbay.dataset.dataset.Dataset` instance.
"""
root_path = os.path.abspath(os.path.expanduser(path))

image_path = os.path.join(root_path, "JPEGImages")
semantic_mask_path = os.path.join(root_path, "SegmentationClass")
instance_mask_path = os.path.join(root_path, "SegmentationObject")
image_set_path = os.path.join(root_path, "ImageSets", "Segmentation")

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)
with open(os.path.join(image_set_path, f"{segment_name}.txt")) as fp:
for stem in fp:
stem = stem.strip()
data = Data(os.path.join(image_path, f"{stem}.jpg"))
label = data.label
label.semantic_mask = SemanticMask(os.path.join(semantic_mask_path, f"{stem}.png"))
label.instance_mask = InstanceMask(os.path.join(instance_mask_path, f"{stem}.png"))

segment.append(data)

return dataset
2 changes: 2 additions & 0 deletions tensorbay/opendataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from .UAVDT import UAVDT
from .VOC2012ActionClassification import VOC2012ActionClassification
from .VOC2012Detection import VOC2012Detection
from .VOC2012Segmentation import VOC2012Segmentation
from .WIDER_FACE import WIDER_FACE

__all__ = [
Expand Down Expand Up @@ -100,4 +101,5 @@
"COVID_CT",
"VOC2012Detection",
"VOC2012ActionClassification",
"VOC2012Segmentation",
]

0 comments on commit 7f92c92

Please sign in to comment.