-
Notifications
You must be signed in to change notification settings - Fork 11
/
cifar10_sobolev.py
349 lines (266 loc) · 12.3 KB
/
cifar10_sobolev.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
"""Code for training Banach Wasserstein GAN on CIFAR 10.
With all the dependencies installed, the code should run as-is.
Data is downloaded on the fly.
"""
import adler.tensorflow as atf
import sys
import tensorflow as tf
import numpy as np
import tensordata
import functools
from sobolev_utils import sobolev_filter
# User selectable parameters
EXPONENT = 2
SOBOLEV_C = 5.0
SOBOLEV_S = 0
MAX_ITERS = 100000
SUMMARY_FREQ = 10
INCEPTION_FREQ = 1000
BATCH_SIZE = 64
BATCH_SIZE_TEST = 100
reset = True
# set seeds for reproducibility
np.random.seed(0)
tf.set_random_seed(0)
sess = tf.InteractiveSession()
# Training specific parameters
name = 'cifar10_sobolev_5/p={}s={}'.format(EXPONENT, SOBOLEV_S)
size = 32
DUAL_EXPONENT = 1 / (1 - 1/EXPONENT) if EXPONENT != 1 else np.inf
print('index={}, s={}, p={}, q={}'.format(index, SOBOLEV_S, EXPONENT, DUAL_EXPONENT))
with tf.name_scope('placeholders'):
x_train_ph, _ = tensordata.get_cifar10_tf(batch_size=BATCH_SIZE)
x_test_ph, _ = tensordata.get_cifar10_tf(batch_size=BATCH_SIZE_TEST)
is_training = tf.placeholder(bool, name='is_training')
use_agumentation = tf.identity(is_training, name='is_training')
with tf.name_scope('pre_process'):
x_train = (x_train_ph - 0.5) * 2.0
x_test = (x_test_ph - 0.5) * 2.0
x_true = tf.cond(is_training,
lambda: x_train,
lambda: x_test)
def apply_conv(x, filters=32, kernel_size=3, he_init=True):
if he_init:
initializer = tf.contrib.layers.variance_scaling_initializer(uniform=True)
else:
initializer = tf.contrib.layers.xavier_initializer(uniform=True)
return tf.layers.conv2d(x, filters=filters, kernel_size=kernel_size,
padding='SAME', kernel_initializer=initializer)
def activation(x):
with tf.name_scope('activation'):
return tf.nn.relu(x)
def bn(x):
return tf.contrib.layers.batch_norm(x,
decay=0.9,
center=True,
scale=True,
epsilon=1e-5,
zero_debias_moving_mean=True,
is_training=is_training)
def stable_norm(x, ord):
x = tf.contrib.layers.flatten(x)
alpha = tf.reduce_max(tf.abs(x) + 1e-5, axis=1)
result = alpha * tf.norm(x / alpha[:, None], ord=ord, axis=1)
return result
def downsample(x):
with tf.name_scope('downsample'):
x = tf.identity(x)
return tf.add_n([x[:,::2,::2,:], x[:,1::2,::2,:],
x[:,::2,1::2,:], x[:,1::2,1::2,:]]) / 4.
def upsample(x):
with tf.name_scope('upsample'):
x = tf.identity(x)
x = tf.concat([x, x, x, x], axis=-1)
return tf.depth_to_space(x, 2)
def conv_meanpool(x, **kwargs):
return downsample(apply_conv(x, **kwargs))
def meanpool_conv(x, **kwargs):
return apply_conv(downsample(x), **kwargs)
def upsample_conv(x, **kwargs):
return apply_conv(upsample(x), **kwargs)
def resblock(x, filters, resample=None, normalize=False):
if normalize:
norm_fn = bn
else:
norm_fn = tf.identity
if resample == 'down':
conv_1 = functools.partial(apply_conv, filters=filters)
conv_2 = functools.partial(conv_meanpool, filters=filters)
conv_shortcut = functools.partial(conv_meanpool, filters=filters,
kernel_size=1, he_init=False)
elif resample == 'up':
conv_1 = functools.partial(upsample_conv, filters=filters)
conv_2 = functools.partial(apply_conv, filters=filters)
conv_shortcut = functools.partial(upsample_conv, filters=filters,
kernel_size=1, he_init=False)
elif resample == None:
conv_1 = functools.partial(apply_conv, filters=filters)
conv_2 = functools.partial(apply_conv, filters=filters)
conv_shortcut = tf.identity
with tf.name_scope('resblock'):
x = tf.identity(x)
update = conv_1(activation(norm_fn(x)))
update = conv_2(activation(norm_fn(update)))
skip = conv_shortcut(x)
return skip + update
def resblock_optimized(x, filters):
with tf.name_scope('resblock'):
x = tf.identity(x)
update = apply_conv(x, filters=filters)
update = conv_meanpool(activation(update), filters=filters)
skip = meanpool_conv(x, filters=filters, kernel_size=1, he_init=False)
return skip + update
def generator(z, reuse):
with tf.variable_scope('generator', reuse=reuse):
with tf.name_scope('pre_process'):
z = tf.layers.dense(z, 4 * 4 * 128)
x = tf.reshape(z, [-1, 4, 4, 128])
with tf.name_scope('x1'):
x = resblock(x, filters=128, resample='up', normalize=True) # 8
x = resblock(x, filters=128, resample='up', normalize=True) # 16
x = resblock(x, filters=128, resample='up', normalize=True) # 32
with tf.name_scope('post_process'):
x = activation(bn(x))
result = apply_conv(x, filters=3, he_init=False)
return tf.tanh(result)
def discriminator(x, reuse):
with tf.variable_scope('discriminator', reuse=reuse):
with tf.name_scope('pre_process'):
x = resblock_optimized(x, filters=128)
with tf.name_scope('x1'):
x = resblock(x, filters=128, resample='down') # 8
x = resblock(x, filters=128) # 16
x = resblock(x, filters=128) # 32
with tf.name_scope('post_process'):
x = activation(x)
x = tf.reduce_mean(x, axis=[1, 2])
flat = tf.contrib.layers.flatten(x)
flat = tf.layers.dense(flat, 1)
return flat
with tf.name_scope('gan'):
z = tf.random_normal([tf.shape(x_true)[0], 128], name="z")
x_generated = generator(z, reuse=False)
d_true = discriminator(x_true, reuse=False)
d_generated = discriminator(x_generated, reuse=True)
z_gen = tf.random_normal([BATCH_SIZE * 2, 128], name="z")
d_generated_train = discriminator(generator(z_gen, reuse=True), reuse=True)
with tf.name_scope('dual_norm'):
sobolev_true = sobolev_filter(x_true, c=SOBOLEV_C, s=SOBOLEV_S)
lamb = tf.reduce_mean(stable_norm(sobolev_true, ord=EXPONENT))
dual_sobolev_true = sobolev_filter(x_true, c=SOBOLEV_C, s=-SOBOLEV_S)
gamma = tf.reduce_mean(stable_norm(sobolev_true, ord=DUAL_EXPONENT))
with tf.name_scope('regularizer'):
epsilon = tf.random_uniform([tf.shape(x_true)[0], 1, 1, 1], 0.0, 1.0)
x_hat = epsilon * x_generated + (1 - epsilon) * x_true
d_hat = discriminator(x_hat, reuse=True)
gradients = tf.gradients(d_hat, x_hat)[0]
dual_sobolev_gradients = sobolev_filter(gradients, c=SOBOLEV_C, s=-SOBOLEV_S)
ddx = stable_norm(dual_sobolev_gradients, ord=DUAL_EXPONENT)
d_regularizer = tf.reduce_mean(tf.square(ddx / gamma - 1))
d_regularizer_mean = tf.reduce_mean(tf.square(d_true))
with tf.name_scope('loss_gan'):
wasserstein_scaled = (tf.reduce_mean(d_generated) - tf.reduce_mean(d_true))
wasserstein = wasserstein_scaled / gamma
g_loss = tf.reduce_mean(d_generated_train) / gamma
d_loss = (-wasserstein +
lamb * d_regularizer +
1e-5 * d_regularizer_mean)
with tf.name_scope('optimizer'):
ema = atf.EMAHelper(decay=0.99)
global_step = tf.Variable(0, trainable=False, name='global_step')
decay = tf.maximum(0., 1.-(tf.cast(global_step, tf.float32)/MAX_ITERS))
learning_rate = 2e-4 * decay
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0., beta2=0.9)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='gan/generator')
g_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='generator')
with tf.control_dependencies(update_ops):
g_train = optimizer.minimize(g_loss, var_list=g_vars,
global_step=global_step)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='gan/discriminator')
d_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='discriminator')
with tf.control_dependencies(update_ops):
d_train = optimizer.minimize(d_loss, var_list=d_vars)
with tf.name_scope('summaries'):
tf.summary.scalar('wasserstein_scaled', wasserstein_scaled)
tf.summary.scalar('wasserstein', wasserstein)
tf.summary.scalar('g_loss', g_loss)
tf.summary.scalar('d_loss', d_loss)
atf.scalars_summary('d_true', d_true)
atf.scalars_summary('d_generated', d_generated)
tf.summary.scalar('d_regularizer', d_regularizer)
tf.summary.scalar('d_regularizer_mean', d_regularizer_mean)
tf.summary.scalar('learning_rate', learning_rate)
tf.summary.scalar('global_step', global_step)
atf.scalars_summary('x_generated', x_generated)
atf.scalars_summary('x_true', x_true)
atf.scalars_summary('gamma', gamma)
atf.scalars_summary('lamb', lamb)
atf.image_grid_summary('x_true', x_true)
atf.image_grid_summary('x_generated', x_generated)
atf.image_grid_summary('gradients', gradients)
atf.image_grid_summary('dual_sobolev_gradients', dual_sobolev_gradients)
atf.scalars_summary('ddx', ddx)
atf.scalars_summary('gradients', gradients)
atf.scalars_summary('dual_sobolev_gradients', dual_sobolev_gradients)
merged_summary = tf.summary.merge_all()
# Advanced metrics
with tf.name_scope('inception'):
# Specific function to compute inception score for very large
# number of samples
def generate_and_classify(z):
INCEPTION_OUTPUT = 'logits:0'
x = generator(z, reuse=True)
x = tf.image.resize_bilinear(x, [299, 299])
return tf.contrib.gan.eval.run_inception(x, output_tensor=INCEPTION_OUTPUT)
# Fixed z for fairness between runs
inception_z = tf.constant(np.random.randn(10000, 128), dtype='float32')
inception_score = tf.contrib.gan.eval.classifier_score(inception_z,
classifier_fn=generate_and_classify,
num_batches=10000 // 100)
inception_summary = tf.summary.merge([
tf.summary.scalar('inception_score', inception_score)])
full_summary = tf.summary.merge([merged_summary, inception_summary])
test_summary_writer, train_summary_writer = atf.util.summary_writers(name, cleanup=reset, write_graph=False)
# Initialize all TF variables
sess.run([tf.global_variables_initializer(),
tf.local_variables_initializer()])
# Coordinate the loading of image files.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# Add op to save and restore
saver = tf.train.Saver()
if not reset:
saver.restore(sess,
atf.util.default_checkpoint_path(name))
# Standardized validation z
z_validate = np.random.randn(BATCH_SIZE_TEST, 128)
# Train the network
while True:
i = sess.run(global_step)
if i >= MAX_ITERS:
break
num_d_train = 5
for j in range(num_d_train):
_, d_loss_result = sess.run([d_train, d_loss],
feed_dict={is_training: True})
_, g_loss_result, _ = sess.run([g_train, g_loss, ema.apply],
feed_dict={is_training: True})
print('s={}, i={}, j={}, d_loss={}, g_loss={}'.format(SOBOLEV_S, i, j,
d_loss_result,
g_loss_result))
if i % SUMMARY_FREQ == SUMMARY_FREQ - 1:
ema_dict = ema.average_dict()
merged_summary_result_train = sess.run(merged_summary,
feed_dict={is_training: False,
**ema_dict})
train_summary_writer.add_summary(merged_summary_result_train, i)
if i % INCEPTION_FREQ == INCEPTION_FREQ - 1:
ema_dict = ema.average_dict()
merged_summary_result_test = sess.run(full_summary,
feed_dict={z: z_validate,
is_training: False,
**ema_dict})
test_summary_writer.add_summary(merged_summary_result_test, i)
if i % 1000 == 999:
saver.save(sess,
atf.util.default_checkpoint_path(name))