-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_generator.py
executable file
·440 lines (400 loc) · 22.5 KB
/
data_generator.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
""" Code for loading data. """
import os
import random
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import flags
import pickle
from utils import get_images
import ipdb
FLAGS = flags.FLAGS
class DataGenerator(object):
def __init__(self, num_samples_per_class, batch_size, config={}):
self.batch_size = batch_size
self.num_samples_per_class = num_samples_per_class
self.num_classes = 1 # by default 1 (only relevant for classification problems)
self.num_datasets = FLAGS.num_datasets
if FLAGS.datasource == '2D':
self.dim_input = 2
self.dim_output = 1
self.input_range = config.get('input_range', [-5.0, 5.0])
elif FLAGS.datasource == 'plainmulti':
self.num_classes = config.get('num_classes', FLAGS.num_classes)
self.img_size = config.get('img_size', (84, 84))
self.dim_input = np.prod(self.img_size) * 3
self.dim_output = self.num_classes
self.plainmulti = ['CUB_Bird', 'DTD_Texture', 'FGVC_Aircraft', 'FGVCx_Fungi', 'vgg_flower', 'GTSRB']
# self.plainmulti = ['vgg_flower', 'GTSRB', 'CUB_Bird', 'DTD_Texture', 'FGVC_Aircraft', 'FGVCx_Fungi']
print("\n\nDatasets trained and tested on: \n\n", [self.plainmulti[i]\
for i in range(self.num_datasets)])
# random.shuffle(self.plainmulti)
metatrain_folders, metaval_folders = [], []
for eachdataset in self.plainmulti:
metatrain_folders.append(
[os.path.join('{0}/plainmulti/{1}/train'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/plainmulti/{1}/train'.format(FLAGS.datadir, eachdataset)) \
if
os.path.isdir(os.path.join('{0}/plainmulti/{1}/train'.format(FLAGS.datadir, eachdataset), label)) \
])
if FLAGS.test_set:
metaval_folders.append(
[os.path.join('{0}/plainmulti/{1}/test'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/plainmulti/{1}/test'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/plainmulti/{1}/test'.format(FLAGS.datadir, eachdataset), label)) \
])
else:
metaval_folders.append(
[os.path.join('{0}/plainmulti/{1}/val'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/plainmulti/{1}/val'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/plainmulti/{1}/val'.format(FLAGS.datadir, eachdataset), label)) \
])
self.metatrain_character_folders = metatrain_folders
self.metaval_character_folders = metaval_folders
self.rotations = config.get('rotations', [0])
elif FLAGS.datasource == 'artmulti':
self.num_classes = config.get('num_classes', FLAGS.num_classes)
self.img_size = config.get('img_size', (84, 84))
self.dim_input = np.prod(self.img_size) * 3
self.dim_output = self.num_classes
self.artmulti = ['CUB_Bird', 'DTD_Texture', 'FGVC_Aircraft', 'FGVCx_Fungi', 'CUB_Bird_blur',
'DTD_Texture_blur', 'FGVC_Aircraft_blur', 'FGVCx_Fungi_blur', 'CUB_Bird_pencil',
'DTD_Texture_pencil', 'FGVC_Aircraft_pencil', 'FGVCx_Fungi_pencil']
metatrain_folders, metaval_folders = [], []
for eachdataset in self.artmulti:
metatrain_folders.append(
[os.path.join('{0}/artmulti/{1}/train'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/artmulti/{1}/train'.format(FLAGS.datadir, eachdataset)) \
if
os.path.isdir(
os.path.join('{0}/artmulti/{1}/train'.format(FLAGS.datadir, eachdataset), label)) \
])
if FLAGS.test_set:
metaval_folders.append(
[os.path.join('{0}/artmulti/{1}/test'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/artmulti/{1}/test'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/artmulti/{1}/test'.format(FLAGS.datadir, eachdataset), label)) \
])
else:
metaval_folders.append(
[os.path.join('{0}/artmulti/{1}/val'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/artmulti/{1}/val'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/artmulti/{1}/val'.format(FLAGS.datadir, eachdataset), label)) \
])
self.metatrain_character_folders = metatrain_folders
self.metaval_character_folders = metaval_folders
self.rotations = config.get('rotations', [0])
elif FLAGS.datasource == 'domainNet':
self.num_classes = config.get('num_classes', FLAGS.num_classes)
self.img_size = config.get('img_size', (84, 84))
self.dim_input = np.prod(self.img_size) * 3
self.dim_output = self.num_classes
self.domainNet = ['clipart', 'infograph', 'painting', 'quickdraw', 'real', 'sketch']
print("\n\nDatasets trained and tested on: \n\n", [self.domainNet[i]\
for i in range(self.num_datasets)])
# random.shuffle(self.domainNet)
metatrain_folders, metaval_folders = [], []
for eachdataset in self.domainNet:
metatrain_folders.append(
[os.path.join('{0}/domainNet/{1}/train'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/domainNet/{1}/train'.format(FLAGS.datadir, eachdataset)) \
if
os.path.isdir(os.path.join('{0}/domainNet/{1}/train'.format(FLAGS.datadir, eachdataset), label)) \
])
if FLAGS.test_set:
metaval_folders.append(
[os.path.join('{0}/domainNet/{1}/test'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/domainNet/{1}/test'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/domainNet/{1}/test'.format(FLAGS.datadir, eachdataset), label)) \
])
else:
metaval_folders.append(
[os.path.join('{0}/domainNet/{1}/val'.format(FLAGS.datadir, eachdataset), label) \
for label in os.listdir('{0}/domainNet/{1}/val'.format(FLAGS.datadir, eachdataset)) \
if os.path.isdir(
os.path.join('{0}/domainNet/{1}/val'.format(FLAGS.datadir, eachdataset), label)) \
])
self.metatrain_character_folders = metatrain_folders
self.metaval_character_folders = metaval_folders
self.rotations = config.get('rotations', [0])
else:
raise ValueError('Unrecognized data source')
def make_data_tensor_plainmulti(self, train=True):
if train:
folders = self.metatrain_character_folders
num_total_batches = 200000
else:
folders = self.metaval_character_folders
num_total_batches = FLAGS.num_test_task
# make list of files
print('Generating filenames')
all_filenames = []
for image_itr in range(num_total_batches):
sel = np.random.randint(self.num_datasets)
if FLAGS.train == False and FLAGS.test_dataset != -1:
sel = FLAGS.test_dataset
sampled_character_folders = random.sample(folders[sel], self.num_classes)
random.shuffle(sampled_character_folders)
labels_and_images = get_images(sampled_character_folders, range(self.num_classes),
nb_samples=self.num_samples_per_class, shuffle=False)
# make sure the above isn't randomized order
labels = [li[0] for li in labels_and_images]
filenames = [li[1] for li in labels_and_images]
all_filenames.extend(filenames)
# make queue for tensorflow to read from
filename_queue = tf.train.string_input_producer(tf.convert_to_tensor(all_filenames), shuffle=False)
print('Generating image processing ops')
image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
if FLAGS.datasource in ['plainmulti', 'artmulti']:
image = tf.image.decode_jpeg(image_file, channels=3)
image.set_shape((self.img_size[0], self.img_size[1], 3))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
else:
image = tf.image.decode_png(image_file)
image.set_shape((self.img_size[0], self.img_size[1], 1))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
image = 1.0 - image # invert
num_preprocess_threads = 1
min_queue_examples = 256
examples_per_batch = self.num_classes * self.num_samples_per_class
batch_image_size = self.batch_size * examples_per_batch
print('Batching images')
images = tf.train.batch(
[image],
batch_size=batch_image_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_image_size,
)
all_image_batches, all_label_batches = [], []
print('Manipulating image data to be right shape')
for i in range(self.batch_size):
image_batch = images[i * examples_per_batch:(i + 1) * examples_per_batch]
label_batch = tf.convert_to_tensor(labels)
new_list, new_label_list = [], []
for k in range(self.num_samples_per_class):
class_idxs = tf.range(0, self.num_classes)
class_idxs = tf.random_shuffle(class_idxs)
true_idxs = class_idxs * self.num_samples_per_class + k
new_list.append(tf.gather(image_batch, true_idxs))
new_label_list.append(tf.gather(label_batch, true_idxs))
new_list = tf.concat(new_list, 0) # has shape [self.num_classes*self.num_samples_per_class, self.dim_input]
new_label_list = tf.concat(new_label_list, 0)
all_image_batches.append(new_list)
all_label_batches.append(new_label_list)
all_image_batches = tf.stack(all_image_batches)
all_label_batches = tf.stack(all_label_batches)
all_label_batches = tf.one_hot(all_label_batches, self.num_classes)
return all_image_batches, all_label_batches
def make_data_tensor_artmulti(self, train=True):
if train:
folders = self.metatrain_character_folders
num_total_batches = 200000
else:
folders = self.metaval_character_folders
num_total_batches = FLAGS.num_test_task
# make list of files
print('Generating filenames')
all_filenames = []
for _ in range(num_total_batches):
sel = np.random.randint(12)
if FLAGS.train == False and FLAGS.test_dataset != -1:
sel = FLAGS.test_dataset
sampled_character_folders = random.sample(folders[sel], self.num_classes)
random.shuffle(sampled_character_folders)
labels_and_images = get_images(sampled_character_folders, range(self.num_classes),
nb_samples=self.num_samples_per_class, shuffle=False)
# make sure the above isn't randomized order
labels = [li[0] for li in labels_and_images]
filenames = [li[1] for li in labels_and_images]
all_filenames.extend(filenames)
# make queue for tensorflow to read from
filename_queue = tf.train.string_input_producer(tf.convert_to_tensor(all_filenames), shuffle=False)
print('Generating image processing ops')
image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
# image_file=datadict[filename_queue]
if FLAGS.datasource in ['plainmulti', 'artmulti']:
image = tf.image.decode_jpeg(image_file, channels=3)
# image = tf.convert_to_tensor(image_file)
image.set_shape((self.img_size[0], self.img_size[1], 3))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
else:
image = tf.image.decode_png(image_file)
image.set_shape((self.img_size[0], self.img_size[1], 1))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
image = 1.0 - image # invert
num_preprocess_threads = 1
min_queue_examples = 256
examples_per_batch = self.num_classes * self.num_samples_per_class
batch_image_size = self.batch_size * examples_per_batch
print('Batching images')
images = tf.train.batch(
[image],
batch_size=batch_image_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_image_size,
)
all_image_batches, all_label_batches = [], []
print('Manipulating image data to be right shape')
for i in range(self.batch_size):
image_batch = images[i * examples_per_batch:(i + 1) * examples_per_batch]
label_batch = tf.convert_to_tensor(labels)
new_list, new_label_list = [], []
for k in range(self.num_samples_per_class):
class_idxs = tf.range(0, self.num_classes)
class_idxs = tf.random_shuffle(class_idxs)
true_idxs = class_idxs * self.num_samples_per_class + k
new_list.append(tf.gather(image_batch, true_idxs))
new_label_list.append(tf.gather(label_batch, true_idxs))
new_list = tf.concat(new_list, 0) # has shape [self.num_classes*self.num_samples_per_class, self.dim_input]
new_label_list = tf.concat(new_label_list, 0)
all_image_batches.append(new_list)
all_label_batches.append(new_label_list)
all_image_batches = tf.stack(all_image_batches)
all_label_batches = tf.stack(all_label_batches)
all_label_batches = tf.one_hot(all_label_batches, self.num_classes)
return all_image_batches, all_label_batches
def make_data_tensor_domainNet(self, train=True):
if train:
folders = self.metatrain_character_folders
num_total_batches = 200000
else:
folders = self.metaval_character_folders
num_total_batches = FLAGS.num_test_task
# make list of files
print('Generating filenames')
all_filenames = []
for image_itr in range(num_total_batches):
sel = np.random.randint(self.num_datasets)
if FLAGS.train == False and FLAGS.test_dataset != -1:
sel = FLAGS.test_dataset
sampled_character_folders = random.sample(folders[sel], self.num_classes)
random.shuffle(sampled_character_folders)
labels_and_images = get_images(sampled_character_folders, range(self.num_classes),
nb_samples=self.num_samples_per_class, shuffle=False)
# make sure the above isn't randomized order
labels = [li[0] for li in labels_and_images]
filenames = [li[1] for li in labels_and_images]
all_filenames.extend(filenames)
# make queue for tensorflow to read from
filename_queue = tf.train.string_input_producer(tf.convert_to_tensor(all_filenames), shuffle=False)
print('Generating image processing ops')
image_reader = tf.WholeFileReader()
_, image_file = image_reader.read(filename_queue)
if FLAGS.datasource in ['plainmulti', 'artmulti', 'domainNet']:
image = tf.image.decode_jpeg(image_file, channels=3)
image = tf.image.resize(image, (self.img_size[0], self.img_size[1]), preserve_aspect_ratio=False)
# image.set_shape((self.img_size[0], self.img_size[1], 3))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
else:
image = tf.image.decode_png(image_file)
image.set_shape((self.img_size[0], self.img_size[1], 1))
image = tf.reshape(image, [self.dim_input])
image = tf.cast(image, tf.float32) / 255.0
image = 1.0 - image # invert
num_preprocess_threads = 1
min_queue_examples = 256
examples_per_batch = self.num_classes * self.num_samples_per_class
batch_image_size = self.batch_size * examples_per_batch
print('Batching images')
images = tf.train.batch(
[image],
batch_size=batch_image_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_image_size,
)
all_image_batches, all_label_batches = [], []
print('Manipulating image data to be right shape')
for i in range(self.batch_size):
image_batch = images[i * examples_per_batch:(i + 1) * examples_per_batch]
label_batch = tf.convert_to_tensor(labels)
new_list, new_label_list = [], []
for k in range(self.num_samples_per_class):
class_idxs = tf.range(0, self.num_classes)
class_idxs = tf.random_shuffle(class_idxs)
true_idxs = class_idxs * self.num_samples_per_class + k
new_list.append(tf.gather(image_batch, true_idxs))
new_label_list.append(tf.gather(label_batch, true_idxs))
new_list = tf.concat(new_list, 0) # has shape [self.num_classes*self.num_samples_per_class, self.dim_input]
new_label_list = tf.concat(new_label_list, 0)
all_image_batches.append(new_list)
all_label_batches.append(new_label_list)
all_image_batches = tf.stack(all_image_batches)
all_label_batches = tf.stack(all_label_batches)
all_label_batches = tf.one_hot(all_label_batches, self.num_classes)
print(all_image_batches.shape, all_label_batches.shape)
return all_image_batches, all_label_batches
def generate_2D_batch(self, train=False):
dim_input = self.dim_input
dim_output = self.dim_output
batch_size = self.batch_size
num_samples_per_class = self.num_samples_per_class
# sin
amp = np.random.uniform(0.1, 5.0, size=self.batch_size)
phase = np.random.uniform(0., 2 * np.pi, size=batch_size)
freq = np.random.uniform(0.8, 1.2, size=batch_size)
# linear
A = np.random.uniform(-3.0, 3.0, size=batch_size)
b = np.random.uniform(-3.0, 3.0, size=batch_size)
# quadratic
A_q = np.random.uniform(-0.2, 0.2, size=batch_size)
b_q = np.random.uniform(-2.0, 2.0, size=batch_size)
c_q = np.random.uniform(-3.0, 3.0, size=batch_size)
# cubic
A_c = np.random.uniform(-0.1, 0.1, size=batch_size)
b_c = np.random.uniform(-0.2, 0.2, size=batch_size)
c_c = np.random.uniform(-2.0, 2.0, size=batch_size)
d_c = np.random.uniform(-3.0, 3.0, size=batch_size)
# 3d curve
A_3cur = np.random.uniform(-1.0, 1.0, size=batch_size)
B_3cur = np.random.uniform(-1.0, 1.0, size=batch_size)
# ripple
A_r = np.random.uniform(-0.2, 0.2, size=batch_size)
B_r = np.random.uniform(-3.0, 3.0, size=batch_size)
sel_set = np.zeros(batch_size)
init_inputs = np.zeros([batch_size, num_samples_per_class, dim_input])
outputs = np.zeros([batch_size, num_samples_per_class, dim_output])
for func in range(batch_size):
init_inputs[func] = np.random.uniform(self.input_range[0], self.input_range[1], size=(num_samples_per_class, dim_input))
sel = np.random.randint(FLAGS.sync_group_num)
if FLAGS.train == False and FLAGS.test_dataset != -1:
sel = FLAGS.test_dataset
if sel == 0:
init_inputs[func, :, 1:2] = 1
outputs[func] = amp[func] * np.sin(freq[func] * init_inputs[func, :, 0:1] + phase[func])
elif sel == 1:
init_inputs[func, :, 1:2] = 1
outputs[func] = A[func] * init_inputs[func, :, 0:1] + b[func]
elif sel == 2:
# outputs[func] = A_q[func] * np.square(init_inputs[func] - c_q[func]) + b_q[func]
init_inputs[func, :, 1:2] = 1
outputs[func] = A_q[func] * np.square(init_inputs[func, :, 0:1]) + b_q[func] * init_inputs[func, :, 0:1] + \
c_q[func]
elif sel == 3:
init_inputs[func, :, 1:2] = 1
outputs[func] = A_c[func] * np.power(init_inputs[func, :, 0:1],
np.tile([3], init_inputs[func, :, 0:1].shape)) + b_c[
func] * np.square(init_inputs[func, :, 0:1]) + c_c[func] * init_inputs[func, :, 0:1] + \
d_c[func]
elif sel == 4:
outputs[func] = A_3cur[func] * np.square(init_inputs[func, :, 0:1]) + B_3cur[func] * np.square(
init_inputs[func, :, 1:2])
elif sel == 5:
outputs[func] = np.sin(
-A_r[func] * (np.square(init_inputs[func, :, 0:1]) + np.square(init_inputs[func, :, 1:2]))) + B_r[func]
outputs[func] += np.random.normal(0, 0.3, size=(num_samples_per_class, dim_output))
sel_set[func] = sel
funcs_params = {'amp': amp, 'phase': phase, 'freq': freq, 'A': A, 'b': b, 'A_q': A_q, 'c_q': c_q, 'b_q': b_q,
'A_c': A_c, 'b_c': b_c, 'c_c': c_c, 'd_c': d_c, 'A_3cur': A_3cur, 'B_3cur': B_3cur, 'A_r':A_r, 'B_r':B_r}
return init_inputs, outputs, funcs_params, sel_set