|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2019 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Shapes3D dataset.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import tempfile |
| 23 | + |
| 24 | +import h5py |
| 25 | +import numpy as np |
| 26 | +from six import moves |
| 27 | +import tensorflow as tf |
| 28 | + |
| 29 | +import tensorflow_datasets.public_api as tfds |
| 30 | + |
| 31 | +_CITATION = """\ |
| 32 | +@misc{3dshapes18, |
| 33 | + title={3D Shapes Dataset}, |
| 34 | + author={Burgess, Chris and Kim, Hyunjik}, |
| 35 | + howpublished={https://github.com/deepmind/3dshapes-dataset/}, |
| 36 | + year={2018} |
| 37 | +} |
| 38 | +""" |
| 39 | + |
| 40 | +_URL = ("https://storage.googleapis.com/3d-shapes/3dshapes.h5") |
| 41 | + |
| 42 | +_DESCRIPTION = """\ |
| 43 | +3dshapes is a dataset of 3D shapes procedurally generated from 6 ground truth |
| 44 | +independent latent factors. These factors are *floor colour*, *wall colour*, *object colour*, |
| 45 | +*scale*, *shape* and *orientation*. |
| 46 | +
|
| 47 | +All possible combinations of these latents are present exactly once, generating N = 480000 total images. |
| 48 | +
|
| 49 | +### Latent factor values |
| 50 | +
|
| 51 | +* floor hue: 10 values linearly spaced in [0, 1] |
| 52 | +* wall hue: 10 values linearly spaced in [0, 1] |
| 53 | +* object hue: 10 values linearly spaced in [0, 1] |
| 54 | +* scale: 8 values linearly spaced in [0, 1] |
| 55 | +* shape: 4 values in [0, 1, 2, 3] |
| 56 | +* orientation: 15 values linearly spaced in [-30, 30] |
| 57 | +
|
| 58 | +We varied one latent at a time (starting from orientation, then shape, etc), and sequentially stored the images in fixed order in the `images` array. The corresponding values of the factors are stored in the same order in the `labels` array. |
| 59 | +""" |
| 60 | + |
| 61 | + |
| 62 | +class Shapes3d(tfds.core.GeneratorBasedBuilder): |
| 63 | + """Shapes3d data set.""" |
| 64 | + |
| 65 | + VERSION = tfds.core.Version("0.1.0") |
| 66 | + |
| 67 | + def _info(self): |
| 68 | + return tfds.core.DatasetInfo( |
| 69 | + builder=self, |
| 70 | + description=_DESCRIPTION, |
| 71 | + features=tfds.features.FeaturesDict({ |
| 72 | + "image": |
| 73 | + tfds.features.Image(shape=(64, 64, 3)), |
| 74 | + "label_floor_hue": |
| 75 | + tfds.features.ClassLabel(num_classes=10), |
| 76 | + "label_wall_hue": |
| 77 | + tfds.features.ClassLabel(num_classes=10), |
| 78 | + "label_object_hue": |
| 79 | + tfds.features.ClassLabel(num_classes=10), |
| 80 | + "label_scale": |
| 81 | + tfds.features.ClassLabel(num_classes=8), |
| 82 | + "label_shape": |
| 83 | + tfds.features.ClassLabel(num_classes=4), |
| 84 | + "label_orientation": |
| 85 | + tfds.features.ClassLabel(num_classes=15), |
| 86 | + "value_floor_hue": |
| 87 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 88 | + "value_wall_hue": |
| 89 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 90 | + "value_object_hue": |
| 91 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 92 | + "value_scale": |
| 93 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 94 | + "value_shape": |
| 95 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 96 | + "value_orientation": |
| 97 | + tfds.features.Tensor(shape=[], dtype=tf.float32), |
| 98 | + }), |
| 99 | + urls=["https://github.com/deepmind/3d-shapes"], |
| 100 | + citation=_CITATION, |
| 101 | + ) |
| 102 | + |
| 103 | + def _split_generators(self, dl_manager): |
| 104 | + filepath = dl_manager.download(_URL) |
| 105 | + |
| 106 | + # There is no predefined train/val/test split for this dataset. |
| 107 | + return [ |
| 108 | + tfds.core.SplitGenerator( |
| 109 | + name=tfds.Split.TRAIN, |
| 110 | + num_shards=1, |
| 111 | + gen_kwargs=dict(filepath=filepath)), |
| 112 | + ] |
| 113 | + |
| 114 | + def _generate_examples(self, filepath): |
| 115 | + """Generate examples for the Shapes3d dataset. |
| 116 | +
|
| 117 | + Args: |
| 118 | + filepath: path to the Shapes3d hdf5 file. |
| 119 | +
|
| 120 | + Yields: |
| 121 | + Dictionaries with images and the different labels. |
| 122 | + """ |
| 123 | + # Simultaneously iterating through the different data sets in the hdf5 |
| 124 | + # file will be slow with a single file. Instead, we first load everything |
| 125 | + # into memory before yielding the samples. |
| 126 | + image_array, values_array = _load_data(filepath) |
| 127 | + |
| 128 | + # We need to calculate the class labels from the float values in the file. |
| 129 | + labels_array = np.zeros_like(values_array, dtype=np.int64) |
| 130 | + for i in range(values_array.shape[1]): |
| 131 | + labels_array[:, i] = _discretize(values_array[:, i]) |
| 132 | + |
| 133 | + for image, labels, values in moves.zip(image_array, labels_array, |
| 134 | + values_array): |
| 135 | + yield { |
| 136 | + "image": image, |
| 137 | + "label_floor_hue": labels[0], |
| 138 | + "label_wall_hue": labels[1], |
| 139 | + "label_object_hue": labels[2], |
| 140 | + "label_scale": labels[3], |
| 141 | + "label_shape": labels[4], |
| 142 | + "label_orientation": labels[5], |
| 143 | + "value_floor_hue": values[0], |
| 144 | + "value_wall_hue": values[1], |
| 145 | + "value_object_hue": values[2], |
| 146 | + "value_scale": values[3], |
| 147 | + "value_shape": values[4], |
| 148 | + "value_orientation": values[5], |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +def _load_data(filepath): |
| 153 | + """Loads the images and latent values into Numpy arrays.""" |
| 154 | + with h5py.File(filepath, "r") as h5dataset: |
| 155 | + image_array = np.array(h5dataset["images"]) |
| 156 | + # The 'label' data set in the hdf5 file actually contains the float values |
| 157 | + # and not the class labels. |
| 158 | + values_array = np.array(h5dataset["labels"]) |
| 159 | + return image_array, values_array |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +def _discretize(a): |
| 165 | + """Discretizes array values to class labels.""" |
| 166 | + arr = np.asarray(a) |
| 167 | + index = np.argsort(arr) |
| 168 | + inverse_index = np.zeros(arr.size, dtype=np.intp) |
| 169 | + inverse_index[index] = np.arange(arr.size, dtype=np.intp) |
| 170 | + arr = arr[index] |
| 171 | + obs = np.r_[True, arr[1:] != arr[:-1]] |
| 172 | + return obs.cumsum()[inverse_index] - 1 |
0 commit comments