forked from davidADSP/GDL_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWGANGP.py
430 lines (318 loc) · 14.9 KB
/
WGANGP.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
from keras.layers import Input, Conv2D, Flatten, Dense, Conv2DTranspose, Reshape, Lambda, Activation, BatchNormalization, LeakyReLU, Dropout, ZeroPadding2D, UpSampling2D
from keras.layers.merge import _Merge
from keras.models import Model, Sequential
from keras import backend as K
from keras.optimizers import Adam, RMSprop
from keras.callbacks import ModelCheckpoint
from keras.utils import plot_model
from keras.initializers import RandomNormal
from functools import partial
import numpy as np
import json
import os
import pickle
import matplotlib.pyplot as plt
class RandomWeightedAverage(_Merge):
def __init__(self, batch_size):
super().__init__()
self.batch_size = batch_size
"""Provides a (random) weighted average between real and generated image samples"""
def _merge_function(self, inputs):
alpha = K.random_uniform((self.batch_size, 1, 1, 1))
return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])
class WGANGP():
def __init__(self
, input_dim
, critic_conv_filters
, critic_conv_kernel_size
, critic_conv_strides
, critic_batch_norm_momentum
, critic_activation
, critic_dropout_rate
, critic_learning_rate
, generator_initial_dense_layer_size
, generator_upsample
, generator_conv_filters
, generator_conv_kernel_size
, generator_conv_strides
, generator_batch_norm_momentum
, generator_activation
, generator_dropout_rate
, generator_learning_rate
, optimiser
, grad_weight
, z_dim
, batch_size
):
self.name = 'gan'
self.input_dim = input_dim
self.critic_conv_filters = critic_conv_filters
self.critic_conv_kernel_size = critic_conv_kernel_size
self.critic_conv_strides = critic_conv_strides
self.critic_batch_norm_momentum = critic_batch_norm_momentum
self.critic_activation = critic_activation
self.critic_dropout_rate = critic_dropout_rate
self.critic_learning_rate = critic_learning_rate
self.generator_initial_dense_layer_size = generator_initial_dense_layer_size
self.generator_upsample = generator_upsample
self.generator_conv_filters = generator_conv_filters
self.generator_conv_kernel_size = generator_conv_kernel_size
self.generator_conv_strides = generator_conv_strides
self.generator_batch_norm_momentum = generator_batch_norm_momentum
self.generator_activation = generator_activation
self.generator_dropout_rate = generator_dropout_rate
self.generator_learning_rate = generator_learning_rate
self.optimiser = optimiser
self.z_dim = z_dim
self.n_layers_critic = len(critic_conv_filters)
self.n_layers_generator = len(generator_conv_filters)
self.weight_init = RandomNormal(mean=0., stddev=0.02) # 'he_normal' #RandomNormal(mean=0., stddev=0.02)
self.grad_weight = grad_weight
self.batch_size = batch_size
self.d_losses = []
self.g_losses = []
self.epoch = 0
self._build_critic()
self._build_generator()
self._build_adversarial()
def gradient_penalty_loss(self, y_true, y_pred, interpolated_samples):
"""
Computes gradient penalty based on prediction and weighted real / fake samples
"""
gradients = K.gradients(y_pred, interpolated_samples)[0]
# compute the euclidean norm by squaring ...
gradients_sqr = K.square(gradients)
# ... summing over the rows ...
gradients_sqr_sum = K.sum(gradients_sqr,
axis=np.arange(1, len(gradients_sqr.shape)))
# ... and sqrt
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
# compute lambda * (1 - ||grad||)^2 still for each single sample
gradient_penalty = K.square(1 - gradient_l2_norm)
# return the mean as loss over all the batch samples
return K.mean(gradient_penalty)
def wasserstein(self, y_true, y_pred):
return -K.mean(y_true * y_pred)
def get_activation(self, activation):
if activation == 'leaky_relu':
layer = LeakyReLU(alpha = 0.2)
else:
layer = Activation(activation)
return layer
def _build_critic(self):
### THE critic
critic_input = Input(shape=self.input_dim, name='critic_input')
x = critic_input
for i in range(self.n_layers_critic):
x = Conv2D(
filters = self.critic_conv_filters[i]
, kernel_size = self.critic_conv_kernel_size[i]
, strides = self.critic_conv_strides[i]
, padding = 'same'
, name = 'critic_conv_' + str(i)
, kernel_initializer = self.weight_init
)(x)
if self.critic_batch_norm_momentum and i > 0:
x = BatchNormalization(momentum = self.critic_batch_norm_momentum)(x)
x = self.get_activation(self.critic_activation)(x)
if self.critic_dropout_rate:
x = Dropout(rate = self.critic_dropout_rate)(x)
x = Flatten()(x)
# x = Dense(512, kernel_initializer = self.weight_init)(x)
# x = self.get_activation(self.critic_activation)(x)
critic_output = Dense(1, activation=None
, kernel_initializer = self.weight_init
)(x)
self.critic = Model(critic_input, critic_output)
def _build_generator(self):
### THE generator
generator_input = Input(shape=(self.z_dim,), name='generator_input')
x = generator_input
x = Dense(np.prod(self.generator_initial_dense_layer_size), kernel_initializer = self.weight_init)(x)
if self.generator_batch_norm_momentum:
x = BatchNormalization(momentum = self.generator_batch_norm_momentum)(x)
x = self.get_activation(self.generator_activation)(x)
x = Reshape(self.generator_initial_dense_layer_size)(x)
if self.generator_dropout_rate:
x = Dropout(rate = self.generator_dropout_rate)(x)
for i in range(self.n_layers_generator):
if self.generator_upsample[i] == 2:
x = UpSampling2D()(x)
x = Conv2D(
filters = self.generator_conv_filters[i]
, kernel_size = self.generator_conv_kernel_size[i]
, padding = 'same'
, name = 'generator_conv_' + str(i)
, kernel_initializer = self.weight_init
)(x)
else:
x = Conv2DTranspose(
filters = self.generator_conv_filters[i]
, kernel_size = self.generator_conv_kernel_size[i]
, padding = 'same'
, strides = self.generator_conv_strides[i]
, name = 'generator_conv_' + str(i)
, kernel_initializer = self.weight_init
)(x)
if i < self.n_layers_generator - 1:
if self.generator_batch_norm_momentum:
x = BatchNormalization(momentum = self.generator_batch_norm_momentum)(x)
x = self.get_activation(self.generator_activation)(x)
else:
x = Activation('tanh')(x)
generator_output = x
self.generator = Model(generator_input, generator_output)
def get_opti(self, lr):
if self.optimiser == 'adam':
opti = Adam(lr=lr, beta_1=0.5)
elif self.optimiser == 'rmsprop':
opti = RMSprop(lr=lr)
else:
opti = Adam(lr=lr)
return opti
def set_trainable(self, m, val):
m.trainable = val
for l in m.layers:
l.trainable = val
def _build_adversarial(self):
#-------------------------------
# Construct Computational Graph
# for the Critic
#-------------------------------
# Freeze generator's layers while training critic
self.set_trainable(self.generator, False)
# Image input (real sample)
real_img = Input(shape=self.input_dim)
# Fake image
z_disc = Input(shape=(self.z_dim,))
fake_img = self.generator(z_disc)
# critic determines validity of the real and fake images
fake = self.critic(fake_img)
valid = self.critic(real_img)
# Construct weighted average between real and fake images
interpolated_img = RandomWeightedAverage(self.batch_size)([real_img, fake_img])
# Determine validity of weighted sample
validity_interpolated = self.critic(interpolated_img)
# Use Python partial to provide loss function with additional
# 'interpolated_samples' argument
partial_gp_loss = partial(self.gradient_penalty_loss,
interpolated_samples=interpolated_img)
partial_gp_loss.__name__ = 'gradient_penalty' # Keras requires function names
self.critic_model = Model(inputs=[real_img, z_disc],
outputs=[valid, fake, validity_interpolated])
self.critic_model.compile(
loss=[self.wasserstein,self.wasserstein, partial_gp_loss]
,optimizer=self.get_opti(self.critic_learning_rate)
,loss_weights=[1, 1, self.grad_weight]
)
#-------------------------------
# Construct Computational Graph
# for Generator
#-------------------------------
# For the generator we freeze the critic's layers
self.set_trainable(self.critic, False)
self.set_trainable(self.generator, True)
# Sampled noise for input to generator
model_input = Input(shape=(self.z_dim,))
# Generate images based of noise
img = self.generator(model_input)
# Discriminator determines validity
model_output = self.critic(img)
# Defines generator model
self.model = Model(model_input, model_output)
self.model.compile(optimizer=self.get_opti(self.generator_learning_rate)
, loss=self.wasserstein
)
self.set_trainable(self.critic, True)
def train_critic(self, x_train, batch_size, using_generator):
valid = np.ones((batch_size,1), dtype=np.float32)
fake = -np.ones((batch_size,1), dtype=np.float32)
dummy = np.zeros((batch_size, 1), dtype=np.float32) # Dummy gt for gradient penalty
if using_generator:
true_imgs = next(x_train)[0]
if true_imgs.shape[0] != batch_size:
true_imgs = next(x_train)[0]
else:
idx = np.random.randint(0, x_train.shape[0], batch_size)
true_imgs = x_train[idx]
noise = np.random.normal(0, 1, (batch_size, self.z_dim))
d_loss = self.critic_model.train_on_batch([true_imgs, noise], [valid, fake, dummy])
return d_loss
def train_generator(self, batch_size):
valid = np.ones((batch_size,1), dtype=np.float32)
noise = np.random.normal(0, 1, (batch_size, self.z_dim))
return self.model.train_on_batch(noise, valid)
def train(self, x_train, batch_size, epochs, run_folder, print_every_n_batches = 10
, n_critic = 5
, using_generator = False):
for epoch in range(self.epoch, self.epoch + epochs):
if epoch % 100 == 0:
critic_loops = 5
else:
critic_loops = n_critic
for _ in range(critic_loops):
d_loss = self.train_critic(x_train, batch_size, using_generator)
g_loss = self.train_generator(batch_size)
print ("%d (%d, %d) [D loss: (%.1f)(R %.1f, F %.1f, G %.1f)] [G loss: %.1f]" % (epoch, critic_loops, 1, d_loss[0], d_loss[1],d_loss[2],d_loss[3],g_loss))
self.d_losses.append(d_loss)
self.g_losses.append(g_loss)
# If at save interval => save generated image samples
if epoch % print_every_n_batches == 0:
self.sample_images(run_folder)
self.model.save_weights(os.path.join(run_folder, 'weights/weights-%d.h5' % (epoch)))
self.model.save_weights(os.path.join(run_folder, 'weights/weights.h5'))
self.save_model(run_folder)
self.epoch+=1
def sample_images(self, run_folder):
r, c = 5, 5
noise = np.random.normal(0, 1, (r * c, self.z_dim))
gen_imgs = self.generator.predict(noise)
#Rescale images 0 - 1
gen_imgs = 0.5 * (gen_imgs + 1)
gen_imgs = np.clip(gen_imgs, 0, 1)
fig, axs = plt.subplots(r, c, figsize=(15,15))
cnt = 0
for i in range(r):
for j in range(c):
axs[i,j].imshow(np.squeeze(gen_imgs[cnt, :,:,:]), cmap = 'gray_r')
axs[i,j].axis('off')
cnt += 1
fig.savefig(os.path.join(run_folder, "images/sample_%d.png" % self.epoch))
plt.close()
def plot_model(self, run_folder):
plot_model(self.model, to_file=os.path.join(run_folder ,'viz/model.png'), show_shapes = True, show_layer_names = True)
plot_model(self.critic, to_file=os.path.join(run_folder ,'viz/critic.png'), show_shapes = True, show_layer_names = True)
plot_model(self.generator, to_file=os.path.join(run_folder ,'viz/generator.png'), show_shapes = True, show_layer_names = True)
def save(self, folder):
with open(os.path.join(folder, 'params.pkl'), 'wb') as f:
pickle.dump([
self.input_dim
, self.critic_conv_filters
, self.critic_conv_kernel_size
, self.critic_conv_strides
, self.critic_batch_norm_momentum
, self.critic_activation
, self.critic_dropout_rate
, self.critic_learning_rate
, self.generator_initial_dense_layer_size
, self.generator_upsample
, self.generator_conv_filters
, self.generator_conv_kernel_size
, self.generator_conv_strides
, self.generator_batch_norm_momentum
, self.generator_activation
, self.generator_dropout_rate
, self.generator_learning_rate
, self.optimiser
, self.grad_weight
, self.z_dim
, self.batch_size
], f)
self.plot_model(folder)
def save_model(self, run_folder):
self.model.save(os.path.join(run_folder, 'model.h5'))
self.critic.save(os.path.join(run_folder, 'critic.h5'))
self.generator.save(os.path.join(run_folder, 'generator.h5'))
pickle.dump(self, open( os.path.join(run_folder, "obj.pkl"), "wb" ))
def load_weights(self, filepath):
self.model.load_weights(filepath)