-
Notifications
You must be signed in to change notification settings - Fork 5
/
datasets.py
230 lines (193 loc) · 8.45 KB
/
datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.
# pylint: skip-file
"""Return training and evaluation/test datasets from config files."""
import jax
import tensorflow as tf
import tensorflow_datasets as tfds
def get_data_scaler(config):
"""Data normalizer. Assume data are always in [0, 1]."""
if config.data.centered:
# Rescale to [-1, 1]
return lambda x: x * 2. - 1.
else:
return lambda x: x
def get_data_inverse_scaler(config):
"""Inverse data normalizer."""
if config.data.centered:
# Rescale [-1, 1] to [0, 1]
return lambda x: (x + 1.) / 2.
else:
return lambda x: x
def crop_resize(image, resolution):
"""Crop and resize an image to the given resolution."""
crop = tf.minimum(tf.shape(image)[0], tf.shape(image)[1])
h, w = tf.shape(image)[0], tf.shape(image)[1]
image = image[(h - crop) // 2:(h + crop) // 2,
(w - crop) // 2:(w + crop) // 2]
image = tf.image.resize(
image,
size=(resolution, resolution),
antialias=True,
method=tf.image.ResizeMethod.BICUBIC)
return tf.cast(image, tf.uint8)
def resize_small(image, resolution):
"""Shrink an image to the given resolution."""
h, w = image.shape[0], image.shape[1]
ratio = resolution / min(h, w)
h = tf.round(h * ratio, tf.int32)
w = tf.round(w * ratio, tf.int32)
return tf.image.resize(image, [h, w], antialias=True)
def central_crop(image, size):
"""Crop the center of an image to the given size."""
top = (image.shape[0] - size) // 2
left = (image.shape[1] - size) // 2
return tf.image.crop_to_bounding_box(image, top, left, size, size)
def get_dataset(config, additional_dim=None, uniform_dequantization=False, evaluation=False):
"""Create data loaders for training and evaluation.
Args:
config: A ml_collection.ConfigDict parsed from config files.
additional_dim: An integer or `None`. If present, add one additional dimension to the output data,
which equals the number of steps jitted together.
uniform_dequantization: If `True`, add uniform dequantization to images.
evaluation: If `True`, fix number of epochs to 1.
Returns:
train_ds, eval_ds, dataset_builder.
"""
# Compute batch size for this worker.
batch_size = config.training.batch_size if not evaluation else config.eval.batch_size
if batch_size % jax.device_count() != 0:
raise ValueError(f'Batch sizes ({batch_size} must be divided by'
f'the number of devices ({jax.device_count()})')
per_device_batch_size = batch_size // jax.device_count()
# Reduce this when image resolution is too large and data pointer is stored
shuffle_buffer_size = 10000
prefetch_size = tf.data.experimental.AUTOTUNE
num_epochs = None if not evaluation else 1
# Create additional data dimension when jitting multiple steps together
if additional_dim is None:
batch_dims = [jax.local_device_count(), per_device_batch_size]
else:
batch_dims = [jax.local_device_count(), additional_dim, per_device_batch_size]
# Create dataset builders for each dataset.
if config.data.dataset == 'CIFAR10':
dataset_builder = tfds.builder('cifar10')
train_split_name = 'train'
eval_split_name = 'test'
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
return tf.image.resize(img, [config.data.image_size, config.data.image_size], antialias=True)
elif config.data.dataset == 'MNIST':
dataset_builder = tfds.builder('mnist')
train_split_name = 'train'
eval_split_name = 'test'
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
return tf.image.resize(img, [config.data.image_size, config.data.image_size], antialias=True)
elif config.data.dataset == 'SVHN':
dataset_builder = tfds.builder('svhn_cropped')
train_split_name = 'train'
eval_split_name = 'test'
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
return tf.image.resize(img, [config.data.image_size, config.data.image_size], antialias=True)
elif config.data.dataset == 'CELEBA':
dataset_builder = tfds.builder('celeb_a')
train_split_name = 'train'
eval_split_name = 'validation'
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
img = central_crop(img, 140)
img = resize_small(img, config.data.image_size)
return img
elif config.data.dataset == 'ImageNet':
size = {
32: '32x32',
64: '64x64'
}[config.data.image_size]
dataset_builder = tfds.builder(f'downsampled_imagenet/{size}')
train_split_name = 'train'
eval_split_name = 'validation'
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
return img
elif config.data.dataset == 'LSUN':
dataset_builder = tfds.builder(f'lsun/{config.data.category}')
train_split_name = 'train'
eval_split_name = 'validation'
if config.data.image_size == 128:
def resize_op(img):
img = tf.image.convert_image_dtype(img, tf.float32)
img = resize_small(img, config.data.image_size)
img = central_crop(img, config.data.image_size)
return img
else:
def resize_op(img):
img = crop_resize(img, config.data.image_size)
img = tf.image.convert_image_dtype(img, tf.float32)
return img
elif config.data.dataset in ('FFHQ', 'CelebAHQ'):
dataset_builder = tf.data.TFRecordDataset(config.data.tfrecords_path)
train_split_name = eval_split_name = 'train'
else:
raise NotImplementedError(
f'Dataset {config.data.dataset} not yet supported.')
# Customize preprocess functions for each dataset.
if config.data.dataset in ('FFHQ', 'CelebAHQ'):
def preprocess_fn(d):
sample = tf.io.parse_single_example(d, features={
'shape': tf.io.FixedLenFeature([3], tf.int64),
'data': tf.io.FixedLenFeature([], tf.string)})
data = tf.io.decode_raw(sample['data'], tf.uint8)
data = tf.reshape(data, sample['shape'])
data = tf.transpose(data, (1, 2, 0))
img = tf.image.convert_image_dtype(data, tf.float32)
if config.data.random_flip and not evaluation:
img = tf.image.random_flip_left_right(img)
if uniform_dequantization:
img = (tf.random.uniform(img.shape, dtype=tf.float32) + img * 255.) / 256.
return dict(image=img, label=None)
else:
def preprocess_fn(d):
"""Basic preprocessing function scales data to [0, 1) and randomly flips."""
img = resize_op(d['image'])
if config.data.random_flip and not evaluation:
img = tf.image.random_flip_left_right(img)
if uniform_dequantization:
# img = (tf.random.uniform(img.shape, dtype=tf.float32) + img * 255.) / 256.
img = (tf.random.uniform((config.data.image_size, config.data.image_size,
config.data.num_channels), dtype=tf.float32) + img * 255.) / 256.
return dict(image=img, label=d.get('label', None))
def create_dataset(dataset_builder, split):
dataset_options = tf.data.Options()
dataset_options.experimental_optimization.map_parallelization = True
dataset_options.experimental_threading.private_threadpool_size = 48
dataset_options.experimental_threading.max_intra_op_parallelism = 1
read_config = tfds.ReadConfig(options=dataset_options)
if isinstance(dataset_builder, tfds.core.DatasetBuilder):
dataset_builder.download_and_prepare()
ds = dataset_builder.as_dataset(
split=split, shuffle_files=True, read_config=read_config)
else:
ds = dataset_builder.with_options(dataset_options)
ds = ds.repeat(count=num_epochs)
ds = ds.shuffle(shuffle_buffer_size)
ds = ds.map(preprocess_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)
for batch_size in reversed(batch_dims):
ds = ds.batch(batch_size, drop_remainder=True)
return ds.prefetch(prefetch_size)
train_ds = create_dataset(dataset_builder, train_split_name)
eval_ds = create_dataset(dataset_builder, eval_split_name)
return train_ds, eval_ds, dataset_builder