-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain_fashion_original.py
332 lines (251 loc) · 11.8 KB
/
train_fashion_original.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import datetime
from functools import partial
import json
import traceback
import imlib as im
import numpy as np
import pylib
import tensorflow as tf
import tflib as tl
import data_fashion_original as data
import models
import sys
from importlib import reload
reload(sys)
#sys.setdefaultencoding('utf8')
# ==============================================================================
# = param =
# ==============================================================================
parser = argparse.ArgumentParser()
# model
att_default = ["无袖","短袖","盖肩袖","中袖/5分袖","6分袖/7分袖/8分袖","9分袖","长袖","红色","粉色","橙色","黄色","绿色","蓝色","紫色","灰色","黑色","白色","米色","棕色","褐色","咖色","驼色","杏色","青色","藏青色","银色","花色","金色"]
parser.add_argument('--atts', dest='atts', default=att_default, choices=data.Celeba.att_dict.keys(), nargs='+', help='attributes to learn')
parser.add_argument('--img_size', dest='img_size', type=int, default=128)
parser.add_argument('--shortcut_layers', dest='shortcut_layers', type=int, default=1)
parser.add_argument('--inject_layers', dest='inject_layers', type=int, default=0)
parser.add_argument('--enc_dim', dest='enc_dim', type=int, default=64)
parser.add_argument('--dec_dim', dest='dec_dim', type=int, default=64)
parser.add_argument('--dis_dim', dest='dis_dim', type=int, default=64)
parser.add_argument('--dis_fc_dim', dest='dis_fc_dim', type=int, default=1024)
parser.add_argument('--enc_layers', dest='enc_layers', type=int, default=5)
parser.add_argument('--dec_layers', dest='dec_layers', type=int, default=5)
parser.add_argument('--dis_layers', dest='dis_layers', type=int, default=5)
# training
parser.add_argument('--mode', dest='mode', default='wgan', choices=['wgan', 'lsgan', 'dcgan'])
parser.add_argument('--epoch', dest='epoch', type=int, default=200, help='# of epochs')
parser.add_argument('--batch_size', dest='batch_size', type=int, default=32)
parser.add_argument('--lr', dest='lr', type=float, default=0.0002, help='learning rate')
parser.add_argument('--n_d', dest='n_d', type=int, default=5, help='# of d updates per g update')
parser.add_argument('--b_distribution', dest='b_distribution', default='none', choices=['none', 'uniform', 'truncated_normal'])
parser.add_argument('--thres_int', dest='thres_int', type=float, default=0.5)
parser.add_argument('--test_int', dest='test_int', type=float, default=1.0)
parser.add_argument('--n_sample', dest='n_sample', type=int, default=10, help='# of sample images')
# others
parser.add_argument('--use_cropped_img', dest='use_cropped_img', action='store_true')
parser.add_argument('--experiment_name', dest='experiment_name', default=datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
parser.add_argument('--experiment_dir', dest='experiment_dir', default=datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
args = parser.parse_args()
# model
atts = args.atts
n_att = len(atts)
img_size = args.img_size
shortcut_layers = args.shortcut_layers
inject_layers = args.inject_layers
enc_dim = args.enc_dim
dec_dim = args.dec_dim
dis_dim = args.dis_dim
dis_fc_dim = args.dis_fc_dim
enc_layers = args.enc_layers
dec_layers = args.dec_layers
dis_layers = args.dis_layers
# training
mode = args.mode
epoch = args.epoch
batch_size = args.batch_size
lr_base = args.lr
n_d = args.n_d
b_distribution = args.b_distribution
thres_int = args.thres_int
test_int = args.test_int
n_sample = args.n_sample
# others
use_cropped_img = args.use_cropped_img
experiment_name = args.experiment_name
experiment_dir = args.experiment_dir
pylib.mkdir('./' + experiment_dir + '/%s' % experiment_name)
with open('./' + experiment_dir + '/%s/setting.txt' % experiment_name, 'w') as f:
f.write(json.dumps(vars(args), indent=4, separators=(',', ':')))
# ==============================================================================
# = graphs =
# ==============================================================================
# data
# added for memory allocation Qing
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
#sess = tl.session()
tr_data = data.Celeba('./data', atts, img_size, batch_size, part='train', sess=sess, crop=not use_cropped_img)
val_data = data.Celeba('./data', atts, img_size, n_sample, part='val', shuffle=False, sess=sess, crop=not use_cropped_img)
# models
Genc = partial(models.Genc, dim=enc_dim, n_layers=enc_layers)
Gdec = partial(models.Gdec, dim=dec_dim, n_layers=dec_layers, shortcut_layers=shortcut_layers, inject_layers=inject_layers)
D = partial(models.D, n_att=n_att, dim=dis_dim, fc_dim=dis_fc_dim, n_layers=dis_layers)
# inputs
lr = tf.placeholder(dtype=tf.float32, shape=[])
xa = tr_data.batch_op[0]
a = tr_data.batch_op[1]
b = tf.random_shuffle(a)
_a = (tf.to_float(a) * 2 - 1) * thres_int
if b_distribution == 'none':
_b = (tf.to_float(b) * 2 - 1) * thres_int
elif b_distribution == 'uniform':
_b = (tf.to_float(b) * 2 - 1) * tf.random_uniform(tf.shape(b)) * (2 * thres_int)
elif b_distribution == 'truncated_normal':
_b = (tf.to_float(b) * 2 - 1) * (tf.truncated_normal(tf.shape(b)) + 2) / 4.0 * (2 * thres_int)
xa_sample = tf.placeholder(tf.float32, shape=[None, img_size, img_size, 3])
_b_sample = tf.placeholder(tf.float32, shape=[None, n_att])
# generate
z = Genc(xa)
print('z shape')
print(z[-1].shape)
#import pdb
#pdb.set_trace()
#print (z[-1])
xb_ = Gdec(z, _b)
print('xb_ shape')
print(xb_.shape)
with tf.control_dependencies([xb_]):
xa_ = Gdec(z, _a)
# discriminate
xa_logit_gan, xa_logit_att = D(xa)
xb__logit_gan, xb__logit_att = D(xb_)
# discriminator losses
if mode == 'wgan': # wgan-gp
wd = tf.reduce_mean(xa_logit_gan) - tf.reduce_mean(xb__logit_gan)
d_loss_gan = -wd # adv_d
gp = models.gradient_penalty(D, xa, xb_)
elif mode == 'lsgan': # lsgan-gp
xa_gan_loss = tf.losses.mean_squared_error(tf.ones_like(xa_logit_gan), xa_logit_gan)
xb__gan_loss = tf.losses.mean_squared_error(tf.zeros_like(xb__logit_gan), xb__logit_gan)
d_loss_gan = xa_gan_loss + xb__gan_loss
gp = models.gradient_penalty(D, xa)
elif mode == 'dcgan': # dcgan-gp
xa_gan_loss = tf.losses.sigmoid_cross_entropy(tf.ones_like(xa_logit_gan), xa_logit_gan)
xb__gan_loss = tf.losses.sigmoid_cross_entropy(tf.zeros_like(xb__logit_gan), xb__logit_gan)
d_loss_gan = xa_gan_loss + xb__gan_loss
gp = models.gradient_penalty(D, xa)
xa_loss_att = tf.losses.sigmoid_cross_entropy(a, xa_logit_att) # L_cls_a
d_loss = d_loss_gan + gp * 10.0 + xa_loss_att # adv_d + L_cls_a (Dec)
# generator losses
if mode == 'wgan':
xb__loss_gan = -tf.reduce_mean(xb__logit_gan) # adv_g
elif mode == 'lsgan':
xb__loss_gan = tf.losses.mean_squared_error(tf.ones_like(xb__logit_gan), xb__logit_gan)
elif mode == 'dcgan':
xb__loss_gan = tf.losses.sigmoid_cross_entropy(tf.ones_like(xb__logit_gan), xb__logit_gan)
xb__loss_att = tf.losses.sigmoid_cross_entropy(b, xb__logit_att) # L_cls_b
xa__loss_rec = tf.losses.absolute_difference(xa, xa_) # rec_a
g_loss = xb__loss_gan + xa__loss_rec * 100.0
g_b_att_loss = xb__loss_att * 3.0
# optim
d_var = tl.trainable_variables('D')
d_step = tf.train.AdamOptimizer(lr, beta1=0.5).minimize(d_loss, var_list=d_var)
g_var = tl.trainable_variables('G')
g_step = tf.train.AdamOptimizer(lr, beta1=0.5).minimize(g_loss, var_list=g_var)
g_dec_var = tl.trainable_variables('Gdec')
g_dec_step = tf.train.AdamOptimizer(lr, beta1=0.5).minimize(g_b_att_loss, var_list=g_dec_var)
# summary
d_summary = tl.summary({
d_loss_gan: 'd_loss_gan',
gp: 'gp',
xa_loss_att: 'xa_loss_att',
}, scope='D')
lr_summary = tl.summary({lr: 'lr'}, scope='Learning_Rate')
g_summary = tl.summary({
xb__loss_gan: 'xb__loss_gan',
xb__loss_att: 'xb__loss_att',
xa__loss_rec: 'xa__loss_rec',
}, scope='G')
g_dec_summary = tl.summary({
xb__loss_att: 'xb__loss_att',
}, scope='Gdec')
d_summary = tf.summary.merge([d_summary, lr_summary])
# sample
x_sample = Gdec(Genc(xa_sample, is_training=False), _b_sample, is_training=False)
# ==============================================================================
# = train =
# ==============================================================================
# iteration counter
it_cnt, update_cnt = tl.counter()
# saver
saver = tf.train.Saver(max_to_keep=1)
# summary writer
summary_writer = tf.summary.FileWriter('./' + experiment_dir + '/%s/summaries' % experiment_name, sess.graph)
# initialization
ckpt_dir = './' + experiment_dir + '/%s/checkpoints' % experiment_name
pylib.mkdir(ckpt_dir)
try:
tl.load_checkpoint(ckpt_dir, sess)
except:
sess.run(tf.global_variables_initializer())
# train
try:
# data for sampling
xa_sample_ipt, a_sample_ipt = val_data.get_next()
b_sample_ipt_list = [a_sample_ipt] # the first is for reconstruction
for i in range(len(atts)):
tmp = np.array(a_sample_ipt, copy=True)
tmp[:, i] = 1 - tmp[:, i] # inverse attribute
tmp = data.Celeba.check_attribute_conflict(tmp, atts[i], atts)
b_sample_ipt_list.append(tmp)
it_per_epoch = len(tr_data) // (batch_size * (n_d + 1))
max_it = epoch * it_per_epoch
for it in range(sess.run(it_cnt), max_it):
with pylib.Timer(is_output=False) as t:
sess.run(update_cnt)
# which epoch
epoch = it // it_per_epoch
it_in_epoch = it % it_per_epoch + 1
# learning rate
lr_ipt = lr_base / (10 ** (epoch // 100))
# train D
for i in range(n_d):
d_summary_opt, _ = sess.run([d_summary, d_step], feed_dict={lr: lr_ipt})
summary_writer.add_summary(d_summary_opt, it)
# train G
g_summary_opt, _ = sess.run([g_summary, g_step], feed_dict={lr: lr_ipt})
summary_writer.add_summary(g_summary_opt, it)
# train G_dec
g_dec_summary_opt, _ = sess.run([g_dec_summary, g_dec_step], feed_dict={lr: lr_ipt})
summary_writer.add_summary(g_dec_summary_opt, it)
# display
if (it + 1) % 1 == 0:
print("Epoch: (%3d) (%5d/%5d) Time: %s!" % (epoch, it_in_epoch, it_per_epoch, t))
# save
if (it + 1) % 1000 == 0:
save_path = saver.save(sess, '%s/Epoch_(%d)_(%dof%d).ckpt' % (ckpt_dir, epoch, it_in_epoch, it_per_epoch))
print('Model is saved at %s!' % save_path)
# sample
if (it + 1) % 70 == 0:
x_sample_opt_list = [xa_sample_ipt, np.full((n_sample, img_size, img_size // 10, 3), -1.0)]
for i, b_sample_ipt in enumerate(b_sample_ipt_list):
_b_sample_ipt = (b_sample_ipt * 2 - 1) * thres_int
if i > 0: # i == 0 is for reconstruction
_b_sample_ipt[..., i - 1] = _b_sample_ipt[..., i - 1] * test_int / thres_int
x_sample_opt_list.append(
sess.run(x_sample, feed_dict={xa_sample: xa_sample_ipt, _b_sample: _b_sample_ipt}))
sample = np.concatenate(x_sample_opt_list, 2)
save_dir = './' + experiment_dir + '/%s/sample_training' % experiment_name
pylib.mkdir(save_dir)
im.imwrite(im.immerge(sample, n_sample, 1), '%s/Epoch_%d_%dof%d.jpg' % (save_dir, epoch, it_in_epoch, it_per_epoch))
except:
traceback.print_exc()
finally:
save_path = saver.save(sess, '%s/Epoch_(%d)_(%dof%d).ckpt' % (ckpt_dir, epoch, it_in_epoch, it_per_epoch))
print('Model is saved at %s!' % save_path)
sess.close()