@@ -14,7 +14,7 @@ def __init__(self,load_model,tf_record_file_path=None,model_save_path=None,best_
14
14
self .img_width = 227
15
15
self .channel = 3
16
16
17
- self .num_epochs = 2
17
+ self .num_epochs = 10
18
18
19
19
# Hyperparameters 1,5,0.5,5,2
20
20
self .weight_detect = 1
@@ -43,33 +43,34 @@ def build_network(self, sess):
43
43
44
44
self .X = tf .placeholder (tf .float32 , [self .batch_size , self .img_height , self .img_width , self .channel ], name = 'images' )
45
45
self .detection = tf .placeholder (tf .int32 , [self .batch_size ], name = 'detection' )
46
- # self.landmarks = tf.placeholder(tf.float32, [self.batch_size, 42], name='landmarks')
47
- # self.visibility = tf.placeholder(tf.float32, [self.batch_size,21], name='visibility')
48
- # self.pose = tf.placeholder(tf.float32, [self.batch_size,3], name='pose')
49
- # self.gender = tf.placeholder(tf.int32, [self.batch_size], name='gender')
46
+ self .landmarks = tf .placeholder (tf .float32 , [self .batch_size , 42 ], name = 'landmarks' )
47
+ self .visibility = tf .placeholder (tf .float32 , [self .batch_size ,21 ], name = 'visibility' )
48
+ self .pose = tf .placeholder (tf .float32 , [self .batch_size ,3 ], name = 'pose' )
49
+ self .gender = tf .placeholder (tf .int32 , [self .batch_size ], name = 'gender' )
50
50
51
- net_output = self .network_det (self .X ) # (out_detection, out_landmarks, out_visibility, out_pose, out_gender)
51
+ net_output = self .network (self .X ) # (out_detection, out_landmarks, out_visibility, out_pose, out_gender)
52
+ self .test_model = net_output
53
+ self .loss_detection = tf .reduce_mean (tf .nn .sigmoid_cross_entropy_with_logits (logits = net_output [0 ], labels = tf .one_hot (self .detection , 2 )))
52
54
53
- self .loss_detection = tf .reduce_mean (tf .nn .sigmoid_cross_entropy_with_logits (logits = net_output , labels = tf .one_hot (self .detection , 2 )))
54
-
55
- # detection_mask = tf.cast(tf.expand_dims(self.detection, axis=1),tf.float32)
55
+ detection_mask = tf .cast (tf .expand_dims (self .detection , axis = 1 ),tf .float32 )
56
56
57
- # visibility_mask = tf.reshape(tf.tile(tf.expand_dims(self.visibility, axis=2), [1,1,2]), [self.batch_size, -1])
58
- # self.loss_landmarks = tf.reduce_mean(tf.square(detection_mask*visibility_mask*(net_output[1] - self.landmarks)))
57
+ visibility_mask = tf .reshape (tf .tile (tf .expand_dims (self .visibility , axis = 2 ), [1 ,1 ,2 ]), [self .batch_size , - 1 ])
58
+ self .loss_landmarks = tf .reduce_mean (tf .square (detection_mask * visibility_mask * (net_output [1 ] - self .landmarks )))
59
59
60
- # self.loss_visibility = tf.reduce_mean(tf.square(detection_mask*(net_output[2] - self.visibility)))
61
- # self.loss_pose = tf.reduce_mean(tf.square(detection_mask*(net_output[3] - self.pose)))
62
- # self.loss_gender = tf.reduce_mean(detection_mask*tf.nn.sigmoid_cross_entropy_with_logits(logits=net_output[4], labels=tf.one_hot(self.gender,2)))
60
+ self .loss_visibility = tf .reduce_mean (tf .square (detection_mask * (net_output [2 ] - self .visibility )))
61
+ self .loss_pose = tf .reduce_mean (tf .square (detection_mask * (net_output [3 ] - self .pose )))
62
+ self .loss_gender = tf .reduce_mean (detection_mask * tf .nn .sigmoid_cross_entropy_with_logits (logits = net_output [4 ], labels = tf .one_hot (self .gender ,2 )))
63
63
64
64
65
- # self.loss = self.weight_detect*self.loss_detection + self.weight_landmarks*self.loss_landmarks \
66
- # + self.weight_visibility*self.loss_visibility + self.weight_pose*self.loss_pose \
67
- # + self.weight_gender*self.loss_gender
65
+ self .loss = self .weight_detect * self .loss_detection + self .weight_landmarks * self .loss_landmarks \
66
+ + self .weight_visibility * self .loss_visibility + self .weight_pose * self .loss_pose \
67
+ + self .weight_gender * self .loss_gender
68
68
69
- self .accuracy = tf .reduce_mean (tf .cast (tf .equal (tf .cast (tf .argmax (net_output ,1 ),tf .int32 ),self .detection ),tf .float32 ))
69
+ self .accuracy = tf .reduce_mean (tf .cast (tf .equal (tf .cast (tf .argmax (net_output [ 0 ] ,1 ),tf .int32 ),self .detection ),tf .float32 ))
70
70
71
- self .loss = self .loss_detection
72
- self .optimizer = tf .train .AdamOptimizer ().minimize (self .loss )
71
+ #self.loss = self.loss_detection
72
+ #self.optimizer = tf.train.AdamOptimizer(1e-7).minimize(self.loss)
73
+ self .optimizer = tf .train .MomentumOptimizer (1e-3 ,0.9 ,use_nesterov = True ).minimize (self .loss )
73
74
self .saver = tf .train .Saver (max_to_keep = 4 , keep_checkpoint_every_n_hours = 4 )
74
75
self .best_saver = tf .train .Saver (max_to_keep = 10 , keep_checkpoint_every_n_hours = 4 )
75
76
@@ -87,21 +88,21 @@ def train(self):
87
88
print "Initializing Model"
88
89
self .sess .run (tf .group (tf .global_variables_initializer (),tf .local_variables_initializer ()))
89
90
90
- # self.load_det_weights(self.best_model_save_path+'weights_0 .npy')
91
-
91
+ #self.load_det_weights(self.restore_model_path+'weights .npy')
92
+
92
93
93
94
coord = tf .train .Coordinator ()
94
95
threads = tf .train .start_queue_runners (sess = self .sess ,coord = coord )
95
96
96
- writer = tf .summary .FileWriter ('./logs' , self .sess .graph )
97
+ writer = tf .summary .FileWriter ('.. /logs' , self .sess .graph )
97
98
loss_summ = tf .summary .scalar ('loss' , self .loss )
98
99
img_summ = tf .summary .image ('images' , self .images , max_outputs = 5 )
99
- # label_summ = tf.summary.histogram('labels', self.detection)
100
- # detect_summ = tf.summary.scalar('det_loss', self.loss_detection)
101
- # landmarks_summ = tf.summary.scalar('landmarks_loss', self.loss_landmarks)
102
- # vis_summ = tf.summary.scalar('visibility_loss', self.loss_visibility)
103
- # pose_summ = tf.summary.scalar('pose_loss', self.loss_pose)
104
- # gender_summ = tf.summary.scalar('gender_loss', self.loss_gender)
100
+ label_summ = tf .summary .histogram ('labels' , self .detection )
101
+ detect_summ = tf .summary .scalar ('det_loss' , self .loss_detection )
102
+ landmarks_summ = tf .summary .scalar ('landmarks_loss' , self .loss_landmarks )
103
+ vis_summ = tf .summary .scalar ('visibility_loss' , self .loss_visibility )
104
+ pose_summ = tf .summary .scalar ('pose_loss' , self .loss_pose )
105
+ gender_summ = tf .summary .scalar ('gender_loss' , self .loss_gender )
105
106
106
107
summ_op = tf .summary .merge_all ()
107
108
@@ -111,24 +112,26 @@ def train(self):
111
112
while not coord .should_stop ():
112
113
batch_imgs , batch_labels , batch_landmarks , batch_visibility , batch_pose , batch_gender = self .sess .run ([self .images ,self .labels ,self .land , self .vis , self .po , self .gen ])
113
114
batch_imgs = (batch_imgs - 127.5 ) / 128.0
114
- # input_feed={self.X: batch_imgs, self.detection: batch_labels, self.landmarks: batch_landmarks, self.visibility: batch_visibility, self.pose: batch_pose, self.gender: np.squeeze(batch_gender)}
115
- input_feed = {self .X : batch_imgs , self .detection : batch_labels }
115
+ input_feed = {self .X : batch_imgs , self .detection : batch_labels , self .landmarks : batch_landmarks , self .visibility : batch_visibility , self .pose : batch_pose , self .gender : np .squeeze (batch_gender )}
116
+ # input_feed={self.X: batch_imgs, self.detection: batch_labels}
116
117
117
- _ ,loss , summ , accuracy = self .sess .run ([self .optimizer , self .loss , summ_op , self .accuracy ], input_feed )
118
+ _ ,model_op ,loss ,l_d ,l_l ,l_v ,l_p ,l_g , summ , accuracy = self .sess .run ([self .optimizer ,self .test_model ,self .loss ,self .loss_detection ,
119
+ self .loss_landmarks ,self .loss_visibility ,self .loss_pose ,self .loss_gender , summ_op , self .accuracy ], input_feed )
118
120
119
121
writer .add_summary (summ , counter )
120
122
121
123
if counter % self .save_after_steps == 0 :
122
- self .saver .save (self .sess ,self .model_save_path + 'statefarm_model ' ,global_step = int (counter ),write_meta_graph = False )
124
+ self .saver .save (self .sess ,self .model_save_path + 'hyperface_model ' ,global_step = int (counter ),write_meta_graph = False )
123
125
124
126
125
127
if loss <= best_loss :
126
128
best_loss = loss
127
- self .best_saver .save (self .sess ,self .best_model_save_path + 'statefarm_best_model ' ,global_step = int (counter ),write_meta_graph = False )
128
- self .save_weights (self .best_model_save_path )
129
+ self .best_saver .save (self .sess ,self .best_model_save_path + 'hyperface_best_model ' ,global_step = int (counter ),write_meta_graph = False )
130
+ # self.save_weights(self.best_model_save_path)
129
131
130
132
if counter % self .print_after_steps == 0 :
131
- print "Iteration:{},Loss:{},Accuracy:{}" .format (counter ,loss ,accuracy )
133
+ print "Iteration:{},Total Loss:{},Detection loss:{},Landmark loss:{},Visbility Loss :{},Pose Loss:{},Gender Loss:{},Accuracy:{}" .format (counter ,loss ,l_d ,l_l ,l_v ,l_p ,l_g ,accuracy )
134
+
132
135
counter += 1
133
136
134
137
except tf .errors .OutOfRangeError :
@@ -213,7 +216,7 @@ def network(self,inputs,reuse=False):
213
216
fc_gender = slim .fully_connected (fc_full , 512 , scope = 'fc_gender1' )
214
217
215
218
out_detection = slim .fully_connected (fc_detection , 2 , scope = 'fc_detection2' , activation_fn = None )
216
- out_landmarks = slim .fully_connected (fc_landmarks , 42 , scope = 'fc_landmarks2' , activation_fn = None )
219
+ out_landmarks = slim .fully_connected (fc_landmarks , 42 , scope = 'fc_landmarks2' , activation_fn = None )
217
220
out_visibility = slim .fully_connected (fc_visibility , 21 , scope = 'fc_visibility2' , activation_fn = None )
218
221
out_pose = slim .fully_connected (fc_pose , 3 , scope = 'fc_pose2' , activation_fn = None )
219
222
out_gender = slim .fully_connected (fc_gender , 2 , scope = 'fc_gender2' , activation_fn = None )
0 commit comments