diff --git a/chainercv/datasets/__init__.py b/chainercv/datasets/__init__.py index 58b6d1b6a1..51738f01fd 100644 --- a/chainercv/datasets/__init__.py +++ b/chainercv/datasets/__init__.py @@ -1,3 +1,7 @@ +from chainercv.datasets.ade20k.ade20k_semantic_segmentation_dataset import ADE20KSemanticSegmentationDataset # NOQA +from chainercv.datasets.ade20k.ade20k_test_image_dataset import ADE20KTestImageDataset # NOQA +from chainercv.datasets.ade20k.ade20k_utils import ade20k_semantic_segmentation_label_colors # NOQA +from chainercv.datasets.ade20k.ade20k_utils import ade20k_semantic_segmentation_label_names # NOQA from chainercv.datasets.camvid.camvid_dataset import camvid_ignore_label_color # NOQA from chainercv.datasets.camvid.camvid_dataset import camvid_label_colors # NOQA from chainercv.datasets.camvid.camvid_dataset import camvid_label_names # NOQA diff --git a/chainercv/datasets/ade20k/__init__.py b/chainercv/datasets/ade20k/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/chainercv/datasets/ade20k/ade20k_semantic_segmentation_dataset.py b/chainercv/datasets/ade20k/ade20k_semantic_segmentation_dataset.py new file mode 100644 index 0000000000..2d2cd73320 --- /dev/null +++ b/chainercv/datasets/ade20k/ade20k_semantic_segmentation_dataset.py @@ -0,0 +1,75 @@ +import glob +import os + +import numpy as np + +from chainer import dataset +from chainercv.utils import read_image + +from chainercv.datasets.ade20k.ade20k_utils import get_ade20k + + +root = 'pfnet/chainercv/ade20k' +url = 'http://data.csail.mit.edu/places/ADEchallenge/ADEChallengeData2016.zip' + + +class ADE20KSemanticSegmentationDataset(dataset.DatasetMixin): + + """Semantic segmentation dataset for `ADE20K`_. + + This is ADE20K dataset distributed in MIT Scene Parsing Benchmark website. + It has 20,210 training images and 2,000 validation images. + + .. _`MIT Scene Parsing Benchmark`: http://sceneparsing.csail.mit.edu/ + + Args: + data_dir (string): Path to the dataset directory. The directory should + contain the :obj:`ADEChallengeData2016` directory. And that + directory should contain at least :obj:`images` and + :obj:`annotations` directries. If :obj:`auto` is given, the dataset + is automatically downloaded into + :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/ade20k`. + split ({'train', 'val'}): Select from dataset splits used in + MIT Scene Parsing Benchmark dataset (ADE20K). + + """ + + def __init__(self, data_dir='auto', split='train'): + if data_dir is 'auto': + data_dir = get_ade20k(root, url) + + if split == 'train' or split == 'val': + img_dir = os.path.join( + data_dir, 'ADEChallengeData2016', 'images', + 'training' if split == 'train' else 'validation') + label_dir = os.path.join( + data_dir, 'ADEChallengeData2016', 'annotations', + 'training' if split == 'train' else 'validation') + else: + raise ValueError( + 'Please give \'split\' argument with either \'train\' or ' + '\'val\'.') + + self.img_paths = sorted(glob.glob(os.path.join(img_dir, '*.jpg'))) + self.label_paths = sorted(glob.glob(os.path.join(label_dir, '*.png'))) + + def __len__(self): + return len(self.img_paths) + + def get_example(self, i): + """Returns the i-th example. + + Args: + i (int): The index of the example. + + Returns: + Returns a tuple consited of a color image and a label whose shapes + are (3, H, W) and (H, W), respectively. H and W are height and + width of the image. The dtype of the color image is + :obj:`numpy.float32` and the dtype of the label image is + :obj:`numpy.int32`. + + """ + img = read_image(self.img_paths[i]) + label = read_image(self.label_paths[i], dtype=np.int32, color=False)[0] + return img, label diff --git a/chainercv/datasets/ade20k/ade20k_test_image_dataset.py b/chainercv/datasets/ade20k/ade20k_test_image_dataset.py new file mode 100644 index 0000000000..92984fab9a --- /dev/null +++ b/chainercv/datasets/ade20k/ade20k_test_image_dataset.py @@ -0,0 +1,52 @@ +import glob +import os + +from chainer import dataset +from chainercv.utils import read_image + +from chainercv.datasets.ade20k.ade20k_utils import get_ade20k + + +root = 'pfnet/chainercv/ade20k' +url = 'http://data.csail.mit.edu/places/ADEchallenge/release_test.zip' + + +class ADE20KTestImageDataset(dataset.DatasetMixin): + + """Image dataset for test split of `ADE20K`_. + + This is an image dataset of test split in ADE20K dataset distributed at + MIT Scene Parsing Benchmark website. It has 3,352 test images. + + .. _`MIT Scene Parsing Benchmark`: http://sceneparsing.csail.mit.edu/ + + Args: + data_dir (string): Path to the dataset directory. The directory should + contain the :obj:`release_test` dir. If :obj:`auto` is given, the + dataset is automatically downloaded into + :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/ade20k`. + + """ + + def __init__(self, data_dir='auto'): + if data_dir is 'auto': + data_dir = get_ade20k(root, url) + img_dir = os.path.join(data_dir, 'release_test', 'testing') + self.img_paths = sorted(glob.glob(os.path.join(img_dir, '*.jpg'))) + + def __len__(self): + return len(self.img_paths) + + def get_example(self, i): + """Returns the i-th example. + + Args: + i (int): The index of the example. + + Returns: + Returns a color image whose shape is (3, H, W). H and W are height + and width of the image. The dtype of the image is + :obj:`numpy.float32`. + + """ + return read_image(self.img_paths[i]) diff --git a/chainercv/datasets/ade20k/ade20k_utils.py b/chainercv/datasets/ade20k/ade20k_utils.py new file mode 100644 index 0000000000..9ac702edfe --- /dev/null +++ b/chainercv/datasets/ade20k/ade20k_utils.py @@ -0,0 +1,321 @@ +# The values used here are copied from CSAILVision/sceneparsing: +# https://github.com/CSAILVision/sceneparsing + +import os + +from chainer.dataset import download +from chainercv import utils + + +def get_ade20k(root, url): + data_root = download.get_dataset_directory(root) + cache_path = utils.cached_download(url) + utils.extractall(cache_path, data_root, os.path.splitext(url)[1]) + return data_root + + +ade20k_semantic_segmentation_label_names = ( + 'wall', + 'edifice', + 'sky', + 'flooring', + 'tree', + 'ceiling', + 'route', + 'bed ', + 'window ', + 'grass', + 'cabinet', + 'pavement', + 'soul', + 'ground', + 'door', + 'table', + 'mount', + 'life', + 'pall', + 'chair', + 'motorcar', + 'water', + 'picture', + 'lounge', + 'shelf', + 'house', + 'sea', + 'mirror', + 'carpeting', + 'field', + 'armchair', + 'seat', + 'fencing', + 'desk', + 'stone', + 'press', + 'lamp', + 'tub', + 'rail', + 'cushion', + 'stand', + 'box', + 'pillar', + 'sign', + 'dresser', + 'counter', + 'sand', + 'sink', + 'skyscraper', + 'fireplace', + 'icebox', + 'stand', + 'path', + 'steps', + 'runway', + 'vitrine', + 'table', + 'pillow', + 'screen', + 'staircase', + 'river', + 'span', + 'bookcase', + 'screen', + 'table', + 'throne', + 'flower', + 'book', + 'hill', + 'bench', + 'countertop', + 'stove', + 'tree', + 'island', + 'system', + 'chair', + 'boat', + 'bar', + 'machine', + 'shanty', + 'vehicle', + 'towel', + 'source', + 'motortruck', + 'tower', + 'pendent', + 'sunblind', + 'lamp', + 'kiosk', + 'box', + 'plane', + 'track', + 'clothes', + 'pole', + 'soil', + 'handrail', + 'stairway', + 'hassock', + 'bottle', + 'sideboard', + 'card', + 'stage', + 'van', + 'ship', + 'fountain', + 'transporter', + 'canopy', + 'machine', + 'toy', + 'natatorium', + 'stool', + 'cask', + 'handbasket', + 'falls', + 'shelter', + 'bag', + 'motorbike', + 'cradle', + 'oven', + 'ball', + 'food', + 'stair', + 'tank', + 'marque', + 'oven', + 'flowerpot', + 'fauna', + 'cycle ', + 'lake', + 'machine', + 'screen', + 'cover', + 'sculpture', + 'hood', + 'sconce', + 'vase', + 'stoplight', + 'tray', + 'bin', + 'fan', + 'dock', + 'screen', + 'plate', + 'device', + 'board', + 'shower', + 'radiator', + 'glass', + 'clock', + 'flag' +) + +ade20k_semantic_segmentation_label_colors = ( + (120, 120, 120), + (180, 120, 120), + (6, 230, 230), + (80, 50, 50), + (4, 200, 3), + (120, 120, 80), + (140, 140, 140), + (204, 5, 255), + (230, 230, 230), + (4, 250, 7), + (224, 5, 255), + (235, 255, 7), + (150, 5, 61), + (120, 120, 70), + (8, 255, 51), + (255, 6, 82), + (143, 255, 140), + (204, 255, 4), + (255, 51, 7), + (204, 70, 3), + (0, 102, 200), + (61, 230, 250), + (255, 6, 51), + (11, 102, 255), + (255, 7, 71), + (255, 9, 224), + (9, 7, 230), + (220, 220, 220), + (255, 9, 92), + (112, 9, 255), + (8, 255, 214), + (7, 255, 224), + (255, 184, 6), + (10, 255, 71), + (255, 41, 10), + (7, 255, 255), + (224, 255, 8), + (102, 8, 255), + (255, 61, 6), + (255, 194, 7), + (255, 122, 8), + (0, 255, 20), + (255, 8, 41), + (255, 5, 153), + (6, 51, 255), + (235, 12, 255), + (160, 150, 20), + (0, 163, 255), + (140, 140, 140), + (250, 10, 15), + (20, 255, 0), + (31, 255, 0), + (255, 31, 0), + (255, 224, 0), + (153, 255, 0), + (0, 0, 255), + (255, 71, 0), + (0, 235, 255), + (0, 173, 255), + (31, 0, 255), + (11, 200, 200), + (255, 82, 0), + (0, 255, 245), + (0, 61, 255), + (0, 255, 112), + (0, 255, 133), + (255, 0, 0), + (255, 163, 0), + (255, 102, 0), + (194, 255, 0), + (0, 143, 255), + (51, 255, 0), + (0, 82, 255), + (0, 255, 41), + (0, 255, 173), + (10, 0, 255), + (173, 255, 0), + (0, 255, 153), + (255, 92, 0), + (255, 0, 255), + (255, 0, 245), + (255, 0, 102), + (255, 173, 0), + (255, 0, 20), + (255, 184, 184), + (0, 31, 255), + (0, 255, 61), + (0, 71, 255), + (255, 0, 204), + (0, 255, 194), + (0, 255, 82), + (0, 10, 255), + (0, 112, 255), + (51, 0, 255), + (0, 194, 255), + (0, 122, 255), + (0, 255, 163), + (255, 153, 0), + (0, 255, 10), + (255, 112, 0), + (143, 255, 0), + (82, 0, 255), + (163, 255, 0), + (255, 235, 0), + (8, 184, 170), + (133, 0, 255), + (0, 255, 92), + (184, 0, 255), + (255, 0, 31), + (0, 184, 255), + (0, 214, 255), + (255, 0, 112), + (92, 255, 0), + (0, 224, 255), + (112, 224, 255), + (70, 184, 160), + (163, 0, 255), + (153, 0, 255), + (71, 255, 0), + (255, 0, 163), + (255, 204, 0), + (255, 0, 143), + (0, 255, 235), + (133, 255, 0), + (255, 0, 235), + (245, 0, 255), + (255, 0, 122), + (255, 245, 0), + (10, 190, 212), + (214, 255, 0), + (0, 204, 255), + (20, 0, 255), + (255, 255, 0), + (0, 153, 255), + (0, 41, 255), + (0, 255, 204), + (41, 0, 255), + (41, 255, 0), + (173, 0, 255), + (0, 245, 255), + (71, 0, 255), + (122, 0, 255), + (0, 255, 184), + (0, 92, 255), + (184, 255, 0), + (0, 133, 255), + (255, 214, 0), + (25, 194, 194), + (102, 255, 0), + (92, 0, 255) +) diff --git a/docs/source/reference/datasets.rst b/docs/source/reference/datasets.rst index ba3f9b5f18..e305719718 100644 --- a/docs/source/reference/datasets.rst +++ b/docs/source/reference/datasets.rst @@ -18,6 +18,17 @@ TransformDataset ~~~~~~~~~~~~~~~~ .. autoclass:: TransformDataset +ADE20K +------ + +ADE20KSemanticSegmentationDataset +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. autoclass:: ADE20KSemanticSegmentationDataset + +ADE20KTestImageDataset +~~~~~~~~~~~~~~~~~~~~~~ +.. autoclass:: ADE20KTestImageDataset + CamVid ------ diff --git a/tests/datasets_tests/ade20k_tests/test_ade20k.py b/tests/datasets_tests/ade20k_tests/test_ade20k.py new file mode 100644 index 0000000000..bb2a8dfc95 --- /dev/null +++ b/tests/datasets_tests/ade20k_tests/test_ade20k.py @@ -0,0 +1,43 @@ +import unittest + +from chainer import testing +from chainer.testing import attr +import numpy as np + +from chainercv.datasets import ade20k_semantic_segmentation_label_names +from chainercv.datasets import ADE20KSemanticSegmentationDataset +from chainercv.datasets import ADE20KTestImageDataset +from chainercv.utils import assert_is_semantic_segmentation_dataset +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + + +@testing.parameterize( + {'split': 'train'}, + {'split': 'val'}, +) +class TestADE20KSemanticSegmentationDataset(unittest.TestCase): + + def setUp(self): + self.dataset = ADE20KSemanticSegmentationDataset(split=self.split) + + @attr.slow + def test_ade20k_dataset(self): + assert_is_semantic_segmentation_dataset( + self.dataset, len(ade20k_semantic_segmentation_label_names), + n_example=10) + + +class TestADE20KTestImageDataset(unittest.TestCase): + + def setUp(self): + self.dataset = ADE20KTestImageDataset() + + @attr.slow + def test_ade20k_dataset(self): + indices = np.random.permutation(np.arange(len(self.dataset))) + for i in indices[:10]: + img = self.dataset[i] + assert_is_image(img, color=True) + + +testing.run_module(__name__, __file__)