This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Support TFDS format for segmentation #1005
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2018 The Blueoil Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================= | ||
import numpy as np | ||
import tensorflow_datasets as tfds | ||
|
||
|
||
class SegmentationBuilder(tfds.core.GeneratorBasedBuilder): | ||
""" | ||
A custom TFDS builder for segmentation dataset. | ||
This class loads data from existing dataset classes and | ||
generate TFDS formatted dataset which is equivalent to the original one. | ||
See also: https://www.tensorflow.org/datasets/add_dataset | ||
""" | ||
|
||
VERSION = tfds.core.Version("0.1.0") | ||
|
||
def __init__(self, dataset_name, dataset_class=None, dataset_kwargs=None, **kwargs): | ||
self.name = dataset_name | ||
self.dataset_class = dataset_class | ||
self.dataset_kwargs = dataset_kwargs | ||
super().__init__(**kwargs) | ||
|
||
def _info(self): | ||
return tfds.core.DatasetInfo( | ||
builder=self, | ||
description="Custom TFDS dataset for segmentation", | ||
features=tfds.features.FeaturesDict({ | ||
"image": tfds.features.Image(), | ||
"label": tfds.features.ClassLabel(), | ||
"segmentation_mask": tfds.features.Image(shape=(None, None, 1)), | ||
}), | ||
) | ||
|
||
def _split_generators(self, dl_manager): | ||
self.info.features["label"].names = self.dataset_class(**self.dataset_kwargs).classes | ||
|
||
predefined_names = { | ||
"train": tfds.Split.TRAIN, | ||
"validation": tfds.Split.VALIDATION, | ||
"test": tfds.Split.TEST, | ||
} | ||
|
||
splits = [] | ||
for subset in self.dataset_class.available_subsets: | ||
dataset = self.dataset_class(subset=subset, **self.dataset_kwargs) | ||
splits.append( | ||
tfds.core.SplitGenerator( | ||
name=predefined_names[subset], | ||
num_shards=self._num_shards(dataset), | ||
gen_kwargs=dict(dataset=dataset) | ||
) | ||
) | ||
|
||
return splits | ||
|
||
def _generate_examples(self, dataset): | ||
for i, (image, segmentation_mask) in enumerate(dataset): | ||
yield i, { | ||
"image": image, | ||
"segmentation_mask": np.expand_dims(segmentation_mask, axis=2), | ||
"label": -1, # dummy label | ||
} | ||
|
||
def _num_shards(self, dataset): | ||
"""Decide a number of shards so as not the size of each shard exceeds 256MiB""" | ||
max_shard_size = 256 * 1024 * 1024 # 256MiB | ||
total_size = sum((image.nbytes + mask.nbytes) for image, mask in dataset) | ||
return (total_size + max_shard_size - 1) // max_shard_size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/unit/fixtures/configs/for_build_tfds_segmentation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2018 The Blueoil Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================= | ||
from easydict import EasyDict | ||
import tensorflow as tf | ||
|
||
from blueoil.common import Tasks | ||
from blueoil.networks.segmentation.lm_segnet_v1 import LmSegnetV1Quantize | ||
from blueoil.datasets.camvid import CamvidCustom | ||
from blueoil.data_processor import Sequence | ||
from blueoil.pre_processor import ( | ||
Resize, | ||
DivideBy255, | ||
) | ||
from blueoil.data_augmentor import ( | ||
Brightness, | ||
Color, | ||
Contrast, | ||
FlipLeftRight, | ||
Hue, | ||
) | ||
from blueoil.quantizations import ( | ||
binary_mean_scaling_quantizer, | ||
linear_mid_tread_half_quantizer, | ||
) | ||
|
||
|
||
class SegmentationDataset(CamvidCustom): | ||
extend_dir = "camvid_custom" | ||
validation_extend_dir = "camvid_custom" | ||
|
||
|
||
IS_DEBUG = False | ||
|
||
NETWORK_CLASS = LmSegnetV1Quantize | ||
DATASET_CLASS = SegmentationDataset | ||
|
||
IMAGE_SIZE = [80, 120] | ||
BATCH_SIZE = 8 | ||
DATA_FORMAT = "NHWC" | ||
TASK = Tasks.SEMANTIC_SEGMENTATION | ||
CLASSES = DATASET_CLASS(subset="train", batch_size=1).classes | ||
|
||
MAX_STEPS = 1 | ||
SAVE_CHECKPOINT_STEPS = 1 | ||
KEEP_CHECKPOINT_MAX = 5 | ||
TEST_STEPS = 100 | ||
SUMMARISE_STEPS = 100 | ||
|
||
# distributed training | ||
IS_DISTRIBUTION = False | ||
|
||
# pretrain | ||
IS_PRETRAIN = False | ||
PRETRAIN_VARS = [] | ||
PRETRAIN_DIR = "" | ||
PRETRAIN_FILE = "" | ||
|
||
PRE_PROCESSOR = Sequence([ | ||
Resize(size=IMAGE_SIZE), | ||
DivideBy255(), | ||
]) | ||
POST_PROCESSOR = None | ||
|
||
NETWORK = EasyDict() | ||
NETWORK.OPTIMIZER_CLASS = tf.compat.v1.train.AdamOptimizer | ||
NETWORK.OPTIMIZER_KWARGS = {"learning_rate": 0.001} | ||
NETWORK.IMAGE_SIZE = IMAGE_SIZE | ||
NETWORK.BATCH_SIZE = BATCH_SIZE | ||
NETWORK.DATA_FORMAT = DATA_FORMAT | ||
NETWORK.ACTIVATION_QUANTIZER = linear_mid_tread_half_quantizer | ||
NETWORK.ACTIVATION_QUANTIZER_KWARGS = { | ||
'bit': 2, | ||
'max_value': 2 | ||
} | ||
NETWORK.WEIGHT_QUANTIZER = binary_mean_scaling_quantizer | ||
NETWORK.WEIGHT_QUANTIZER_KWARGS = {} | ||
|
||
DATASET = EasyDict() | ||
DATASET.BATCH_SIZE = BATCH_SIZE | ||
DATASET.DATA_FORMAT = DATA_FORMAT | ||
DATASET.PRE_PROCESSOR = PRE_PROCESSOR | ||
DATASET.AUGMENTOR = Sequence([ | ||
Brightness((0.75, 1.25)), | ||
Color((0.75, 1.25)), | ||
Contrast((0.75, 1.25)), | ||
FlipLeftRight(), | ||
Hue((-10, 10)), | ||
]) | ||
DATASET.TFDS_KWARGS = { | ||
"name": "tfds_segmentation", | ||
"data_dir": "tmp/tests/datasets", | ||
"image_size": IMAGE_SIZE, | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the feature
label
is used for having a list of classes but not needed for every example. Is my understanding correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tfujiwar
Yes! Your understanding is correct! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!