-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathebgan.py
218 lines (172 loc) · 8.22 KB
/
ebgan.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
import argparse
import tensorflow as tf
import glob
# import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow.keras.layers as layers
import time
parser = argparse.ArgumentParser()
parser.add_argument("--n_epochs", type=int, default=200, help="number of epochs of training")
parser.add_argument("--freq", type=int, default=1, help="number of epochs of saving")
parser.add_argument("--batch_size", type=int, default=512, help="size of the batches")
parser.add_argument("--buffer_size", type=int, default=60000, help="size of the buffers")
parser.add_argument("--lr", type=float, default=0.0002, help="adam: learning rate")
parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=100, help="dimensionality of the latent space")
parser.add_argument("--img_size", type=int, default=28, help="size of each image dimension")
parser.add_argument("--channels", type=int, default=1, help="number of image channels")
parser.add_argument("--sample_interval", type=int, default=400, help="interval betwen image samples")
opt = parser.parse_args()
print(opt)
img_shape = (opt.img_size, opt.img_size, opt.channels)
# EBGAN Parameter
pt_loss_weight = 0.1
margin = max(1, opt.batch_size/64.)
def pullaway_loss(embeddings):
"""
Pull Away loss calculation
:param embeddings: The embeddings to be orthogonalized for varied faces. Shape [batch_size, embeddings_dim]
:return: pull away term loss
"""
norm = tf.sqrt(tf.math.reduce_sum(tf.square(embeddings), 1, keepdims=True))
normalized_embeddings = embeddings / norm
similarity = tf.matmul(normalized_embeddings, normalized_embeddings, transpose_b=True)
batch_size = tf.cast(tf.shape(embeddings)[0], tf.float32)
pt_loss = (tf.reduce_sum(similarity) - batch_size) / (batch_size * (batch_size - 1))
return pt_loss
# data load & preprocessing
(train_x, _), (_, _) = tf.keras.datasets.mnist.load_data()
train_x= train_x.reshape(train_x.shape[0], 28, 28, 1).astype('float32')
BUFFER_SIZE=train_x.shape[0]
train_x = train_x/255.
train_ds = tf.data.Dataset.from_tensor_slices(train_x).shuffle(BUFFER_SIZE).batch(opt.batch_size)
num_examples_to_generate = 16
# We will reuse this seed overtime (so it's easier)
# to visualize progress in the animated GIF)
seed = tf.random.normal([num_examples_to_generate, opt.latent_dim])
# define discriminator
class Discriminator(tf.keras.Model):
def __init__(self, batch_size=64, is_training=True):
super(Discriminator, self).__init__(name='discriminator')
self.batch_size = batch_size
self.is_training = is_training
self.bn_1 = layers.BatchNormalization(trainable=self.is_training)
self.bn_2 = layers.BatchNormalization(trainable=self.is_training)
self.fc_1 = layers.Dense(32)
self.fc_2 = layers.Dense(64*14*14)
self.conv_1 = layers.Conv2D(64, 4,strides=2, padding='same')
self.up_conv_1 = layers.Conv2DTranspose(1, 4, 2,padding='same')
def call(self, inputs, training):
x = self.conv_1(inputs)
x = layers.ReLU()(x)
x = layers.Flatten()(x)
x = self.fc_1(x)
x = self.bn_1(x, training)
code = layers.ReLU()(x)
x = self.fc_2(code)
x = self.bn_2(x, training)
x = layers.ReLU()(x)
x = layers.Reshape((14, 14, 64))(x)
x = self.up_conv_1(x)
out = tf.keras.activations.sigmoid(x)
recon_error = tf.math.sqrt(2 * tf.nn.l2_loss(out - inputs)) / self.batch_size
return out, recon_error, code
class Generator(tf.keras.Model):
def __init__(self, is_training=True):
super(Generator, self).__init__(name='generator')
self.is_training = is_training
self.fc_1 = layers.Dense(1024)
self.fc_2 = layers.Dense(128*7*7)
self.bn_1 = layers.BatchNormalization(trainable=self.is_training)
self.bn_2 = layers.BatchNormalization(trainable=self.is_training)
self.bn_3 = layers.BatchNormalization(trainable=self.is_training)
self.up_conv_1 = layers.Conv2DTranspose(64, 4, 2,padding='same')
self.up_conv_2 = layers.Conv2DTranspose(1, 4, 2,padding='same')
def call(self, inputs, training):
x = self.fc_1(inputs)
x = self.bn_1(x, training)
x = layers.ReLU()(x)
x = self.fc_2(x)
x = self.bn_2(x, training)
x = layers.ReLU()(x)
x = layers.Reshape((7, 7, 128))(x)
x = self.up_conv_1(x)
x = self.bn_3(x, training)
x = layers.ReLU()(x)
x = self.up_conv_2(x)
x = tf.keras.activations.sigmoid(x)
return x
def get_random_z(z_dim, batch_size):
return tf.random.uniform([batch_size, z_dim], minval=-1, maxval=1)
# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()
generator_optimizer = tf.keras.optimizers.Adam(opt.lr, beta_1=0.5, beta_2=0.999)
discriminator_optimizer = tf.keras.optimizers.Adam(opt.lr, beta_1=0.5, beta_2=0.999)
# metrics setting
g_loss_metrics = tf.metrics.Mean(name='g_loss')
d_loss_metrics = tf.metrics.Mean(name='d_loss')
total_loss_metrics = tf.metrics.Mean(name='total_loss')
checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,
discriminator_optimizer=discriminator_optimizer,
generator=generator,
discriminator=discriminator)
@tf.function
def train_step(images):
noise = get_random_z(opt.latent_dim, images.shape[0])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
D_real_img, D_real_err, D_real_code = discriminator(images, training=True)
fake_imgs = generator(noise, training=True)
D_fake_img, D_fake_err, D_fake_code = discriminator(fake_imgs, training=True)
# get loss for discriminator
d_loss = D_real_err + tf.maximum(margin - D_fake_err, 0)
# get loss for generator
g_loss = D_fake_err + pt_loss_weight * pullaway_loss(D_fake_code)
gradients_of_generator = gen_tape.gradient(g_loss, generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(d_loss, discriminator.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
return g_loss, d_loss
# ----------
# Training
# ----------
def train(dataset, epochs):
for epoch in range(epochs):
start = time.time()
for batch_idx, image_batch in enumerate(dataset):
g_loss, d_loss = train_step(image_batch)
g_loss_metrics(g_loss)
d_loss_metrics(d_loss)
total_loss_metrics(g_loss + d_loss)
template = '[Epoch{}/{}], Batch[{}/{}] D_loss={:.5f} G_loss={:.5f} Total_loss={:.5f}'
print(template.format(epoch, epochs, batch_idx, len(dataset), d_loss_metrics.result(),
g_loss_metrics.result(), total_loss_metrics.result()))
g_loss_metrics.reset_states()
d_loss_metrics.reset_states()
total_loss_metrics.reset_states()
# Produce images for the GIF as we go
generate_and_save_images(generator,epoch + 1, seed)
# Save the model every 15 epochs
if (epoch + 1) % opt.freq == 0:
checkpoint.save(file_prefix=checkpoint_prefix)
print('Time for epoch {} is {} sec'.format(epoch + 1, time.time() - start))
# # Generate after the final epoch
generate_and_save_images(generator,epochs,seed)
def generate_and_save_images(model, epoch, test_input):
# Notice `training` is set to False.
# This is so all layers run in inference mode (batchnorm).
predictions = model(test_input, training=False)
fig = plt.figure(figsize=(4, 4))
for i in range(predictions.shape[0]):
plt.subplot(4, 4, i + 1)
plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
plt.axis('off')
plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
# plt.show()
if __name__ == "__main__":
train(train_ds,opt.n_epochs)