Repository navigation
Expand file tree
/
Copy pathdatasets.py
More file actions
265 lines (222 loc) · 9.08 KB
/
Copy pathdatasets.py
File metadata and controls
265 lines (222 loc) · 9.08 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import numpy as np
import jax
import tensorflow as tf
import tensorflow_datasets as tfds
import torch.utils.data
# flow-specific code
import torchvision.transforms.functional as F
import torchvision as tv
import torchvision.transforms as tr
from torchvision.datasets import MNIST
import torchvision.transforms as transforms
def logit_transform(image, lambd=1e-6):
image = lambd + (1 - 2 * lambd) * image
return torch.log(image) - torch.log1p(-image)
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, uniform_dequantization=False, evaluation=False):
"""Create data loaders for training and evaluation.
Args:
config: A ml_collection.ConfigDict parsed from config files.
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()})')
# 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 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 == '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.
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)
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
def get_dataset_for_flow(config, uniform_dequantization=False):
"""
hello
:param config:
:param uniform_dequantization:
:param evaluation:
:return:
"""
data_dir = '/atlas/u/kechoi/time-score-dre/'
train_transform = test_transform = transforms.Compose([
transforms.Resize(config.data.image_size),
transforms.ToTensor()
])
dataset = MNIST(os.path.join(data_dir, 'datasets', 'mnist'),
train=True,
download=True,
transform=train_transform)
# subset to first 50K examples for train
train_indices = np.arange(50000)
train_ds = torch.utils.data.Subset(dataset, train_indices)
eval_ds = torch.utils.data.Subset(dataset, np.arange(50000, 60000))
# eval_ds = MNIST(os.path.join(data_dir, 'datasets', 'mnist_test'),
# train=False, download=True,
# transform=test_transform)
# TODO: not set up for actual evaluation yet! this is just returning the validation set
train_ds = torch.utils.data.DataLoader(
train_ds, config.training.batch_size,
shuffle=True, num_workers=2, drop_last=False)
eval_ds = torch.utils.data.DataLoader(
eval_ds, config.eval.batch_size,
shuffle=False, num_workers=2,
drop_last=False)
return train_ds, eval_ds
def get_test_set_for_flow(config):
data_dir = '/atlas/u/kechoi/time-score-dre/'
test_transform = transforms.Compose([
transforms.Resize(config.data.image_size),
transforms.ToTensor()
])
eval_ds = MNIST(os.path.join(data_dir, 'datasets', 'mnist_test'),
train=False, download=True,
transform=test_transform)
eval_ds = torch.utils.data.DataLoader(
eval_ds, config.eval.batch_size,
shuffle=False, num_workers=2,
drop_last=False)
return eval_ds