forked from brain-research/self-attention-gan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_ori.py
359 lines (294 loc) · 12 KB
/
utils_ori.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
# Copyright 2018 Google LLC
#
# 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
#
# https://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.
# ==============================================================================
"""Some codes from https://github.com/Newmu/dcgan_code."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import os
import numpy as np
import scipy.misc
import sympy
import tensorflow as tf
tfgan = tf.contrib.gan
classifier_metrics = tf.contrib.gan.eval.classifier_metrics
gfile = tf.gfile
def make_z_normal(num_batches, batch_size, z_dim):
"""Make random noises tensors with normal distribution feeding into the generator
Args:
num_batches: copies of batches
batch_size: the batch_size for z
z_dim: The dimension of the z (noise) vector.
Returns:
zs: noise tensors.
"""
shape = [num_batches, batch_size, z_dim]
z = tf.random_normal(shape, name='z0', dtype=tf.float32)
return z
def run_custom_inception(
images,
output_tensor,
graph_def=None,
# image_size=classifier_metrics.INCEPTION_DEFAULT_IMAGE_SIZE):
image_size=299):
# input_tensor=classifier_metrics.INCEPTION_V1_INPUT):
"""Run images through a pretrained Inception classifier.
This method resizes the images before feeding them through Inception; we do
this to accommodate feeding images through in minibatches without having to
construct any large tensors.
Args:
images: Input tensors. Must be [batch, height, width, channels]. Input shape
and values must be in [-1, 1], which can be achieved using
`preprocess_image`.
output_tensor: Name of output Tensor. This function will compute activations
at the specified layer. Examples include INCEPTION_V3_OUTPUT and
INCEPTION_V3_FINAL_POOL which would result in this function computing
the final logits or the penultimate pooling layer.
graph_def: A GraphDef proto of a pretrained Inception graph. If `None`,
call `default_graph_def_fn` to get GraphDef.
image_size: Required image width and height. See unit tests for the default
values.
input_tensor: Name of input Tensor.
Returns:
Logits.
"""
images = tf.image.resize_bilinear(images, [image_size, image_size])
return tfgan.eval.run_inception(
images,
graph_def=graph_def,
image_size=image_size,
# input_tensor=input_tensor,
output_tensor=output_tensor)
def get_real_activations(data_dir,
batch_size,
num_batches,
label_offset=0,
cycle_length=1,
shuffle_buffer_size=100000):
"""Fetches num_batches batches of size batch_size from the data_dir.
Args:
data_dir: The directory to read data from. Expected to be a single
TFRecords file.
batch_size: The number of elements in a single minibatch.
num_batches: The number of batches to fetch at a time.
label_offset: The scalar to add to the labels in the dataset. The imagenet
GAN code expects labels in [0, 999], and this scalar can be used to move
other labels into this range. (Default: 0)
cycle_length: The number of input elements to process concurrently in the
Dataset loader. (Default: 1)
shuffle_buffer_size: The number of records to load before shuffling. Larger
means more likely randomization. (Default: 100000)
Returns:
A list of num_batches batches of size batch_size.
"""
# filenames = gfile.Glob(os.path.join(data_dir, '*_train_*-*-of-*'))
filenames = tf.gfile.Glob(os.path.join(data_dir, '*.tfrecords'))
filename_dataset = tf.data.Dataset.from_tensor_slices(filenames)
filename_dataset = filename_dataset.shuffle(len(filenames))
prefetch = max(int((batch_size * num_batches) / cycle_length), 1)
dataset = filename_dataset.interleave(
lambda fn: tf.data.TFRecordDataset(fn).prefetch(prefetch),
cycle_length=cycle_length)
dataset = dataset.shuffle(shuffle_buffer_size)
image_size = 128
# graph_def = classifier_metrics._default_graph_def_fn() # pylint: disable=protected-access
def _extract_image_and_label(record):
"""Extracts and preprocesses the image and label from the record."""
features = tf.parse_single_example(
record,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape(image_size * image_size * 3)
image = tf.reshape(image, [image_size, image_size, 3])
image = tf.cast(image, tf.float32) * (2. / 255) - 1.
label = tf.cast(features['label'], tf.int32)
label += label_offset
return image, label
dataset = dataset.map(
_extract_image_and_label,
num_parallel_calls=16).prefetch(batch_size * num_batches)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
real_images, _ = iterator.get_next()
real_images.set_shape([batch_size, image_size, image_size, 3])
pools = run_custom_inception(
real_images, graph_def=None, output_tensor=['pool_3:0'])[0]
def while_cond(_, i):
return tf.less(i, num_batches)
def while_body(real_pools, i):
with tf.control_dependencies([real_pools]):
imgs, _ = iterator.get_next()
imgs.set_shape([batch_size, image_size, image_size, 3])
pools = run_custom_inception(
imgs, graph_def=None, output_tensor=['pool_3:0'])[0]
real_pools = tf.concat([real_pools, pools], 0)
return (real_pools, tf.add(i, 1))
# Get activations from real images.
i = tf.constant(1)
real_pools, _ = tf.while_loop(
while_cond,
while_body, [pools, i],
shape_invariants=[tf.TensorShape([None, 2048]),
i.get_shape()],
parallel_iterations=1,
back_prop=False,
swap_memory=True,
name='RealActivations')
real_pools.set_shape([batch_size * num_batches, 2048])
return real_pools, real_images
def get_imagenet_batches(data_dir,
batch_size,
num_batches,
label_offset=0,
cycle_length=1,
shuffle_buffer_size=100000):
"""Fetches num_batches batches of size batch_size from the data_dir.
Args:
data_dir: The directory to read data from. Expected to be a single
TFRecords file.
batch_size: The number of elements in a single minibatch.
num_batches: The number of batches to fetch at a time.
label_offset: The scalar to add to the labels in the dataset. The imagenet
GAN code expects labels in [0, 999], and this scalar can be used to move
other labels into this range. (Default: 0)
cycle_length: The number of input elements to process concurrently in the
Dataset loader. (Default: 1)
shuffle_buffer_size: The number of records to load before shuffling. Larger
means more likely randomization. (Default: 100000)
Returns:
A list of num_batches batches of size batch_size.
"""
# filenames = gfile.Glob(os.path.join(data_dir, '*_train_*-*-of-*'))
filenames = tf.gfile.Glob(os.path.join(data_dir, '*.tfrecords'))
filename_dataset = tf.data.Dataset.from_tensor_slices(filenames)
filename_dataset = filename_dataset.shuffle(len(filenames))
prefetch = max(int((batch_size * num_batches) / cycle_length), 1)
dataset = filename_dataset.interleave(
lambda fn: tf.data.TFRecordDataset(fn).prefetch(prefetch),
cycle_length=cycle_length)
dataset = dataset.shuffle(shuffle_buffer_size)
image_size = 128
def _extract_image_and_label(record):
"""Extracts and preprocesses the image and label from the record."""
features = tf.parse_single_example(
record,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape(image_size * image_size * 3)
image = tf.reshape(image, [image_size, image_size, 3])
image = tf.cast(image, tf.float32) * (2. / 255) - 1.
label = tf.cast(features['label'], tf.int32)
label += label_offset
return image, label
dataset = dataset.map(
_extract_image_and_label,
num_parallel_calls=16).prefetch(batch_size * num_batches)
dataset = dataset.repeat() # Repeat for unlimited epochs.
dataset = dataset.batch(batch_size)
dataset = dataset.batch(num_batches)
iterator = dataset.make_one_shot_iterator()
images, labels = iterator.get_next()
batches = []
for i in range(num_batches):
# Dataset batches lose shape information. Put it back in.
im = images[i, ...]
im.set_shape([batch_size, image_size, image_size, 3])
lb = labels[i, ...]
lb.set_shape((batch_size,))
batches.append((im, tf.expand_dims(lb, 1)))
return batches
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def merge(images, size):
h, w = images.shape[1], images.shape[2]
img = np.zeros((h * size[0], w * size[1], 3))
idx = 0
for i in range(0, size[0]):
for j in range(0, size[1]):
img[j * h:(j + 1) * h, i * w:(i + 1) * w, :] = images[idx]
idx += 1
return img
def imsave(images, size, path):
with gfile.Open(path, mode='w') as f:
saved = scipy.misc.imsave(f, merge(images, size))
return saved
def inverse_transform(images):
return (images + 1.) / 2.
def visualize(sess, dcgan, config, option):
option = 0
if option == 0:
all_samples = []
for i in range(484):
print(i)
samples = sess.run(dcgan.generator)
all_samples.append(samples)
samples = np.concatenate(all_samples, 0)
n = int(np.sqrt(samples.shape[0]))
m = samples.shape[0] // n
save_images(samples, [m, n], './' + config.sample_dir + '/test.png')
elif option == 1:
counter = 0
coord = tf.train.Coordinator()
tf.train.start_queue_runners(coord=coord)
while counter < 1005:
print(counter)
samples, fake = sess.run([dcgan.generator, dcgan.d_loss_class])
fake = np.argsort(fake)
print(np.sum(samples))
print(fake)
for i in range(samples.shape[0]):
name = '%s%d.png' % (chr(ord('a') + counter % 10), counter)
img = np.expand_dims(samples[fake[i]], 0)
if counter >= 1000:
save_images(img, [1, 1], './{}/turk/fake{}.png'.format(
config.sample_dir, counter - 1000))
else:
save_images(img, [1, 1], './{}/turk/{}'.format(
config.sample_dir, name))
counter += 1
elif option == 2:
values = np.arange(0, 1, 1. / config.batch_size)
for idx in range(100):
print(' [*] %d' % idx)
z_sample = np.zeros([config.batch_size, dcgan.z_dim])
for kdx, z in enumerate(z_sample):
z[idx] = values[kdx]
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
save_images(samples, [8, 8], './{}/test_arange_{}.png'.format(
config.sample_dir, idx))
def squarest_grid_size(num_images):
"""Calculates the size of the most square grid for num_images.
Calculates the largest integer divisor of num_images less than or equal to
sqrt(num_images) and returns that as the width. The height is
num_images / width.
Args:
num_images: The total number of images.
Returns:
A tuple of (height, width) for the image grid.
"""
divisors = sympy.divisors(num_images)
square_root = math.sqrt(num_images)
width = 1
for d in divisors:
if d > square_root:
break
width = d
return (num_images // width, width)