forked from jesse1029/Fake-Face-Images-Detection-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_celeba_wgan.py
145 lines (116 loc) · 4.3 KB
/
train_celeba_wgan.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
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import glob
import utils
import traceback
import numpy as np
import tensorflow as tf
import models_64x64 as models
""" param """
epoch = 50
batch_size = 64
lr = 0.0002
z_dim = 100
clip = 0.01
n_critic = 5
gpu_id = 3
''' data '''
# you should prepare your own data in ./data/img_align_celeba
# celeba original size is [218, 178, 3]
def preprocess_fn(img):
crop_size = 108
re_size = 64
img = tf.image.crop_to_bounding_box(img, (218 - crop_size) // 2, (178 - crop_size) // 2, crop_size, crop_size)
img = tf.to_float(tf.image.resize_images(img, [re_size, re_size], method=tf.image.ResizeMethod.BICUBIC)) / 127.5 - 1
return img
img_paths = glob.glob('./data/img_align_celeba/img_align_celeba/')
data_pool = utils.DiskImageData('./data/img_align_celeba/img_align_celeba/', batch_size, shape=[218, 178, 3], preprocess_fn=preprocess_fn)
""" graphs """
with tf.device('/gpu:%d' % gpu_id):
''' models '''
generator = models.generator
discriminator = models.discriminator
''' graph '''
# inputs
real = tf.placeholder(tf.float32, shape=[None, 64, 64, 3])
z = tf.placeholder(tf.float32, shape=[None, z_dim])
# generate
fake = generator(z, reuse=False)
# dicriminate
r_logit = discriminator(real, reuse=False)
f_logit = discriminator(fake)
# losses
wd = tf.reduce_mean(r_logit) - tf.reduce_mean(f_logit)
d_loss = -wd
g_loss = -tf.reduce_mean(f_logit)
# otpims
d_var = utils.trainable_variables('discriminator')
g_var = utils.trainable_variables('generator')
d_step_ = tf.train.RMSPropOptimizer(learning_rate=lr).minimize(d_loss, var_list=d_var)
with tf.control_dependencies([d_step_]):
d_step = tf.group(*(tf.assign(var, tf.clip_by_value(var, -clip, clip)) for var in d_var))
g_step = tf.train.RMSPropOptimizer(learning_rate=lr).minimize(g_loss, var_list=g_var)
# summaries
d_summary = utils.summary({wd: 'wd'})
g_summary = utils.summary({g_loss: 'g_loss'})
# sample
f_sample = generator(z, training=False)
""" train """
''' init '''
# session
sess = utils.session()
# iteration counter
it_cnt, update_cnt = utils.counter()
# saver
saver = tf.train.Saver(max_to_keep=5)
# summary writer
summary_writer = tf.summary.FileWriter('./summaries/celeba_wgan', sess.graph)
''' initialization '''
ckpt_dir = './checkpoints/celeba_wgan'
utils.mkdir(ckpt_dir + '/')
if not utils.load_checkpoint(ckpt_dir, sess):
sess.run(tf.global_variables_initializer())
''' train '''
try:
z_ipt_sample = np.random.normal(size=[100, z_dim])
batch_epoch = len(data_pool) // (batch_size * n_critic)
max_it = epoch * batch_epoch
for it in range(sess.run(it_cnt), max_it):
sess.run(update_cnt)
# which epoch
epoch = it // batch_epoch
it_epoch = it % batch_epoch + 1
# train D
if it < 25:
c_iter = 100
else:
c_iter = n_critic
for i in range(n_critic):
# batch data
real_ipt = data_pool.batch()
z_ipt = np.random.normal(size=[batch_size, z_dim])
d_summary_opt, _ = sess.run([d_summary, d_step], feed_dict={real: real_ipt, z: z_ipt})
summary_writer.add_summary(d_summary_opt, it)
# train G
z_ipt = np.random.normal(size=[batch_size, z_dim])
g_summary_opt, _ = sess.run([g_summary, g_step], feed_dict={z: z_ipt})
summary_writer.add_summary(g_summary_opt, it)
# display
if it % 1 == 0:
print("Epoch: (%3d) (%5d/%5d)" % (epoch, it_epoch, batch_epoch))
# save
if (it + 1) % 1000 == 0:
save_path = saver.save(sess, '%s/Epoch_(%d)_(%dof%d).ckpt' % (ckpt_dir, epoch, it_epoch, batch_epoch))
print('Model saved in file: % s' % save_path)
# sample
if (it + 1) % 100 == 0:
f_sample_opt = sess.run(f_sample, feed_dict={z: z_ipt_sample})
save_dir = './sample_images_while_training/celeba_wgan'
utils.mkdir(save_dir + '/')
utils.imwrite(utils.immerge(f_sample_opt, 10, 10), '%s/Epoch_(%d)_(%dof%d).jpg' % (save_dir, epoch, it_epoch, batch_epoch))
except Exception, e:
traceback.print_exc()
finally:
print(" [*] Close main session!")
sess.close()