forked from FFZhang1231/Facial-expression-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacial_expression_train.py
2069 lines (1873 loc) · 80.5 KB
/
Facial_expression_train.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This code is implemented by tensorflow r0.12
# Date: Nov. 20th, 2017
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 *
import pdb
import pandas as pd
class PFER_expression(object):
def __init__(self,
session, # TensorFlow session
size_image=224, # size the input images
size_kernel=5, # size of the kernels in convolution and deconvolution
size_batch=36, # 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_fx=50, # number of channels of the layer f(x)
num_categories=6, # number of expressions in the training dataset
num_poses =5, # number of poses 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 fx
is_training=True, # flag for training or testing mode
save_dir='./savePFEW', # path to save checkpoints, samples, and summary
):
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_fx = num_fx
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.num_poses = num_poses
# path of the file of trainset. trainMULTIPIE.txt: name/expression label/pose label
self.pathtrain = '/path/to/your/data'
self.file_names = np.loadtxt(self.pathtrain + 'trainMULTIPIE.txt', dtype=bytes, delimiter=' ').astype(str)
np.random.shuffle(self.file_names)
self.len_trainset = len(self.file_names)
self.num_batches = self.len_trainset // self.size_batch
self.gen_names = np.loadtxt(self.pathtrain + 'genMULTIPIE.txt', dtype=bytes, delimiter=' ').astype(str)
np.random.shuffle(self.gen_names)
self.gen_trainset = len(self.gen_names)
self.num_batches1 = self.gen_trainset // self.size_batch
self.test_names = np.loadtxt(self.pathtrain + 'testMULTIPIE.txt', dtype=bytes, delimiter=' ').astype(str)
np.random.shuffle(self.test_names)
gen = open('testname.txt', 'w') # name of the testset
self.len_testset = len(self.test_names)
self.num_batches2 = self.len_testset // self.size_batch
for ii in range(self.test_names.shape[0]):
gen.write(self.test_names[ii, 0] + '\n')
gen.close()
self.len_testset = len(self.test_names)
self.num_batches2 = self.len_testset // self.size_batch
#train on BU-3DFE
# ************ similar to above ************
#train on SFEW
# ************ similar to above ************
# ************************************* input to graph ********************************************************
self.input_image = tf.placeholder( #input_image size [36, 224, 224, 3]
tf.float32,
[self.size_batch, self.size_image, self.size_image, self.num_input_channels],
name='input_images'
)
self.expression = tf.placeholder( #expression label for G, D_att, and C_exp. onehot
tf.float32,
[self.size_batch, self.num_categories],
name='expression_labels'
)
# if sparse_softmax_cross_entropy_with_logits is used
# self.expression1 = tf.placeholder( #expression label for C_exp
# tf.int64,
# [self.size_batch],
# name='expression_labels'
# )
self.pose = tf.placeholder( #pose label for G and D_att, and C_exp. onehot
tf.float32,
[self.size_batch, self.num_poses],
name='pose_labels'
)
# if sparse_softmax_cross_entropy_with_logits is used
# self.pose1 = tf.placeholder( #pose label for C_exp
# tf.int64,
# [self.size_batch],
# name='pose_labels'
# )
self.f_prior = tf.placeholder( #prior distribution of D_i
tf.float32,
[self.size_batch, self.num_fx],
name='f_prior'
)
# ************************************* build the graph *******************************************************
print '\n\tBuilding graph ...'
# G_encoder: input image --> f(x)
self.f = self.Gencoder(
image=self.input_image
)
# G_decoder: f(x) + expression + pose --> generated image
self.G = self.Gdecoder(
f=self.f,
y=self.expression,
pose=self.pose,
enable_tile_label=self.enable_tile_label,
tile_ratio=self.tile_ratio
)
# discriminator on identity
self.D_f, self.D_f_logits = self.discriminator_i(
f=self.f,
is_training=self.is_training
)
# discriminator on G
self.D_G, self.D_G_logits= self.discriminator_att(
image=self.G,
y=self.expression,
pose=self.pose,
is_training=self.is_training
)
# discriminator on f_prior
self.D_f_prior, self.D_f_prior_logits = self.discriminator_i(
f=self.f_prior,
is_training=self.is_training,
reuse_variables=True
)
# discriminator on input image
self.D_input, self.D_input_logits = self.discriminator_att(
image=self.input_image,
y=self.expression,
pose=self.pose,
is_training=self.is_training,
reuse_variables=True
)
# classifier on original facial images and generated facial images
self.D_input_ex_logits, self.D_input_pose_logits = self.discriminator_acc(
image=self.input_image,
is_training=self.is_training
)
# ************************************* loss functions *******************************************************
# loss function of generator G
self.EG_loss = tf.reduce_mean(tf.abs(self.input_image - self.G)) # L1 loss
# loss function of discriminator on identity
self.D_f_loss_prior = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_f_prior_logits, tf.ones_like(self.D_f_prior_logits))
)
self.D_f_loss_f = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_f_logits, tf.zeros_like(self.D_f_logits))
)
# loss function of G on identity
self.E_f_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_f_logits, tf.ones_like(self.D_f_logits))
)
# loss function of discriminator on image
self.D_att_loss_input = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_input_logits, tf.ones_like(self.D_input_logits))
)
self.D_att_loss_G = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_G_logits, tf.zeros_like(self.D_G_logits))
)
# loss function of G on image
self.G_att_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(self.D_G_logits, tf.ones_like(self.D_G_logits))
)
# (1) loss function of classifier on image
self.D_ex_loss_input = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.expression, logits=self.D_input_ex_logits) )
self.D_pose_loss_input = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.pose, logits=self.D_input_pose_logits) )
#(2) if sparse_softmax_cross_entropy_with_logits is used
# self.D_ex_loss_input = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.expression1, logits=self.D_input_ex_logits) )
# self.D_pose_loss_input = tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.pose1, logits=self.D_input_pose_logits) )
# pdb.set_trace()
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
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ACCURACY OPS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#Accuracy of expression
self.d_ex_count = tf.reduce_sum(tf.cast(tf.equal(tf.argmax(self.D_input_ex_logits, 1), tf.argmax(self.expression,1)), 'int32'))
#Accuracy of pose
self.d_pose_count = tf.reduce_sum(tf.cast(tf.equal(tf.argmax(self.D_input_pose_logits, 1), tf.argmax(self.pose, 1)), 'int32'))
#(2) if sparse_softmax_cross_entropy_with_logits is used
# self.d_ex_count = tf.reduce_sum(
# tf.cast(tf.equal(tf.argmax(self.D_input_ex_logits, 1), self.expression1), 'int32'))
# self.d_pose_count = tf.reduce_sum(
# tf.cast(tf.equal(tf.argmax(self.D_input_pose_logits, 1), self.pose1), 'int32'))
# *********************************** trainable variables ****************************************************
trainable_variables = tf.trainable_variables()
# print (trainable_variables)
# variables of G_encoder
self.E_variables = [var for var in trainable_variables if 'E_' in var.name]
# variables of G_decoder
self.G_variables = [var for var in trainable_variables if 'G_' in var.name]
# variables of discriminator on identity
self.D_f_variables = [var for var in trainable_variables if 'D_f_' in var.name]
# variables of discriminator on attributes
self.D_att_variables = [var for var in trainable_variables if 'D_att_' in var.name]
# variables of discriminator on expression
self.D_acc_variables = [var for var in trainable_variables if 'D_acc_' in var.name]
# ************************************* collect the summary ***************************************
self.f_summary = tf.summary.histogram('f', self.f)
self.f_prior_summary = tf.summary.histogram('f_prior', self.f_prior)
self.EG_loss_summary = tf.summary.scalar('EG_loss', self.EG_loss)
self.D_f_loss_f_summary = tf.summary.scalar('D_f_loss_f', self.D_f_loss_f)
self.D_f_loss_prior_summary = tf.summary.scalar('D_f_loss_prior', self.D_f_loss_prior)
self.E_f_loss_summary = tf.summary.scalar('E_f_loss', self.E_f_loss)
self.D_f_logits_summary = tf.summary.histogram('D_f_logits', self.D_f_logits)
self.D_f_prior_logits_summary = tf.summary.histogram('D_f_prior_logits', self.D_f_prior_logits)
self.D_att_loss_input_summary = tf.summary.scalar('D_att_loss_input', self.D_att_loss_input)
self.D_att_loss_G_summary = tf.summary.scalar('D_att_loss_G', self.D_att_loss_G)
self.G_att_loss_summary = tf.summary.scalar('G_att_loss', self.G_att_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)
self.D_input_ex_logits_summary = tf.summary.histogram('D_input_ex_logits', self.D_input_ex_logits)
self.D_ex_loss_input_summary = tf.summary.scalar('D_ex_loss_input_summary',self.D_ex_loss_input)
self.d_ex_count_summary = tf.summary.scalar('d_ex_count', self.d_ex_count)
self.d_pose_count_summary = tf.summary.scalar('d_pose_count', self.d_pose_count)
# for saving the graph and variables
self.saver = tf.train.Saver(max_to_keep=10)
#get the train data and test data
def get_batch_train_test(self, enable_shuffle=True, idx=0):
# # *************************** load file names of images ******************************************************
if self.is_training:
if enable_shuffle:
np.random.shuffle(self.file_names)
tt_files = self.file_names[idx*self.size_batch: idx*self.size_batch + self.size_batch]
#path of the traindata
self.path = self.pathtrain + 'data/MultiPie_train/'
else:
tt_files = self.test_names[idx*self.size_batch: idx*self.size_batch + self.size_batch]
#path of the testdata
self.path = self.pathtrain + 'data/MultiPie_test/'
batch_images = np.zeros((self.size_batch, self.size_image, self.size_image, 3))
for i in range(tt_files.shape[0]):
sample = [load_image(
image_path= self.path + tt_files[i, 0],
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
)]
if self.num_input_channels == 1:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)[:, :, :, None]
else:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)
tt_label_expression = np.ones(
shape=(len(tt_files), self.num_categories),
dtype=np.float32
) * self.image_value_range[0]
tt_label_pose = np.ones(
shape=(len(tt_files), self.num_poses),
dtype=np.float32
) * self.image_value_range[0]
# if sparse_softmax_cross_entropy_with_logits is used
# tt_label_expression1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
#
# tt_label_pose1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
for i, label in enumerate(tt_files[:, 1]):
label = tt_files[i, 1].astype('int')
#changing the expression label as onehot with the target as 1, others as -1;
tt_label_expression[i, label] = self.image_value_range[-1]
# if sparse_softmax_cross_entropy_with_logits is used
# expression label
# tt_label_expression1[i]=label
pose = tt_files[i, 2].astype('int')
if pose == 41:
pose = 0
elif pose == 130:
pose = 1
elif pose == 50:
pose = 2
elif pose == 51:
pose = 3
elif pose == 140:
pose = 4
# changing the pose label as onehot with the target as 1, others as -1;
tt_label_pose[i, pose] = self.image_value_range[-1]
# if sparse_softmax_cross_entropy_with_logits is used
# pose label
# tt_label_pose1[i]=pose
# return batch_images, tt_label_expression, tt_label_pose, tt_label_expression1, tt_label_pose1, tt_files
return batch_images, tt_label_expression, tt_label_pose, tt_files
# get the gen data
def get_batch_gen(self, DIS=True, idx=0):
if DIS:
print('dis')
np.random.shuffle(self.gen_names)
tt_files = self.gen_names[idx*self.size_batch: idx*self.size_batch + self.size_batch]
#path of the traindata
self.path = self.pathtrain + 'data/MultiPie_train/'
batch_images = np.zeros((self.size_batch, self.size_image, self.size_image, 3))
for i in range(tt_files.shape[0]):
sample = [load_image(
image_path= self.path + tt_files[i, 0],
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
)]
if self.num_input_channels == 1:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)[:, :, :, None]
else:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)
tt_label_expression = np.ones(
shape=(len(tt_files), self.num_categories),
dtype=np.float32
) * self.image_value_range[0]
tt_label_pose = np.ones(
shape=(len(tt_files), self.num_poses),
dtype=np.float32
) * self.image_value_range[0]
# tt_label_expression1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
#
# tt_label_pose1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
for i, label in enumerate(tt_files[:, 1]):
label = tt_files[i, 1].astype('int')
tt_label_expression[i, label] = self.image_value_range[-1]
# tt_label_expression1[i]=label
pose = tt_files[i, 2].astype('int')
if pose == 41:
pose = 0
elif pose == 130:
pose = 1
elif pose == 50:
pose = 2
elif pose == 51:
pose = 3
elif pose == 140:
pose = 4
tt_label_pose[i, pose] = self.image_value_range[-1]
# tt_label_pose1[i]=pose
# return batch_images, tt_label_expression, tt_label_pose, tt_label_expression1, tt_label_pose1, tt_files
return batch_images, tt_label_expression, tt_label_pose, tt_files
# get the validation data to validate the generated images
def get_batch_sample(self, idx=0):
tt_files = self.test_names[idx*self.size_batch: idx*self.size_batch + self.size_batch]
batch_images = np.zeros((self.size_batch, self.size_image, self.size_image, 3))
for i in range(tt_files.shape[0]):
sample = [load_image(
#path of the testdata
image_path=self.pathtrain + 'data/MultiPie_test/' + tt_files[i,0],
image_size=self.size_image,
image_value_range=self.image_value_range,
is_gray=(self.num_input_channels == 1),
)]
if self.num_input_channels == 1:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)[:, :, :, None]
else:
batch_images[i, :, :, :] = np.array(sample).astype(np.float32)
tt_label_expression = np.ones(
shape=(len(tt_files), self.num_categories),
dtype=np.float32
) * self.image_value_range[0]
tt_label_pose = np.ones(
shape=(len(tt_files), self.num_poses),
dtype=np.float32
) * self.image_value_range[0]
# tt_label_expression1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
#
# tt_label_pose1 = np.ones(
# shape=(len(tt_files)),
# dtype=np.float32
# ) * self.image_value_range[0]
for i, label in enumerate(tt_files[:, 1]):
label = tt_files[i, 1].astype('int')
tt_label_expression[i, label] = self.image_value_range[-1]
# tt_label_expression1[i]=label
pose = tt_files[i, 2].astype('int')
if pose == 41:
pose = 0
elif pose == 130:
pose = 1
elif pose == 50:
pose = 2
elif pose == 51:
pose = 3
elif pose == 140:
pose = 4
tt_label_pose[i, pose] = self.image_value_range[-1]
# tt_label_pose1[i]=pose
# return batch_images, tt_label_expression, tt_label_pose, tt_label_expression1, tt_label_pose1, tt_files
return batch_images, tt_label_expression, tt_label_pose, tt_files
def train(self,
num_epochs=100,# number of epochs
learning_rate=0.0002, # learning rate of optimizer
#learning_rate=0.00005, # learning rate of optimizer
beta1=0.5, # parameter for Adam optimizer
decay_rate=0.99, # learning rate decay (0, 1], 1 means no decay
#decay_rate=1, # learning rate decay (0, 1], 1 means no decay
enable_shuffle=True, # enable shuffle of the dataset
use_trained_model=True,# used the saved checkpoint to initialize the model
):
# *********************************** optimizer **************************************************************
# over all, there are four loss functions, weights may differ from the paper because of different datasets
# self.loss_EG = self.EG_loss + 0.0005 * self.G_att_loss + 0.0005 * self.E_f_loss + 0.0001 * self.tv_loss+ 0.0001*self.loss_Ex # slightly increase the params
self.loss_EG = self.EG_loss + 0.0001 * self.G_att_loss + 0.0001 * self.E_f_loss + 0.0001 * self.tv_loss # slightly increase the params
self.loss_Df = self.D_f_loss_prior + self.D_f_loss_f
self.loss_Datt = self.D_att_loss_input + self.D_att_loss_G
self.loss_Ex = self.D_ex_loss_input+self.D_pose_loss_input
# 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=self.len_trainset / self.size_batch * 2,
decay_rate=decay_rate,
staircase=True
)
# optimizer for G_encoder + G_decoder
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 f(x)
self.D_f_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_Df,
var_list=self.D_f_variables
)
# optimizer for discriminator on attributes
self.D_att_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_Datt,
var_list=self.D_att_variables
)
# optimizer for discriminator on expression
self.D_ex_optimizer = tf.train.AdamOptimizer(
learning_rate=EG_learning_rate,
beta1=beta1
).minimize(
loss=self.loss_Ex,
var_list=self.D_acc_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.f_summary, self.f_prior_summary,
self.D_f_loss_f_summary, self.D_f_loss_prior_summary,
self.D_f_logits_summary, self.D_f_prior_logits_summary,
self.EG_loss_summary, self.E_f_loss_summary,
self.D_att_loss_input_summary, self.D_att_loss_G_summary,
self.G_att_loss_summary, self.EG_learning_rate_summary,
self.D_G_logits_summary, self.D_input_logits_summary,
self.D_input_ex_logits_summary,
self.D_ex_loss_input_summary,
self.d_ex_count_summary,self.d_pose_count_summary
])
self.writer = tf.summary.FileWriter(os.path.join(self.save_dir, 'summary'), self.session.graph)
# ******************************************* training *******************************************************
print '\n\tPreparing for 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 >_<!")
#if sparse_softmax_cross_entropy_with_logits is used
# sample_images, sample_label_expression, sample_label_pose, sample_label_expression1, sample_label_pose1, batch_files_name = self.get_batch_sample(0)
sample_images, sample_label_expression, sample_label_pose, batch_files_name = self.get_batch_sample(0)
sample_expression_label = map(lambda x: [[i, 0][i < 0] for i in x], sample_label_expression)
sample_pose_label = map(lambda x: [[i, 0][i < 0] for i in x], sample_label_pose)
for epoch in range(num_epochs):
# to save the test results
trainresult = open('result/'+ str(epoch) + 'a.txt', 'w')
f1 = open('result/'+str(epoch) + 'test.txt', 'w')
f2 = open('result/'+str(epoch) + 'index.txt', 'w')
self.is_training= True
DIS=True
enable_shuffle =True
for ind_batch in range(self.num_batches):
self.is_training=True
#if sparse_softmax_cross_entropy_with_logits is used
# batch_images, batch_label_expression, batch_label_pose, batch_label_expression1, batch_label_pose1, batch_files_name = self.get_batch_train_test(enable_shuffle,ind_batch)
batch_images, batch_label_expression, batch_label_pose, batch_files_name = self.get_batch_train_test(enable_shuffle,ind_batch)
#map batch_label_expression and batch_label_pose to onehot with the target as 1, others as 0
expression_label =map(lambda x:[[i,0][i<0] for i in x], batch_label_expression)
pose_label = map(lambda x:[[i,0][i<0] for i in x], batch_label_pose)
enable_shuffle = False
start_time = time.time()
# prior distribution on the prior of f [-1,1]
batch_f_prior = np.random.uniform(
self.image_value_range[0],
self.image_value_range[-1],
[self.size_batch, self.num_fx]
).astype(np.float32)
_, _, _ = self.session.run(
fetches=[
self.EG_optimizer,
self.D_f_optimizer,
self.D_att_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
#(1) if softmax_cross_entropy_with_logits is used
_ = self.session.run(
fetches=[
self.D_ex_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: expression_label,
self.pose: pose_label
}
)
#(2) if sparse_softmax_cross_entropy_with_logits is used
# _ = self.session.run(
# fetches=[
# self.D_ex_optimizer
# ],
# feed_dict={
# self.input_image: batch_images,
# self.expression1: batch_label_expression1,
# self.pose1: batch_label_pose1
# }
# )
_ = self.session.run(
fetches=[
self.EG_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
_ = self.session.run(
fetches=[
self.EG_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
_ = self.session.run(
fetches=[
self.EG_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
_ = self.session.run(
fetches=[
self.EG_optimizer
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
#(1) if softmax_cross_entropy_with_logits is used
Dex_err, Dpose_err, D_ex, D_pose = self.session.run(
fetches=[
self.D_ex_loss_input,
self.D_pose_loss_input,
self.d_ex_count,
self.d_pose_count
],
feed_dict={
self.input_image: batch_images,
self.expression: expression_label,
self.pose: pose_label,
self.f_prior: batch_f_prior
}
)
#(2) if sparse_softmax_cross_entropy_with_logits is used
# Dex_err, Dpose_err, D_ex, D_pose = self.session.run(
# fetches=[
# self.D_ex_loss_input,
# # self.D_pose_loss_G,
# self.D_pose_loss_input,
# self.d_ex_count,
# # self.g_ex_count,
# self.d_pose_count,
# # self.g_pose_count,
# ],
# feed_dict={
# self.input_image: batch_images,
# self.expression1: batch_label_expression1,
# self.pose1: batch_label_pose1,
# self.f_prior: batch_f_prior
# }
# )
EG_err, Ef_err, Df_err, Dfp_err, Gi_err, DiG_err, Di_err, TV = self.session.run(
fetches = [
self.EG_loss,
self.E_f_loss,
self.D_f_loss_f,
self.D_f_loss_prior,
self.G_att_loss,
self.D_att_loss_G,
self.D_att_loss_input,
self.tv_loss
],
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
print("\nEpoch: [%3d/%3d] Batch: [%3d/%3d]\n\tEG_err=%.4f\tTV=%.4f" %
(epoch+1, num_epochs, ind_batch+1, self.num_batches, EG_err, TV))
print ("\t Accuracy Dex=%.4f /36 \t Dpose =%.4f /36 " % (D_ex, D_pose))
#using the generated images to train the classifier
if epoch > 6:
#You can change parameter $add$ to add different times of generated images
if epoch > 6 and epoch < 10:
add = *
elif epoch > 10 and epoch < 20:
add = *
elif epoch > 20 and epoch < 30:
add = *
elif epoch > 30:
add = *
for addimg in range(add):
# if sparse_softmax_cross_entropy_with_logits is used
# gen_images, gen_label_expression, gen_label_pose, gen_label_expression1, gen_label_pose1, gen_files_name = self.get_batch_gen(DIS, ind_batch*add+addimg)
gen_images, gen_label_expression, gen_label_pose, gen_files_name = self.get_batch_gen(DIS, ind_batch * add + addimg)
DIS = False
gen_label_expressiononehot = map(lambda x: [[i, 0][i < 0] for i in x], gen_label_expression)
gen_label_poseonehot = map(lambda x: [[i, 0][i < 0] for i in x], gen_label_pose)
f, G = self.session.run(
[self.f, self.G],
feed_dict={
self.input_image: gen_images,
self.expression: gen_label_expression,
self.pose: gen_label_pose
}
)
#(1)
_ = self.session.run(
fetches=[
self.D_ex_optimizer
],
feed_dict={
self.input_image: G,
self.expression: gen_label_expressiononehot,
self.pose: gen_label_poseonehot
}
)
#(2)
# _ = self.session.run(
# fetches=[
# self.D_ex_optimizer
# ],
# feed_dict={
# self.input_image: G,
# self.expression1: gen_label_expression1,
# self.pose1: gen_label_pose1
# }
# )
# (1)
Dex_err, Dpose_err, D_ex, D_pose = self.session.run(
fetches=[
self.D_ex_loss_input,
self.D_pose_loss_input,
self.d_ex_count,
self.d_pose_count
],
feed_dict={
self.input_image: batch_images,
self.expression: expression_label,
self.pose: pose_label,
self.f_prior: batch_f_prior
}
)
#(2)
# Dex_err, Dpose_err, D_ex, D_pose = self.session.run(
# fetches=[
# self.D_ex_loss_input,
# # self.D_pose_loss_G,
# self.D_pose_loss_input,
# self.d_ex_count,
# # self.g_ex_count,
# self.d_pose_count,
# # self.g_pose_count,
# ],
# feed_dict={
# self.input_image: batch_images,
# self.expression1: gen_label_expression1,
# self.pose1: gen_label_pose1,
# self.f_prior: batch_f_prior
# }
# )
print("\tEf=%.4f\tDf=%.4f\tDfp=%.4f" % (Ef_err, Df_err, Dfp_err))
print("\tGi=%.4f\tDi=%.4f\tDiG=%.4f" % (Gi_err, Di_err, DiG_err))
print("\tDex=%.4f\tDpose=%.4f" % (Dex_err, Dpose_err))
print ("\t Accuracy DGex=%.4f /36 \t DGpose =%.4f /36 " % (D_ex, D_pose))
result = 'epoch=' + str(epoch) + '\t' + 'num_batches=' + str(ind_batch) + '\t' + str(D_ex) + '\t' + '\n'
trainresult.writelines(result)
# estimate left run time
elapse = time.time() - start_time
time_left = ((num_epochs - epoch - 1) * self.num_batches + (self.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
# pdb.set_trace()
# (1)
summary = self.summary.eval(
feed_dict={
self.input_image: batch_images,
self.expression: batch_label_expression,
self.pose: batch_label_pose,
self.f_prior: batch_f_prior
}
)
# (2)
# summary = self.summary.eval(
# feed_dict={
# self.input_image: batch_images,
# self.expression: batch_label_expression,
# self.pose: batch_label_pose,
# self.expression1: batch_label_expression1,
# self.pose1: batch_label_pose1,
# self.f_prior: batch_f_prior
# }
# )
self.writer.add_summary(summary, self.EG_global_step.eval())
trainresult.close()
# save sample images for each epoch
name = '{:02d}.png'.format(epoch+1)
self.sample(sample_images, sample_label_expression, sample_label_pose, name)
self.test(sample_images, sample_label_pose, name, sample_label_expression)
#self.test_acc(sample_images, sample_expression_label, sample_pose_label)
#print (self.is_training)
for ind_batch in range(self.num_batches2):
self.is_training= False
# batch_images, batch_label_expression, batch_label_pose, batch_label_expression1, batch_label_pose1, batch_files_name = self.get_batch_train_test(enable_shuffle, ind_batch)
batch_images, batch_label_expression, batch_label_pose, batch_files_name = self.get_batch_train_test(enable_shuffle, ind_batch)
batch_label_expressiononehot = map(lambda x: [[i, 0][i < 0] for i in x], batch_label_expression)
batch_label_poseonehot = map(lambda x: [[i, 0][i < 0] for i in x], batch_label_pose)
accex, accpose,accindex= self.test_acc(batch_images, batch_label_expressiononehot, batch_label_poseonehot)
re = str(accex) +'\t' + str(accpose) +'\n'
#Record the classified labels of each test image
for jj in range(accindex.shape[0]):
resu = accindex[jj]
f2.writelines(str(resu)+'\n')
# Record the number of right test image in each group (each batch_size)
f1.writelines(re)
f1.close()
f2.close()
# save checkpoint for each 10 epoch
if np.mod(epoch, 10) == 9:
self.save_checkpoint()
# save the trained model
self.save_checkpoint()
# close the summary writer
self.writer.close()
def Gencoder(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_fx,
name=name
)
# output
return tf.nn.tanh(current)
def Gdecoder(self, f, y, pose, 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_fx * tile_ratio / self.num_categories)
else:
duplicate = 1
f = concat_label(f, y, duplicate=duplicate)
if enable_tile_label:
duplicate = int(self.num_fx * tile_ratio / self.num_poses)
else:
duplicate = 1
f = concat_label(f, pose, duplicate=duplicate)
size_mini_map = int(self.size_image / 2 ** num_layers)
# fc layer
name = 'G_fc'
current = fc(
input_vector=f,
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
)