forked from ZZUTK/Face-Aging-CAAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaceAging.py
741 lines (686 loc) · 31.5 KB
/
FaceAging.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# This code is an alternative implementation of the paper by
# Zhifei Zhang, Yang Song, and Hairong Qi. "Age Progression/Regression by Conditional Adversarial Autoencoder."
# IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2017.
#
# Date: Mar. 24th, 2017
#
# Please cite above paper if you use this code
#
from __future__ import division
import os
import time
from glob import glob
import tensorflow as tf
import numpy as np
from scipy.io import savemat
from ops import *
class FaceAging(object):
def __init__(self,
session, # TensorFlow session
size_image=128, # size the input images
size_kernel=5, # size of the kernels in convolution and deconvolution
size_batch=100, # mini-batch size for training and testing, must be square of an integer
num_input_channels=3, # number of channels of input images
num_encoder_channels=64, # number of channels of the first conv layer of encoder
num_z_channels=50, # number of channels of the layer z (noise or code)
num_categories=10, # number of categories (age segments) in the training dataset
num_gen_channels=1024, # number of channels of the first deconv layer of generator
enable_tile_label=True, # enable to tile the label
tile_ratio=1.0, # ratio of the length between tiled label and z
is_training=True, # flag for training or testing mode
save_dir='./save', # path to save checkpoints, samples, and summary
dataset_name='UTKFace' # name of the dataset in the folder ./data
):
self.session = session
self.image_value_range = (-1, 1)
self.size_image = size_image
self.size_kernel = size_kernel
self.size_batch = size_batch
self.num_input_channels = num_input_channels
self.num_encoder_channels = num_encoder_channels
self.num_z_channels = num_z_channels
self.num_categories = num_categories
self.num_gen_channels = num_gen_channels
self.enable_tile_label = enable_tile_label
self.tile_ratio = tile_ratio
self.is_training = is_training
self.save_dir = save_dir
self.dataset_name = dataset_name
# ************************************* input to graph ********************************************************
self.input_image = tf.placeholder(
tf.float32,
[self.size_batch, self.size_image, self.size_image, self.num_input_channels],
name='input_images'
)
self.age = tf.placeholder(
tf.float32,
[self.size_batch, self.num_categories],
name='age_labels'
)
self.gender = tf.placeholder(
tf.float32,
[self.size_batch, 2],
name='gender_labels'
)
self.z_prior = tf.placeholder(
tf.float32,
[self.size_batch, self.num_z_channels],
name='z_prior'
)
# ************************************* build the graph *******************************************************
print '\n\tBuilding graph ...'
# encoder: input image --> z
self.z = self.encoder(
image=self.input_image
)
# generator: z + label --> generated image
self.G = self.generator(
z=self.z,
y=self.age,
gender=self.gender,
enable_tile_label=self.enable_tile_label,
tile_ratio=self.tile_ratio
)
# discriminator on z
self.D_z, self.D_z_logits = self.discriminator_z(
z=self.z,
is_training=self.is_training
)
# discriminator on G
self.D_G, self.D_G_logits = self.discriminator_img(
image=self.G,
y=self.age,
gender=self.gender,
is_training=self.is_training
)
# discriminator on z_prior
self.D_z_prior, self.D_z_prior_logits = self.discriminator_z(
z=self.z_prior,
is_training=self.is_training,
reuse_variables=True
)
# discriminator on input image
self.D_input, self.D_input_logits = self.discriminator_img(
image=self.input_image,
y=self.age,
gender=self.gender,
is_training=self.is_training,
reuse_variables=True
)
# ************************************* loss functions *******************************************************
# loss function of encoder + generator
#self.EG_loss = tf.nn.l2_loss(self.input_image - self.G) / self.size_batch # L2 loss
self.EG_loss = tf.reduce_mean(tf.abs(self.input_image - self.G)) # L1 loss
# loss function of discriminator on z
self.D_z_loss_prior = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_z_prior_logits, labels=tf.ones_like(self.D_z_prior_logits))
)
self.D_z_loss_z = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_z_logits, labels=tf.zeros_like(self.D_z_logits))
)
self.E_z_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_z_logits, labels=tf.ones_like(self.D_z_logits))
)
# loss function of discriminator on image
self.D_img_loss_input = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_input_logits, labels=tf.ones_like(self.D_input_logits))
)
self.D_img_loss_G = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_G_logits, labels=tf.zeros_like(self.D_G_logits))
)
self.G_img_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=self.D_G_logits, labels=tf.ones_like(self.D_G_logits))
)
# total variation to smooth the generated image
tv_y_size = self.size_image
tv_x_size = self.size_image
self.tv_loss = (
(tf.nn.l2_loss(self.G[:, 1:, :, :] - self.G[:, :self.size_image - 1, :, :]) / tv_y_size) +
(tf.nn.l2_loss(self.G[:, :, 1:, :] - self.G[:, :, :self.size_image - 1, :]) / tv_x_size)) / self.size_batch
# *********************************** trainable variables ****************************************************
trainable_variables = tf.trainable_variables()
# variables of encoder
self.E_variables = [var for var in trainable_variables if 'E_' in var.name]
# variables of generator
self.G_variables = [var for var in trainable_variables if 'G_' in var.name]
# variables of discriminator on z
self.D_z_variables = [var for var in trainable_variables if 'D_z_' in var.name]
# variables of discriminator on image
self.D_img_variables = [var for var in trainable_variables if 'D_img_' in var.name]
# ************************************* collect the summary ***************************************
self.z_summary = tf.summary.histogram('z', self.z)
self.z_prior_summary = tf.summary.histogram('z_prior', self.z_prior)
self.EG_loss_summary = tf.summary.scalar('EG_loss', self.EG_loss)
self.D_z_loss_z_summary = tf.summary.scalar('D_z_loss_z', self.D_z_loss_z)
self.D_z_loss_prior_summary = tf.summary.scalar('D_z_loss_prior', self.D_z_loss_prior)
self.E_z_loss_summary = tf.summary.scalar('E_z_loss', self.E_z_loss)
self.D_z_logits_summary = tf.summary.histogram('D_z_logits', self.D_z_logits)
self.D_z_prior_logits_summary = tf.summary.histogram('D_z_prior_logits', self.D_z_prior_logits)
self.D_img_loss_input_summary = tf.summary.scalar('D_img_loss_input', self.D_img_loss_input)
self.D_img_loss_G_summary = tf.summary.scalar('D_img_loss_G', self.D_img_loss_G)
self.G_img_loss_summary = tf.summary.scalar('G_img_loss', self.G_img_loss)
self.D_G_logits_summary = tf.summary.histogram('D_G_logits', self.D_G_logits)
self.D_input_logits_summary = tf.summary.histogram('D_input_logits', self.D_input_logits)
# for saving the graph and variables
self.saver = tf.train.Saver(max_to_keep=2)
def train(self,
num_epochs=200, # number of epochs
learning_rate=0.0002, # learning rate of optimizer
beta1=0.5, # parameter for Adam optimizer
decay_rate=1.0, # learning rate decay (0, 1], 1 means no decay
enable_shuffle=True, # enable shuffle of the dataset
use_trained_model=True, # use the saved checkpoint to initialize the network
use_init_model=True, # use the init model to initialize the network
weigts=(0.0001, 0, 0) # the weights of adversarial loss and TV loss
):
# *************************** load file names of images ******************************************************
file_names = glob(os.path.join('./data', self.dataset_name, '*.jpg'))
size_data = len(file_names)
np.random.seed(seed=2017)
if enable_shuffle:
np.random.shuffle(file_names)
# *********************************** optimizer **************************************************************
# over all, there are three loss functions, weights may differ from the paper because of different datasets
self.loss_EG = self.EG_loss + weigts[0] * self.G_img_loss + weigts[1] * self.E_z_loss + weigts[2] * self.tv_loss # slightly increase the params
self.loss_Dz = self.D_z_loss_prior + self.D_z_loss_z
self.loss_Di = self.D_img_loss_input + self.D_img_loss_G
# set learning rate decay
self.EG_global_step = tf.Variable(0, trainable=False, name='global_step')
EG_learning_rate = tf.train.exponential_decay(
learning_rate=learning_rate,
global_step=self.EG_global_step,
decay_steps=size_data / self.size_batch * 2,
decay_rate=decay_rate,
staircase=True
)
# optimizer for encoder + generator
with tf.variable_scope('opt', reuse=tf.AUTO_REUSE):
self.EG_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_EG,
global_step=self.EG_global_step,
var_list=self.E_variables + self.G_variables
)
# optimizer for discriminator on z
self.D_z_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_Dz,
var_list=self.D_z_variables
)
# optimizer for discriminator on image
self.D_img_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_Di,
var_list=self.D_img_variables
)
# *********************************** tensorboard *************************************************************
# for visualization (TensorBoard): $ tensorboard --logdir path/to/log-directory
self.EG_learning_rate_summary = tf.summary.scalar('EG_learning_rate', EG_learning_rate)
self.summary = tf.summary.merge([
self.z_summary, self.z_prior_summary,
self.D_z_loss_z_summary, self.D_z_loss_prior_summary,
self.D_z_logits_summary, self.D_z_prior_logits_summary,
self.EG_loss_summary, self.E_z_loss_summary,
self.D_img_loss_input_summary, self.D_img_loss_G_summary,
self.G_img_loss_summary, self.EG_learning_rate_summary,
self.D_G_logits_summary, self.D_input_logits_summary
])
self.writer = tf.summary.FileWriter(os.path.join(self.save_dir, 'summary'), self.session.graph)
# ************* get some random samples as testing data to visualize the learning process *********************
sample_files = file_names[0:self.size_batch]
file_names[0:self.size_batch] = []
sample = [load_image(
image_path=sample_file,
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
) for sample_file in sample_files]
if self.num_input_channels == 1:
sample_images = np.array(sample).astype(np.float32)[:, :, :, None]
else:
sample_images = np.array(sample).astype(np.float32)
sample_label_age = np.ones(
shape=(len(sample_files), self.num_categories),
dtype=np.float32
) * self.image_value_range[0]
sample_label_gender = np.ones(
shape=(len(sample_files), 2),
dtype=np.float32
) * self.image_value_range[0]
for i, label in enumerate(sample_files):
label = int(str(sample_files[i]).split('/')[-1].split('_')[0])
if 0 <= label <= 5:
label = 0
elif 6 <= label <= 10:
label = 1
elif 11 <= label <= 15:
label = 2
elif 16 <= label <= 20:
label = 3
elif 21 <= label <= 30:
label = 4
elif 31 <= label <= 40:
label = 5
elif 41 <= label <= 50:
label = 6
elif 51 <= label <= 60:
label = 7
elif 61 <= label <= 70:
label = 8
else:
label = 9
sample_label_age[i, label] = self.image_value_range[-1]
gender = int(str(sample_files[i]).split('/')[-1].split('_')[1])
sample_label_gender[i, gender] = self.image_value_range[-1]
# ******************************************* training *******************************************************
# initialize the graph
tf.global_variables_initializer().run()
# load check point
if use_trained_model:
if self.load_checkpoint():
print("\tSUCCESS ^_^")
else:
print("\tFAILED >_<!")
# load init model
if use_init_model:
if not os.path.exists('init_model/model-init.data-00000-of-00001'):
from init_model.zip_opt import join
try:
join('init_model/model_parts', 'init_model/model-init.data-00000-of-00001')
except:
raise Exception('Error joining files')
self.load_checkpoint(model_path='init_model')
# epoch iteration
num_batches = len(file_names) // self.size_batch
for epoch in range(num_epochs):
if enable_shuffle:
np.random.shuffle(file_names)
for ind_batch in range(num_batches):
start_time = time.time()
# read batch images and labels
batch_files = file_names[ind_batch*self.size_batch:(ind_batch+1)*self.size_batch]
batch = [load_image(
image_path=batch_file,
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
) for batch_file in batch_files]
if self.num_input_channels == 1:
batch_images = np.array(batch).astype(np.float32)[:, :, :, None]
else:
batch_images = np.array(batch).astype(np.float32)
batch_label_age = np.ones(
shape=(len(batch_files), self.num_categories),
dtype=np.float
) * self.image_value_range[0]
batch_label_gender = np.ones(
shape=(len(batch_files), 2),
dtype=np.float
) * self.image_value_range[0]
for i, label in enumerate(batch_files):
label = int(str(batch_files[i]).split('/')[-1].split('_')[0])
if 0 <= label <= 5:
label = 0
elif 6 <= label <= 10:
label = 1
elif 11 <= label <= 15:
label = 2
elif 16 <= label <= 20:
label = 3
elif 21 <= label <= 30:
label = 4
elif 31 <= label <= 40:
label = 5
elif 41 <= label <= 50:
label = 6
elif 51 <= label <= 60:
label = 7
elif 61 <= label <= 70:
label = 8
else:
label = 9
batch_label_age[i, label] = self.image_value_range[-1]
gender = int(str(batch_files[i]).split('/')[-1].split('_')[1])
batch_label_gender[i, gender] = self.image_value_range[-1]
# prior distribution on the prior of z
batch_z_prior = np.random.uniform(
self.image_value_range[0],
self.image_value_range[-1],
[self.size_batch, self.num_z_channels]
).astype(np.float32)
# update
_, _, _, EG_err, Ez_err, Dz_err, Dzp_err, Gi_err, DiG_err, Di_err, TV = self.session.run(
fetches = [
self.EG_optimizer,
self.D_z_optimizer,
self.D_img_optimizer,
self.EG_loss,
self.E_z_loss,
self.D_z_loss_z,
self.D_z_loss_prior,
self.G_img_loss,
self.D_img_loss_G,
self.D_img_loss_input,
self.tv_loss
],
feed_dict={
self.input_image: batch_images,
self.age: batch_label_age,
self.gender: batch_label_gender,
self.z_prior: batch_z_prior
}
)
print("\nEpoch: [%3d/%3d] Batch: [%3d/%3d]\n\tEG_err=%.4f\tTV=%.4f" %
(epoch+1, num_epochs, ind_batch+1, num_batches, EG_err, TV))
print("\tEz=%.4f\tDz=%.4f\tDzp=%.4f" % (Ez_err, Dz_err, Dzp_err))
print("\tGi=%.4f\tDi=%.4f\tDiG=%.4f" % (Gi_err, Di_err, DiG_err))
# estimate left run time
elapse = time.time() - start_time
time_left = ((num_epochs - epoch - 1) * num_batches + (num_batches - ind_batch - 1)) * elapse
print("\tTime left: %02d:%02d:%02d" %
(int(time_left / 3600), int(time_left % 3600 / 60), time_left % 60))
# add to summary
summary = self.summary.eval(
feed_dict={
self.input_image: batch_images,
self.age: batch_label_age,
self.gender: batch_label_gender,
self.z_prior: batch_z_prior
}
)
self.writer.add_summary(summary, self.EG_global_step.eval())
# save sample images for each epoch
name = '{:02d}.png'.format(epoch+1)
self.sample(sample_images, sample_label_age, sample_label_gender, name)
self.test(sample_images, sample_label_gender, name)
# save checkpoint for each 5 epoch
if np.mod(epoch, 5) == 4:
self.save_checkpoint()
# save the trained model
self.save_checkpoint()
# close the summary writer
self.writer.close()
def encoder(self, image, reuse_variables=False):
if reuse_variables:
tf.get_variable_scope().reuse_variables()
num_layers = int(np.log2(self.size_image)) - int(self.size_kernel / 2)
current = image
# conv layers with stride 2
for i in range(num_layers):
name = 'E_conv' + str(i)
current = conv2d(
input_map=current,
num_output_channels=self.num_encoder_channels * (2 ** i),
size_kernel=self.size_kernel,
name=name
)
current = tf.nn.relu(current)
# fully connection layer
name = 'E_fc'
current = fc(
input_vector=tf.reshape(current, [self.size_batch, -1]),
num_output_length=self.num_z_channels,
name=name
)
# output
return tf.nn.tanh(current)
def generator(self, z, y, gender, reuse_variables=False, enable_tile_label=True, tile_ratio=1.0):
if reuse_variables:
tf.get_variable_scope().reuse_variables()
num_layers = int(np.log2(self.size_image)) - int(self.size_kernel / 2)
if enable_tile_label:
duplicate = int(self.num_z_channels * tile_ratio / self.num_categories)
else:
duplicate = 1
z = concat_label(z, y, duplicate=duplicate)
if enable_tile_label:
duplicate = int(self.num_z_channels * tile_ratio / 2)
else:
duplicate = 1
z = concat_label(z, gender, duplicate=duplicate)
size_mini_map = int(self.size_image / 2 ** num_layers)
# fc layer
name = 'G_fc'
current = fc(
input_vector=z,
num_output_length=self.num_gen_channels * size_mini_map * size_mini_map,
name=name
)
# reshape to cube for deconv
current = tf.reshape(current, [-1, size_mini_map, size_mini_map, self.num_gen_channels])
current = tf.nn.relu(current)
# deconv layers with stride 2
for i in range(num_layers):
name = 'G_deconv' + str(i)
current = deconv2d(
input_map=current,
output_shape=[self.size_batch,
size_mini_map * 2 ** (i + 1),
size_mini_map * 2 ** (i + 1),
int(self.num_gen_channels / 2 ** (i + 1))],
size_kernel=self.size_kernel,
name=name
)
current = tf.nn.relu(current)
name = 'G_deconv' + str(i+1)
current = deconv2d(
input_map=current,
output_shape=[self.size_batch,
self.size_image,
self.size_image,
int(self.num_gen_channels / 2 ** (i + 2))],
size_kernel=self.size_kernel,
stride=1,
name=name
)
current = tf.nn.relu(current)
name = 'G_deconv' + str(i + 2)
current = deconv2d(
input_map=current,
output_shape=[self.size_batch,
self.size_image,
self.size_image,
self.num_input_channels],
size_kernel=self.size_kernel,
stride=1,
name=name
)
# output
return tf.nn.tanh(current)
def discriminator_z(self, z, is_training=True, reuse_variables=False, num_hidden_layer_channels=(64, 32, 16), enable_bn=True):
if reuse_variables:
tf.get_variable_scope().reuse_variables()
current = z
# fully connection layer
for i in range(len(num_hidden_layer_channels)):
name = 'D_z_fc' + str(i)
current = fc(
input_vector=current,
num_output_length=num_hidden_layer_channels[i],
name=name
)
if enable_bn:
name = 'D_z_bn' + str(i)
current = tf.contrib.layers.batch_norm(
current,
scale=False,
is_training=is_training,
scope=name,
reuse=reuse_variables
)
current = tf.nn.relu(current)
# output layer
name = 'D_z_fc' + str(i+1)
current = fc(
input_vector=current,
num_output_length=1,
name=name
)
return tf.nn.sigmoid(current), current
def discriminator_img(self, image, y, gender, is_training=True, reuse_variables=False, num_hidden_layer_channels=(16, 32, 64, 128), enable_bn=True):
if reuse_variables:
tf.get_variable_scope().reuse_variables()
num_layers = len(num_hidden_layer_channels)
current = image
# conv layers with stride 2
for i in range(num_layers):
name = 'D_img_conv' + str(i)
current = conv2d(
input_map=current,
num_output_channels=num_hidden_layer_channels[i],
size_kernel=self.size_kernel,
name=name
)
if enable_bn:
name = 'D_img_bn' + str(i)
current = tf.contrib.layers.batch_norm(
current,
scale=False,
is_training=is_training,
scope=name,
reuse=reuse_variables
)
current = tf.nn.relu(current)
if i == 0:
current = concat_label(current, y)
current = concat_label(current, gender, int(self.num_categories / 2))
# fully connection layer
name = 'D_img_fc1'
current = fc(
input_vector=tf.reshape(current, [self.size_batch, -1]),
num_output_length=1024,
name=name
)
current = lrelu(current)
name = 'D_img_fc2'
current = fc(
input_vector=current,
num_output_length=1,
name=name
)
# output
return tf.nn.sigmoid(current), current
def save_checkpoint(self):
checkpoint_dir = os.path.join(self.save_dir, 'checkpoint')
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
self.saver.save(
sess=self.session,
save_path=os.path.join(checkpoint_dir, 'model'),
global_step=self.EG_global_step.eval()
)
def load_checkpoint(self, model_path=None):
if model_path is None:
print("\n\tLoading pre-trained model ...")
checkpoint_dir = os.path.join(self.save_dir, 'checkpoint')
else:
print("\n\tLoading init model ...")
checkpoint_dir = model_path
checkpoints = tf.train.get_checkpoint_state(checkpoint_dir)
if checkpoints and checkpoints.model_checkpoint_path:
checkpoints_name = os.path.basename(checkpoints.model_checkpoint_path)
try:
self.saver.restore(self.session, os.path.join(checkpoint_dir, checkpoints_name))
return True
except:
return False
else:
return False
def sample(self, images, labels, gender, name):
sample_dir = os.path.join(self.save_dir, 'samples')
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
z, G = self.session.run(
[self.z, self.G],
feed_dict={
self.input_image: images,
self.age: labels,
self.gender: gender
}
)
size_frame = int(np.sqrt(self.size_batch))
save_batch_images(
batch_images=G,
save_path=os.path.join(sample_dir, name),
image_value_range=self.image_value_range,
size_frame=[size_frame, size_frame]
)
def test(self, images, gender, name):
test_dir = os.path.join(self.save_dir, 'test')
if not os.path.exists(test_dir):
os.makedirs(test_dir)
images = images[:int(np.sqrt(self.size_batch)), :, :, :]
gender = gender[:int(np.sqrt(self.size_batch)), :]
size_sample = images.shape[0]
labels = np.arange(size_sample)
labels = np.repeat(labels, size_sample)
query_labels = np.ones(
shape=(size_sample ** 2, size_sample),
dtype=np.float32
) * self.image_value_range[0]
for i in range(query_labels.shape[0]):
query_labels[i, labels[i]] = self.image_value_range[-1]
query_images = np.tile(images, [self.num_categories, 1, 1, 1])
query_gender = np.tile(gender, [self.num_categories, 1])
z, G = self.session.run(
[self.z, self.G],
feed_dict={
self.input_image: query_images,
self.age: query_labels,
self.gender: query_gender
}
)
save_batch_images(
batch_images=query_images,
save_path=os.path.join(test_dir, 'input.png'),
image_value_range=self.image_value_range,
size_frame=[size_sample, size_sample]
)
save_batch_images(
batch_images=G,
save_path=os.path.join(test_dir, name),
image_value_range=self.image_value_range,
size_frame=[size_sample, size_sample]
)
def custom_test(self, testing_samples_dir):
if not self.load_checkpoint():
print("\tFAILED >_<!")
exit(0)
else:
print("\tSUCCESS ^_^")
num_samples = int(np.sqrt(self.size_batch))
file_names = glob(testing_samples_dir)
if len(file_names) < num_samples:
print 'The number of testing images is must larger than %d' % num_samples
exit(0)
sample_files = file_names[0:num_samples]
sample = [load_image(
image_path=sample_file,
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
) for sample_file in sample_files]
if self.num_input_channels == 1:
images = np.array(sample).astype(np.float32)[:, :, :, None]
else:
images = np.array(sample).astype(np.float32)
gender_male = np.ones(
shape=(num_samples, 2),
dtype=np.float32
) * self.image_value_range[0]
gender_female = np.ones(
shape=(num_samples, 2),
dtype=np.float32
) * self.image_value_range[0]
for i in range(gender_male.shape[0]):
gender_male[i, 0] = self.image_value_range[-1]
gender_female[i, 1] = self.image_value_range[-1]
self.test(images, gender_male, 'test_as_male.png')
self.test(images, gender_female, 'test_as_female.png')
print '\n\tDone! Results are saved as %s\n' % os.path.join(self.save_dir, 'test', 'test_as_xxx.png')