-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_generation.py
363 lines (262 loc) · 11.9 KB
/
face_generation.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
# coding: utf-8
# # 人脸生成(Face Generation)
# 在该项目中,你将使用生成式对抗网络(Generative Adversarial Nets)来生成新的人脸图像。
# ### 获取数据
# 该项目将使用以下数据集:
# - MNIST
# - CelebA
# In[1]:
data_dir = './data'
import helper
helper.download_extract('mnist', data_dir)
helper.download_extract('celeba', data_dir)
# ## 探索数据(Explore the Data)
# ### MNIST
#
# In[2]:
show_n_images = 25
get_ipython().run_line_magic('matplotlib', 'inline')
import os
from glob import glob
from matplotlib import pyplot
mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L')
pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray')
# ### CelebA
# 一个包含 20 多万张名人图片及相关图片说明的数据集
# In[3]:
show_n_images = 25
mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB')
pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))
# ## 预处理数据(Preprocess the Data)
#
#
# ## 建立神经网络(Build the Neural Network)
#
#
# ### 检查 TensorFlow 版本并获取 GPU 型号
# In[4]:
from distutils.version import LooseVersion
import warnings
import tensorflow as tf
# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer. You are using {}'.format(tf.__version__)
print('TensorFlow Version: {}'.format(tf.__version__))
# Check for a GPU
if not tf.test.gpu_device_name():
warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
# ### 输入(Input)
# In[5]:
def model_inputs(image_width, image_height, image_channels, z_dim):
"""
Create the model inputs
:param image_width: The input image width
:param image_height: The input image height
:param image_channels: The number of image channels
:param z_dim: The dimension of Z
:return: Tuple of (tensor of real input images, tensor of z data, learning rate)
"""
input_real = tf.placeholder(tf.float32,[None, image_width, image_height, image_channels])
input_z = tf.placeholder(tf.float32, [None, z_dim])
learning_rate = tf.placeholder(tf.float32)
return input_real, input_z, learning_rate
# ### 辨别器(Discriminator)
# In[6]:
def discriminator(images, reuse=False):
"""
Create the discriminator network
:param image: Tensor of input image(s)
:param reuse: Boolean if the weights should be reused
:return: Tuple of (tensor output of the discriminator, tensor logits of the discriminator)
"""
with tf.variable_scope('discriminator', reuse=reuse):
alpha = 0.1
dropout = 0.6
#28*28*3
x1 = tf.layers.conv2d(images, 64, 5, strides=2 ,padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
relu1 = tf.maximum(alpha * x1, x1)
drop1 = tf.nn.dropout(relu1, dropout)
#14*14*64
x2 = tf.layers.conv2d(drop1, 128, 5, strides=2 ,padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
bn2 = tf.layers.batch_normalization(x2, training=True)
relu2 = tf.maximum(alpha * bn2, bn2)
drop2 = tf.nn.dropout(relu2, dropout)
#7*7*128
x3 = tf.layers.conv2d(drop2, 256, 5, strides=2 ,padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
bn3 = tf.layers.batch_normalization(x3, training=True)
relu3 = tf.maximum(alpha * bn3, bn3)
drop3 = tf.nn.dropout(relu3, dropout)
#4*4*256
# x4 = tf.layers.conv2d(drop3, 512, 5, strides=1, padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
# bn4 = tf.layers.batch_normalization(x4, training=True)
# relu4 = tf.maximum(alpha * bn4, bn4)
# drop4 = tf.nn.dropout(relu4, dropout)
#4*4*512
flat = tf.reshape(drop3, [-1, 4*4*256])
logits = tf.layers.dense(flat, 1, kernel_initializer=tf.contrib.layers.xavier_initializer())
out = tf.nn.sigmoid(logits)
return out, logits
# ### 生成器(Generator)
# In[7]:
def generator(z, out_channel_dim, is_train=True):
"""
Create the generator network
:param z: Input z
:param out_channel_dim: The number of channels in the output image
:param is_train: Boolean if generator is being used for training
:return: The tensor output of the generator
"""
with tf.variable_scope('generator', reuse=not is_train):
alpha = 0.1
dropout = 0.6
#fully connected and reshape
x1 = tf.layers.dense(z, 7*7*256, kernel_initializer=tf.contrib.layers.xavier_initializer())
x1 = tf.layers.batch_normalization(x1, training=is_train)
x1 = tf.nn.relu(x1)
x1 = tf.reshape(x1, [-1, 7, 7, 256])
x1 = tf.nn.dropout(x1, dropout)
#7*7*256
x2 = tf.image.resize_nearest_neighbor(x1, (14, 14))
x2 = tf.layers.conv2d_transpose(x2, 128, 5, strides=1, padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
x2 = tf.layers.batch_normalization(x2, training=is_train)
x2 = tf.nn.relu(x2)
#n14*14*128
x3 = tf.image.resize_nearest_neighbor(x2, (28, 28))
x3 = tf.layers.conv2d_transpose(x3, 64, 5, strides=1, padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
x3 = tf.layers.batch_normalization(x3, training=is_train)
x3 = tf.nn.relu(x3)
#28*28*64
# x4 = tf.layers.conv2d_transpose(x3, 32, 5, strides=1, padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
# x4 = tf.layers.batch_normalization(x4, training=is_train)
# x4 = tf.nn.relu(x4)
#28*28*32
#output layer
logits = tf.layers.conv2d_transpose(x3, out_channel_dim, 5, strides=1, padding='same', kernel_initializer=tf.contrib.layers.xavier_initializer())
out = tf.nn.tanh(logits)
return out
# ### 损失函数(Loss)
# In[8]:
def model_loss(input_real, input_z, out_channel_dim):
"""
Get the loss for the discriminator and generator
:param input_real: Images from the real dataset
:param input_z: Z input
:param out_channel_dim: The number of channels in the output image
:return: A tuple of (discriminator loss, generator loss)
"""
g_model = generator(input_z, out_channel_dim)
d_model_real, d_logits_real = discriminator(input_real)
d_model_fake, d_logits_fake = discriminator(g_model, reuse=True)
d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real, labels=tf.ones_like(d_model_real) * 0.9))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.zeros_like(d_model_fake)))
d_loss = d_loss_real + d_loss_fake
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.ones_like(d_model_fake)))
return d_loss, g_loss
# ### 优化(Optimization)
# In[9]:
def model_opt(d_loss, g_loss, learning_rate, beta1):
"""
Get optimization operations
:param d_loss: Discriminator loss Tensor
:param g_loss: Generator loss Tensor
:param learning_rate: Learning Rate Placeholder
:param beta1: The exponential decay rate for the 1st moment in the optimizer
:return: A tuple of (discriminator training operation, generator training operation)
"""
# TODO: Implement Function
t_vars = tf.trainable_variables()
g_vars = [var for var in t_vars if var.name.startswith('generator')]
d_vars = [var for var in t_vars if var.name.startswith('discriminator')]
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
d_train_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(d_loss, var_list=d_vars)
g_train_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(g_loss, var_list=g_vars)
return d_train_opt, g_train_opt
# ## 训练神经网络(Neural Network Training)
# ### 输出显示
# In[10]:
import numpy as np
def show_generator_output(sess, n_images, input_z, out_channel_dim, image_mode):
"""
Show example output for the generator
:param sess: TensorFlow session
:param n_images: Number of Images to display
:param input_z: Input Z Tensor
:param out_channel_dim: The number of channels in the output image
:param image_mode: The mode to use for images ("RGB" or "L")
"""
cmap = None if image_mode == 'RGB' else 'gray'
z_dim = input_z.get_shape().as_list()[-1]
example_z = np.random.uniform(-1, 1, size=[n_images, z_dim])
samples = sess.run(
generator(input_z, out_channel_dim, False),
feed_dict={input_z: example_z})
images_grid = helper.images_square_grid(samples, image_mode)
pyplot.imshow(images_grid, cmap=cmap)
pyplot.show()
# ### 训练
# In[11]:
def train(epoch_count, batch_size, z_dim, learning_rate, beta1, get_batches, data_shape, data_image_mode):
"""
Train the GAN
:param epoch_count: Number of epochs
:param batch_size: Batch Size
:param z_dim: Z dimension
:param learning_rate: Learning Rate
:param beta1: The exponential decay rate for the 1st moment in the optimizer
:param get_batches: Function to get batches
:param data_shape: Shape of the data
:param data_image_mode: The image mode to use for images ("RGB" or "L")
"""
# Build Model
input_real, input_z, _ = model_inputs(data_shape[1], data_shape[2], data_shape[3], z_dim)
d_loss, g_loss = model_loss(input_real, input_z, data_shape[3])
global_step = tf.Variable(0, trainable=False)
d_train_opt, _ = model_opt(d_loss, g_loss, learning_rate, beta1)
_, g_train_opt = model_opt(d_loss, g_loss, learning_rate, beta1)
saver = tf.train.Saver()
steps = 0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch_i in range(epoch_count):
for batch_images in get_batches(batch_size):
# Train Model
steps += 1
batch_z = np.random.uniform(-1, 1, size=[batch_size, z_dim])
batch_images = batch_images * 2
#Run optimizers
sess.run(d_train_opt, feed_dict={input_real: batch_images, input_z: batch_z})
sess.run(g_train_opt, feed_dict={input_real: batch_images, input_z: batch_z})
if(steps % 10 == 0):
#Get and print losses
train_loss_d = sess.run(d_loss, feed_dict={input_real: batch_images, input_z: batch_z})
train_loss_g = sess.run(g_loss, feed_dict={input_z: batch_z})
print("Epoch {}/{}...".format(epoch_i+1, epoch_count),
"Discriminator loss: {:.4f}".format(train_loss_d),
"Generator loss: {:.4f}".format(train_loss_g))
if(steps % 100 == 0):
show_generator_output(sess, 25, input_z, data_shape[3], data_image_mode)
# ### MNIST
#
# In[12]:
batch_size = 32
z_dim = 100
learning_rate = 0.0008
beta1 = 0.5
epochs = 2
mnist_dataset = helper.Dataset('mnist', glob(os.path.join(data_dir, 'mnist/*.jpg')))
with tf.Graph().as_default():
train(epochs, batch_size, z_dim, learning_rate, beta1, mnist_dataset.get_batches,
mnist_dataset.shape, mnist_dataset.image_mode)
# ### CelebA
#
# In[13]:
batch_size = 32
z_dim = 100
learning_rate = 0.0008
beta1 = 0.5
epochs = 1
celeba_dataset = helper.Dataset('celeba', glob(os.path.join(data_dir, 'img_align_celeba/*.jpg')))
with tf.Graph().as_default():
train(epochs, batch_size, z_dim, learning_rate, beta1, celeba_dataset.get_batches,
celeba_dataset.shape, celeba_dataset.image_mode)