-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasource.py
executable file
·303 lines (232 loc) · 9.82 KB
/
datasource.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Code for loading data.
lifted mostly from aditya-grover's UAE project
"""
import numpy as np
import random
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow.python.platform import flags
FLAGS = flags.FLAGS
class Datasource(object):
def __init__(self, sess):
self.sess = sess
self.seed = FLAGS.seed
tf.set_random_seed(self.seed)
np.random.seed(self.seed)
self.batch_size = FLAGS.batch_size
# minor changes
if FLAGS.datasource == 'mnist' or FLAGS.datasource == 'omniglot2mnist':
self.target_dataset = 'mnist'
self.TRAIN_FILE = 'mnist_train.tfrecords'
self.VALID_FILE = 'mnist_valid.tfrecords'
self.TEST_FILE = 'mnist_test.tfrecords'
self.input_dim = 784
self.num_classes = 10
self.dtype = tf.float32
self.preprocess = self._preprocess_mnist
self.get_dataset = self.get_tf_dataset
elif FLAGS.datasource == 'BinaryMNIST':
# added BinaryMNIST for testing purposes
self.target_dataset = 'BinaryMNIST'
self.TRAIN_FILE = 'bin_mnist_train.tfrecords'
self.VALID_FILE = 'bin_mnist_valid.tfrecords'
self.TEST_FILE = 'bin_mnist_test.tfrecords'
self.input_dim = 784
self.num_classes = 10
self.dtype = tf.float32
self.preprocess = self._preprocess_binary_mnist
self.get_dataset = self.get_binary_tf_dataset
elif FLAGS.datasource == 'random':
# added BinaryMNIST for testing purposes
self.target_dataset = 'random'
self.TRAIN_FILE = 'rand_bits_train.tfrecords'
self.VALID_FILE = 'rand_bits_valid.tfrecords'
self.TEST_FILE = 'rand_bits_test.tfrecords'
self.input_dim = 100
self.num_classes = 10
self.dtype = tf.float32
# we can re-use the functions for BinaryMNIST since these also don't have class labels
self.preprocess = self._preprocess_binary_mnist
self.get_dataset = self.get_binary_tf_dataset
elif FLAGS.datasource == 'omniglot':
self.target_dataset = 'omniglot'
self.TRAIN_FILE = 'omniglot_train.tfrecords'
self.VALID_FILE = 'omniglot_valid.tfrecords'
self.TEST_FILE = 'omniglot_test.tfrecords'
self.input_dim = 784
self.num_classes = 50
self.dtype = tf.float32
self.preprocess = self._preprocess_omniglot
self.get_dataset = self.get_tf_dataset
elif FLAGS.datasource == 'binary_omniglot':
self.target_dataset = 'binary_omniglot'
self.TRAIN_FILE = 'binary_omniglot_train.tfrecords'
self.VALID_FILE = 'binary_omniglot_valid.tfrecords'
self.TEST_FILE = 'binary_omniglot_test.tfrecords'
self.input_dim = 784
self.num_classes = 50
self.dtype = tf.float32
self.preprocess = self._preprocess_omniglot
self.get_dataset = self.get_tf_dataset
elif FLAGS.datasource == 'svhn':
self.target_dataset = 'svhn'
self.TRAIN_FILE = 'svhn_train.tfrecords'
self.VALID_FILE = 'svhn_valid.tfrecords'
self.TEST_FILE = 'svhn_test.tfrecords'
self.input_dim = (32 * 32 * 3)
self.input_height = 32
self.input_width = 32
self.input_channels = 3
self.num_classes = 10
self.dtype = tf.float32
self.preprocess = self._preprocess_svhn
self.get_dataset = self.get_tf_dataset
elif FLAGS.datasource == 'cifar10':
self.target_dataset = 'cifar10'
self.TRAIN_FILE = 'train.tfrecords'
self.VALID_FILE = 'validation.tfrecords'
self.TEST_FILE = 'eval.tfrecords'
self.input_dim = (32 * 32 * 3)
self.input_height = 32
self.input_width = 32
self.input_channels = 3
self.num_classes = 10
self.dtype = tf.float32
self.preprocess = self._preprocess_cifar10
self.get_dataset = self.get_cifar10_tf_dataset
# minor changes
elif FLAGS.datasource == 'celebA':
self.target_dataset = 'celebA'
self.TRAIN_FILE = 'celebA_train.tfrecords'
self.VALID_FILE = 'celebA_valid.tfrecords'
self.TEST_FILE = 'celebA_test.tfrecords'
# TODO: this is specific to the aligned+cropped (64 x 64) images
self.input_dim = (64 * 64 * 3)
self.input_height = 64
self.input_width = 64
self.input_channels = 3
self.dtype = tf.float32
self.preprocess = self._preprocess_celebA
self.get_dataset = self.get_tf_dataset_celebA
else:
raise NotImplementedError
train_dataset = self.get_dataset('train')
return
def _preprocess_omniglot(self, parsed_example):
# otherwise this gets decoded weird...
image = tf.decode_raw(parsed_example['features'], tf.float64)
image = tf.cast(image, tf.float32)
image.set_shape([self.input_dim])
label = tf.cast(parsed_example['label'], tf.int32)
# TODO: there are more labels here but i think it's fine for now
return image, label
def _preprocess_mnist(self, parsed_example):
image = tf.decode_raw(parsed_example['features'], tf.uint8)
image.set_shape([self.input_dim])
image = tf.cast(image, tf.float32) * (1. / 255)
label = tf.cast(parsed_example['label'], tf.int32)
return image, label
def _preprocess_binary_mnist(self, parsed_example):
image = tf.decode_raw(parsed_example['features'], tf.float32)
image.set_shape([self.input_dim])
# labels not available for BinaryMNIST
return image
def _preprocess_svhn(self, parsed_example):
image = tf.decode_raw(parsed_example['features'], tf.uint8)
image = tf.reshape(image, (self.input_height, self.input_width, self.input_channels))
image = tf.cast(image, tf.float32) * (1. / 255)
label = tf.cast(parsed_example['label'], tf.int32)
return image, label
def _preprocess_cifar10(self, parsed_example):
# the automatically generated cifar10 tfrecord has field "image" instead of "features"
image = tf.decode_raw(parsed_example['image'], tf.uint8)
image = tf.reshape(image, (self.input_channels, self.input_height, self.input_width))
image = tf.transpose(image, [1, 2, 0])
image = tf.cast(image, tf.float32) * (1. / 255)
label = tf.cast(parsed_example['label'], tf.int32)
return image, label
def _preprocess_celebA(self, parsed_example):
image = tf.decode_raw(parsed_example['features'], tf.uint8)
# image = tf.decode_raw(parsed_example['image_raw'], tf.uint8)
image = tf.reshape(image, (self.input_height, self.input_width, self.input_channels))
# convert from bytes to data
image = tf.divide(tf.to_float(image), 127.5) - 1.0
# convert back to [0, 1] pixels
image = tf.clip_by_value(tf.divide(image + 1., 2.), 0., 1.)
return image
def _test_celebA(self):
record_iterator = tf.python_io.tf_record_iterator(path=os.path.join(FLAGS.datadir, self.target_dataset, self.TRAIN_FILE))
for img_id, string_record in enumerate(record_iterator):
example = tf.train.Example()
example.ParseFromString(string_record)
print(int(example.features.feature['height'].int64_list.value))
height = int(example.features.feature['height']
.int64_list
.value[0])
width = int(example.features.feature['width']
.int64_list
.value[0])
depth = int(example.features.feature['depth']
.int64_list
.value[0])
img_string = (example.features.feature['image_raw']
.bytes_list
.value[0])
img_1d = np.fromstring(img_string, dtype=np.uint8)
print(img_1d.shape)
reconstructed_img = img_1d.reshape((height, width, -1))
print(reconstructed_img.shape)
import skimage.io as io
io.imshow(reconstructed_img)
io.show()
if img_id == 5:
exit()
def get_tf_dataset_celebA(self, split):
def _parse_function(example_proto):
example = {'features': tf.FixedLenFeature((), tf.string, default_value=''),
# 'height': tf.FixedLenFeature((), tf.int64, default_value=218),
# 'width': tf.FixedLenFeature((), tf.int64, default_value=178),
# 'channels': tf.FixedLenFeature((), tf.int64, default_value=3)
}
parsed_example = tf.parse_single_example(example_proto, example)
preprocessed_features = self.preprocess(parsed_example)
return preprocessed_features
filename = os.path.join(FLAGS.datadir, self.target_dataset, self.TRAIN_FILE if split=='train'
else self.VALID_FILE if split=='valid' else self.TEST_FILE)
tf_dataset = tf.data.TFRecordDataset(filename)
return tf_dataset.map(_parse_function)
def get_cifar10_tf_dataset(self, split):
def _parse_function(example_proto):
example = {'image': tf.FixedLenFeature((), tf.string, default_value=''),
'label': tf.FixedLenFeature((), tf.int64, default_value=0)}
parsed_example = tf.parse_single_example(example_proto, example)
preprocessed_features, preprocessed_label = self.preprocess(parsed_example)
return preprocessed_features, preprocessed_label
filename = os.path.join(FLAGS.datadir, self.target_dataset, self.TRAIN_FILE if split=='train'
else self.VALID_FILE if split=='valid' else self.TEST_FILE)
tf_dataset = tf.data.TFRecordDataset(filename)
return tf_dataset.map(_parse_function)
def get_tf_dataset(self, split):
def _parse_function(example_proto):
example = {'features': tf.FixedLenFeature((), tf.string, default_value=''),
'label': tf.FixedLenFeature((), tf.int64, default_value=0)}
parsed_example = tf.parse_single_example(example_proto, example)
preprocessed_features, preprocessed_label = self.preprocess(parsed_example)
return preprocessed_features, preprocessed_label
filename = os.path.join(FLAGS.datadir, self.target_dataset, self.TRAIN_FILE if split=='train'
else self.VALID_FILE if split=='valid' else self.TEST_FILE)
tf_dataset = tf.data.TFRecordDataset(filename)
return tf_dataset.map(_parse_function)
def get_binary_tf_dataset(self, split):
def _parse_function(example_proto):
# no labels available for binary MNIST
example = {'features': tf.FixedLenFeature((), tf.string, default_value='')}
parsed_example = tf.parse_single_example(example_proto, example)
preprocessed_features = self.preprocess(parsed_example)
return preprocessed_features
filename = os.path.join(FLAGS.datadir, self.target_dataset, self.TRAIN_FILE if split=='train'
else self.VALID_FILE if split=='valid' else self.TEST_FILE)
tf_dataset = tf.data.TFRecordDataset(filename)
return tf_dataset.map(_parse_function)