14
14
from config import config
15
15
from models import model
16
16
from pycocotools .coco import maskUtils
17
- from tensorlayer .prepro import (
18
- keypoint_random_crop , keypoint_random_flip , keypoint_random_resize ,
19
- keypoint_random_resize_shortestedge , keypoint_random_rotate )
20
- from utils import (PoseInfo , draw_intermedia_results , get_heatmap ,
21
- get_vectormap , load_mscoco_dataset )
17
+ from tensorlayer .prepro import (keypoint_random_crop , keypoint_random_flip , keypoint_random_resize ,
18
+ keypoint_random_resize_shortestedge , keypoint_random_rotate )
19
+ from utils import (PoseInfo , draw_intermedia_results , get_heatmap , get_vectormap , load_mscoco_dataset )
22
20
23
21
tf .logging .set_verbosity (tf .logging .DEBUG )
24
22
tl .logging .set_verbosity (tl .logging .DEBUG )
25
23
26
- tl .files .exists_or_mkdir (
27
- config .LOG .vis_path , verbose = False ) # to save visualization results
28
- tl .files .exists_or_mkdir (
29
- config .MODEL .model_path , verbose = False ) # to save model files
24
+ tl .files .exists_or_mkdir (config .LOG .vis_path , verbose = False ) # to save visualization results
25
+ tl .files .exists_or_mkdir (config .MODEL .model_path , verbose = False ) # to save model files
30
26
31
27
# os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
32
28
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
@@ -83,16 +79,12 @@ def _data_aug_fn(image, ground_truth):
83
79
mask_miss = np .bitwise_and (mask_miss , bin_mask )
84
80
85
81
# image data augmentation
86
- image , annos , mask_miss = keypoint_random_resize (
87
- image , annos , mask_miss , zoom_range = (0.8 , 1.2 ))
88
- image , annos , mask_miss = keypoint_random_rotate (
89
- image , annos , mask_miss , rg = 15.0 )
90
- image , annos , mask_miss = keypoint_random_flip (
91
- image , annos , mask_miss , prob = 0.5 )
82
+ image , annos , mask_miss = keypoint_random_resize (image , annos , mask_miss , zoom_range = (0.8 , 1.2 ))
83
+ image , annos , mask_miss = keypoint_random_rotate (image , annos , mask_miss , rg = 15.0 )
84
+ image , annos , mask_miss = keypoint_random_flip (image , annos , mask_miss , prob = 0.5 )
92
85
image , annos , mask_miss = keypoint_random_resize_shortestedge (
93
86
image , annos , mask_miss , min_size = (hin , win )) # TODO: give size
94
- image , annos , mask_miss = keypoint_random_crop (
95
- image , annos , mask_miss , size = (hin , win )) # TODO: give size
87
+ image , annos , mask_miss = keypoint_random_crop (image , annos , mask_miss , size = (hin , win )) # TODO: give size
96
88
97
89
# generate result maps including keypoints heatmap, pafs and mask
98
90
h , w , _ = np .shape (image )
@@ -107,8 +99,7 @@ def _data_aug_fn(image, ground_truth):
107
99
image = image * np .repeat (img_mask , 3 , 2 )
108
100
109
101
resultmap = np .array (resultmap , dtype = np .float32 )
110
- mask_miss = cv2 .resize (
111
- mask_miss , (hout , wout ), interpolation = cv2 .INTER_AREA )
102
+ mask_miss = cv2 .resize (mask_miss , (hout , wout ), interpolation = cv2 .INTER_AREA )
112
103
mask_miss = np .array (mask_miss , dtype = np .float32 )
113
104
return image , resultmap , mask_miss
114
105
@@ -118,8 +109,7 @@ def _map_fn(img_list, annos):
118
109
image = tf .read_file (img_list )
119
110
image = tf .image .decode_jpeg (image , channels = 3 ) # get RGB with 0~1
120
111
image = tf .image .convert_image_dtype (image , dtype = tf .float32 )
121
- image , resultmap , mask = tf .py_func (_data_aug_fn , [image , annos ],
122
- [tf .float32 , tf .float32 , tf .float32 ])
112
+ image , resultmap , mask = tf .py_func (_data_aug_fn , [image , annos ], [tf .float32 , tf .float32 , tf .float32 ])
123
113
return image , resultmap , mask
124
114
125
115
@@ -136,8 +126,7 @@ def _map_fn(img_list, annos):
136
126
train_mask_list = train_data .get_mask ()
137
127
# train_targets = list(zip(train_objs_info_list, train_mask_list))
138
128
if len (train_imgs_file_list ) != len (train_objs_info_list ):
139
- raise Exception (
140
- "number of training images and annotations do not match" )
129
+ raise Exception ("number of training images and annotations do not match" )
141
130
else :
142
131
print ("number of training images {}" .format (len (train_imgs_file_list )))
143
132
@@ -160,21 +149,17 @@ def _map_fn(img_list, annos):
160
149
your_objs_info_list = your_data .get_joint_list ()
161
150
your_mask_list = your_data .get_mask ()
162
151
if len (your_imgs_file_list ) != len (your_objs_info_list ):
163
- raise Exception (
164
- "number of customized images and annotations do not match" )
152
+ raise Exception ("number of customized images and annotations do not match" )
165
153
else :
166
- print ("number of customized images {}" .format (
167
- len (your_imgs_file_list )))
154
+ print ("number of customized images {}" .format (len (your_imgs_file_list )))
168
155
169
156
# choice dataset for training
170
157
# 1. only coco training set
171
158
# imgs_file_list = train_imgs_file_list
172
159
# train_targets = list(zip(train_objs_info_list, train_mask_list))
173
160
# 2. your customized data from "data/your_data" and coco training set
174
161
imgs_file_list = train_imgs_file_list + your_imgs_file_list
175
- train_targets = list (
176
- zip (train_objs_info_list + your_objs_info_list ,
177
- train_mask_list + your_mask_list ))
162
+ train_targets = list (zip (train_objs_info_list + your_objs_info_list , train_mask_list + your_mask_list ))
178
163
179
164
# define data augmentation
180
165
def generator ():
@@ -183,8 +168,7 @@ def generator():
183
168
for _input , _target in zip (imgs_file_list , train_targets ):
184
169
yield _input .encode ('utf-8' ), cPickle .dumps (_target )
185
170
186
- dataset = tf .data .Dataset ().from_generator (
187
- generator , output_types = (tf .string , tf .string ))
171
+ dataset = tf .data .Dataset ().from_generator (generator , output_types = (tf .string , tf .string ))
188
172
dataset = dataset .map (_map_fn , num_parallel_calls = 8 )
189
173
dataset = dataset .shuffle (buffer_size = 2046 )
190
174
dataset = dataset .repeat (n_epoch )
@@ -197,19 +181,14 @@ def generator():
197
181
# Train with placeholder can help your to check the data easily.
198
182
# define model architecture
199
183
x = tf .placeholder (tf .float32 , [None , hin , win , 3 ], "image" )
200
- confs = tf .placeholder (tf .float32 , [None , hout , wout , n_pos ],
201
- "confidence_maps" )
202
- pafs = tf .placeholder (tf .float32 , [None , hout , wout , n_pos * 2 ],
203
- "pafs" )
184
+ confs = tf .placeholder (tf .float32 , [None , hout , wout , n_pos ], "confidence_maps" )
185
+ pafs = tf .placeholder (tf .float32 , [None , hout , wout , n_pos * 2 ], "pafs" )
204
186
# if the people does not have keypoints annotations, ignore the area
205
- img_mask1 = tf .placeholder (tf .float32 , [None , hout , wout , n_pos ],
206
- 'img_mask1' )
207
- img_mask2 = tf .placeholder (tf .float32 , [None , hout , wout , n_pos * 2 ],
208
- 'img_mask2' )
187
+ img_mask1 = tf .placeholder (tf .float32 , [None , hout , wout , n_pos ], 'img_mask1' )
188
+ img_mask2 = tf .placeholder (tf .float32 , [None , hout , wout , n_pos * 2 ], 'img_mask2' )
209
189
num_images = np .shape (imgs_file_list )[0 ]
210
190
211
- cnn , b1_list , b2_list , net = model (x , n_pos , img_mask1 , img_mask2 ,
212
- True , False )
191
+ cnn , b1_list , b2_list , net = model (x , n_pos , img_mask1 , img_mask2 , True , False )
213
192
214
193
# define loss
215
194
losses = []
@@ -218,12 +197,8 @@ def generator():
218
197
stage_losses = []
219
198
L2 = 0.0
220
199
for idx , (l1 , l2 ) in enumerate (zip (b1_list , b2_list )):
221
- loss_l1 = tf .nn .l2_loss (
222
- (tf .concat (l1 .outputs , axis = 0 ) - tf .concat (confs , axis = 0 )) *
223
- img_mask1 )
224
- loss_l2 = tf .nn .l2_loss (
225
- (tf .concat (l2 .outputs , axis = 0 ) - tf .concat (pafs , axis = 0 )) *
226
- img_mask2 )
200
+ loss_l1 = tf .nn .l2_loss ((tf .concat (l1 .outputs , axis = 0 ) - tf .concat (confs , axis = 0 )) * img_mask1 )
201
+ loss_l2 = tf .nn .l2_loss ((tf .concat (l2 .outputs , axis = 0 ) - tf .concat (pafs , axis = 0 )) * img_mask2 )
227
202
losses .append (tf .reduce_mean ([loss_l1 , loss_l2 ]))
228
203
stage_losses .append (loss_l1 / batch_size )
229
204
stage_losses .append (loss_l2 / batch_size )
@@ -237,15 +212,14 @@ def generator():
237
212
total_loss = tf .reduce_sum (losses ) / batch_size + L2
238
213
239
214
global_step = tf .Variable (1 , trainable = False )
240
- print ('Config:' , 'n_epoch: ' , n_epoch , 'batch_size: ' , batch_size ,
241
- 'base_lr: ' , base_lr , 'step_size: ' , step_size )
215
+ print ('Config:' , 'n_epoch: ' , n_epoch , 'batch_size: ' , batch_size , 'base_lr: ' , base_lr , 'step_size: ' ,
216
+ step_size )
242
217
with tf .variable_scope ('learning_rate' ):
243
218
lr_v = tf .Variable (base_lr , trainable = False )
244
219
245
220
opt = tf .train .MomentumOptimizer (lr_v , 0.9 )
246
221
train_op = opt .minimize (total_loss , global_step = global_step )
247
- config = tf .ConfigProto (
248
- allow_soft_placement = True , log_device_placement = False )
222
+ config = tf .ConfigProto (allow_soft_placement = True , log_device_placement = False )
249
223
250
224
# start training
251
225
with tf .Session (config = config ) as sess :
@@ -292,14 +266,8 @@ def generator():
292
266
# os.path.join(config.LOG.vis_path, 'data_aug_{}.png'.format(i))
293
267
# tl.file.save_image()
294
268
295
- [
296
- _ , the_loss , loss_ll , L2_reg , conf_result , weight_norm ,
297
- paf_result
298
- ] = sess .run (
299
- [
300
- train_op , total_loss , stage_losses , L2 , last_conf , L2 ,
301
- last_paf
302
- ],
269
+ [_ , the_loss , loss_ll , L2_reg , conf_result , weight_norm , paf_result ] = sess .run (
270
+ [train_op , total_loss , stage_losses , L2 , last_conf , L2 , last_paf ],
303
271
feed_dict = {
304
272
x : x_ ,
305
273
confs : confs_ ,
@@ -308,35 +276,25 @@ def generator():
308
276
img_mask2 : mask2
309
277
})
310
278
311
- tstring = time .strftime ('%d-%m %H:%M:%S' ,
312
- time .localtime (time .time ()))
279
+ tstring = time .strftime ('%d-%m %H:%M:%S' , time .localtime (time .time ()))
313
280
lr = sess .run (lr_v )
314
- print (
315
- 'Total Loss at iteration {} is: {} Learning rate {:10e} weight_norm {:10e} Time: {}' .
316
- format (gs_num , the_loss , lr , weight_norm , tstring ))
281
+ print ('Total Loss at iteration {} is: {} Learning rate {:10e} weight_norm {:10e} Time: {}' .format (
282
+ gs_num , the_loss , lr , weight_norm , tstring ))
317
283
for ix , ll in enumerate (loss_ll ):
318
- print ('Network#' , ix , 'For Branch' , ix % 2 + 1 , 'Loss:' ,
319
- ll )
284
+ print ('Network#' , ix , 'For Branch' , ix % 2 + 1 , 'Loss:' , ll )
320
285
321
286
# save some intermedian results
322
287
if (gs_num != 0 ) and (gs_num % 1 == 0 ): # save_interval == 0):
323
- draw_intermedia_results (x_ , confs_ , conf_result , pafs_ ,
324
- paf_result , mask , 'train' )
288
+ draw_intermedia_results (x_ , confs_ , conf_result , pafs_ , paf_result , mask , 'train' )
325
289
# np.save(config.LOG.vis_path + 'image' + str(gs_num) + '.npy', x_)
326
290
# np.save(config.LOG.vis_path + 'heat_ground' + str(gs_num) + '.npy', confs_)
327
291
# np.save(config.LOG.vis_path + 'heat_result' + str(gs_num) + '.npy', conf_result)
328
292
# np.save(config.LOG.vis_path + 'paf_ground' + str(gs_num) + '.npy', pafs_)
329
293
# np.save(config.LOG.vis_path + 'mask' + str(gs_num) + '.npy', mask)
330
294
# np.save(config.LOG.vis_path + 'paf_result' + str(gs_num) + '.npy', paf_result)
331
295
tl .files .save_npz_dict (
332
- net .all_params ,
333
- os .path .join (model_path ,
334
- 'pose' + str (gs_num ) + '.npz' ),
335
- sess = sess )
336
- tl .files .save_npz_dict (
337
- net .all_params ,
338
- os .path .join (model_path , 'pose.npz' ),
339
- sess = sess )
296
+ net .all_params , os .path .join (model_path , 'pose' + str (gs_num ) + '.npz' ), sess = sess )
297
+ tl .files .save_npz_dict (net .all_params , os .path .join (model_path , 'pose.npz' ), sess = sess )
340
298
if gs_num > 3000001 :
341
299
break
342
300
elif config .TRAIN .train_mode == 'dataset' : # TODO
0 commit comments