@@ -14,7 +14,7 @@ def __init__(self,load_model,tf_record_file_path=None,model_save_path=None,best_
1414 self .img_width = 227
1515 self .channel = 3
1616
17- self .num_epochs = 2
17+ self .num_epochs = 10
1818
1919 # Hyperparameters 1,5,0.5,5,2
2020 self .weight_detect = 1
@@ -43,33 +43,34 @@ def build_network(self, sess):
4343
4444 self .X = tf .placeholder (tf .float32 , [self .batch_size , self .img_height , self .img_width , self .channel ], name = 'images' )
4545 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' )
5050
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 )))
5254
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 )
5656
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 )))
5959
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 )))
6363
6464
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
6868
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 ))
7070
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 )
7374 self .saver = tf .train .Saver (max_to_keep = 4 , keep_checkpoint_every_n_hours = 4 )
7475 self .best_saver = tf .train .Saver (max_to_keep = 10 , keep_checkpoint_every_n_hours = 4 )
7576
@@ -87,21 +88,21 @@ def train(self):
8788 print "Initializing Model"
8889 self .sess .run (tf .group (tf .global_variables_initializer (),tf .local_variables_initializer ()))
8990
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+
9293
9394 coord = tf .train .Coordinator ()
9495 threads = tf .train .start_queue_runners (sess = self .sess ,coord = coord )
9596
96- writer = tf .summary .FileWriter ('./logs' , self .sess .graph )
97+ writer = tf .summary .FileWriter ('.. /logs' , self .sess .graph )
9798 loss_summ = tf .summary .scalar ('loss' , self .loss )
9899 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 )
105106
106107 summ_op = tf .summary .merge_all ()
107108
@@ -111,24 +112,26 @@ def train(self):
111112 while not coord .should_stop ():
112113 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 ])
113114 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}
116117
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 )
118120
119121 writer .add_summary (summ , counter )
120122
121123 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 )
123125
124126
125127 if loss <= best_loss :
126128 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)
129131
130132 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+
132135 counter += 1
133136
134137 except tf .errors .OutOfRangeError :
@@ -213,7 +216,7 @@ def network(self,inputs,reuse=False):
213216 fc_gender = slim .fully_connected (fc_full , 512 , scope = 'fc_gender1' )
214217
215218 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 )
217220 out_visibility = slim .fully_connected (fc_visibility , 21 , scope = 'fc_visibility2' , activation_fn = None )
218221 out_pose = slim .fully_connected (fc_pose , 3 , scope = 'fc_pose2' , activation_fn = None )
219222 out_gender = slim .fully_connected (fc_gender , 2 , scope = 'fc_gender2' , activation_fn = None )
0 commit comments