-
Notifications
You must be signed in to change notification settings - Fork 87
/
train_JPPNet-s2.py
333 lines (283 loc) · 19.2 KB
/
train_JPPNet-s2.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
from __future__ import print_function
import os
import time
import tensorflow as tf
import numpy as np
import random
from utils import *
from LIP_model import *
# Set gpus
gpus = [0] # Here I set CUDA to only see one GPU
os.environ["CUDA_VISIBLE_DEVICES"]=','.join([str(i) for i in gpus])
num_gpus = len(gpus) # number of GPUs to use
### parameters setting
N_CLASSES = 20
INPUT_SIZE = (384, 384)
BATCH_SIZE = 1
BATCH_I = 1
SHUFFLE = True
RANDOM_SCALE = True
RANDOM_MIRROR = True
LEARNING_RATE = 1e-4
MOMENTUM = 0.9
POWER = 0.9
NUM_STEPS = 7616 * 35 + 1
SAVE_PRED_EVERY = 7616
p_Weight = 1
s_Weight = 1
DATA_DIR = './datasets/lip'
LIST_PATH = './datasets/lip/list/train_rev.txt'
DATA_ID_LIST = './datasets/lip/list/train_id.txt'
SNAPSHOT_DIR = './checkpoint/JPPNet-s2'
LOG_DIR = './logs/JPPNet-s2'
def main():
RANDOM_SEED = random.randint(1000, 9999)
tf.set_random_seed(RANDOM_SEED)
# Create queue coordinator.
coord = tf.train.Coordinator()
h, w = INPUT_SIZE
# Load reader.
with tf.name_scope("create_inputs"):
reader = LIPReader(DATA_DIR, LIST_PATH, DATA_ID_LIST, INPUT_SIZE, RANDOM_SCALE, RANDOM_MIRROR, SHUFFLE, coord)
image_batch, label_batch, heatmap_batch = reader.dequeue(BATCH_SIZE)
image_batch075 = tf.image.resize_images(image_batch, [int(h * 0.75), int(w * 0.75)])
image_batch050 = tf.image.resize_images(image_batch, [int(h * 0.5), int(w * 0.5)])
heatmap_batch = tf.scalar_mul(1.0/255, heatmap_batch)
tower_grads = []
reuse1 = False
reuse2 = False
# Define loss and optimisation parameters.
base_lr = tf.constant(LEARNING_RATE)
step_ph = tf.placeholder(dtype=tf.float32, shape=())
learning_rate = tf.scalar_mul(base_lr, tf.pow((1 - step_ph / NUM_STEPS), POWER))
optim = tf.train.MomentumOptimizer(learning_rate, MOMENTUM)
for i in xrange (num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('Tower_%d' % (i)) as scope:
if i == 0:
reuse1 = False
reuse2 = True
else:
reuse1 = True
reuse2 = True
next_image = image_batch[i*BATCH_I:(i+1)*BATCH_I,:]
next_image075 = image_batch075[i*BATCH_I:(i+1)*BATCH_I,:]
next_image050 = image_batch050[i*BATCH_I:(i+1)*BATCH_I,:]
next_heatmap = heatmap_batch[i*BATCH_I:(i+1)*BATCH_I,:]
next_label = label_batch[i*BATCH_I:(i+1)*BATCH_I,:]
# Create network.
with tf.variable_scope('', reuse=reuse1):
net_100 = JPPNetModel({'data': next_image}, is_training=False, n_classes=N_CLASSES)
with tf.variable_scope('', reuse=reuse2):
net_075 = JPPNetModel({'data': next_image075}, is_training=False, n_classes=N_CLASSES)
with tf.variable_scope('', reuse=reuse2):
net_050 = JPPNetModel({'data': next_image050}, is_training=False, n_classes=N_CLASSES)
# parsing net
parsing_fea1_100 = net_100.layers['res5d_branch2b_parsing']
parsing_fea1_075 = net_075.layers['res5d_branch2b_parsing']
parsing_fea1_050 = net_050.layers['res5d_branch2b_parsing']
parsing_out1_100 = net_100.layers['fc1_human']
parsing_out1_075 = net_075.layers['fc1_human']
parsing_out1_050 = net_050.layers['fc1_human']
# pose net
resnet_fea_100 = net_100.layers['res4b22_relu']
resnet_fea_075 = net_075.layers['res4b22_relu']
resnet_fea_050 = net_050.layers['res4b22_relu']
with tf.variable_scope('', reuse=reuse1):
pose_out1_100, pose_fea1_100 = pose_net(resnet_fea_100, 'fc1_pose')
pose_out2_100, pose_fea2_100 = pose_refine(pose_out1_100, parsing_out1_100, pose_fea1_100, name='fc2_pose')
parsing_out2_100, parsing_fea2_100 = parsing_refine(parsing_out1_100, pose_out1_100, parsing_fea1_100, name='fc2_parsing')
parsing_out3_100, parsing_fea3_100 = parsing_refine(parsing_out2_100, pose_out2_100, parsing_fea2_100, name='fc3_parsing')
pose_out3_100, pose_fea3_100 = pose_refine(pose_out2_100, parsing_out2_100, pose_fea2_100, name='fc3_pose')
with tf.variable_scope('', reuse=reuse2):
pose_out1_075, pose_fea1_075 = pose_net(resnet_fea_075, 'fc1_pose')
pose_out2_075, pose_fea2_075 = pose_refine(pose_out1_075, parsing_out1_075, pose_fea1_075, name='fc2_pose')
parsing_out2_075, parsing_fea2_075 = parsing_refine(parsing_out1_075, pose_out1_075, parsing_fea1_075, name='fc2_parsing')
parsing_out3_075, parsing_fea3_075 = parsing_refine(parsing_out2_075, pose_out2_075, parsing_fea2_075, name='fc3_parsing')
pose_out3_075, pose_fea3_075 = pose_refine(pose_out2_075, parsing_out2_075, pose_fea2_075, name='fc3_pose')
with tf.variable_scope('', reuse=reuse2):
pose_out1_050, pose_fea1_050 = pose_net(resnet_fea_050, 'fc1_pose')
pose_out2_050, pose_fea2_050 = pose_refine(pose_out1_050, parsing_out1_050, pose_fea1_050, name='fc2_pose')
parsing_out2_050, parsing_fea2_050 = parsing_refine(parsing_out1_050, pose_out1_050, parsing_fea1_050, name='fc2_parsing')
parsing_out3_050, parsing_fea3_050 = parsing_refine(parsing_out2_050, pose_out2_050, parsing_fea2_050, name='fc3_parsing')
pose_out3_050, pose_fea3_050 = pose_refine(pose_out2_050, parsing_out2_050, pose_fea2_050, name='fc3_pose')
# combine resize
parsing_out1 = tf.reduce_mean(tf.stack([parsing_out1_100,
tf.image.resize_images(parsing_out1_075, tf.shape(parsing_out1_100)[1:3,]),
tf.image.resize_images(parsing_out1_050, tf.shape(parsing_out1_100)[1:3,])]), axis=0)
parsing_out2 = tf.reduce_mean(tf.stack([parsing_out2_100,
tf.image.resize_images(parsing_out2_075, tf.shape(parsing_out2_100)[1:3,]),
tf.image.resize_images(parsing_out2_050, tf.shape(parsing_out2_100)[1:3,])]), axis=0)
parsing_out3 = tf.reduce_mean(tf.stack([parsing_out3_100,
tf.image.resize_images(parsing_out3_075, tf.shape(parsing_out3_100)[1:3,]),
tf.image.resize_images(parsing_out3_050, tf.shape(parsing_out3_100)[1:3,])]), axis=0)
pose_out1 = tf.reduce_mean(tf.stack([pose_out1_100,
tf.image.resize_nearest_neighbor(pose_out1_075, tf.shape(pose_out1_100)[1:3,]),
tf.image.resize_nearest_neighbor(pose_out1_050, tf.shape(pose_out1_100)[1:3,])]), axis=0)
pose_out2 = tf.reduce_mean(tf.stack([pose_out2_100,
tf.image.resize_nearest_neighbor(pose_out2_075, tf.shape(pose_out2_100)[1:3,]),
tf.image.resize_nearest_neighbor(pose_out2_050, tf.shape(pose_out2_100)[1:3,])]), axis=0)
pose_out3 = tf.reduce_mean(tf.stack([pose_out3_100,
tf.image.resize_nearest_neighbor(pose_out3_075, tf.shape(pose_out3_100)[1:3,]),
tf.image.resize_nearest_neighbor(pose_out3_050, tf.shape(pose_out3_100)[1:3,])]), axis=0)
### Predictions: ignoring all predictions with labels greater or equal than n_classes
raw_prediction_p1 = tf.reshape(parsing_out1, [-1, N_CLASSES])
raw_prediction_p1_100 = tf.reshape(parsing_out1_100, [-1, N_CLASSES])
raw_prediction_p1_075 = tf.reshape(parsing_out1_075, [-1, N_CLASSES])
raw_prediction_p1_050 = tf.reshape(parsing_out1_050, [-1, N_CLASSES])
raw_prediction_p2 = tf.reshape(parsing_out2, [-1, N_CLASSES])
raw_prediction_p2_100 = tf.reshape(parsing_out2_100, [-1, N_CLASSES])
raw_prediction_p2_075 = tf.reshape(parsing_out2_075, [-1, N_CLASSES])
raw_prediction_p2_050 = tf.reshape(parsing_out2_050, [-1, N_CLASSES])
raw_prediction_p3 = tf.reshape(parsing_out3, [-1, N_CLASSES])
raw_prediction_p3_100 = tf.reshape(parsing_out3_100, [-1, N_CLASSES])
raw_prediction_p3_075 = tf.reshape(parsing_out3_075, [-1, N_CLASSES])
raw_prediction_p3_050 = tf.reshape(parsing_out3_050, [-1, N_CLASSES])
label_proc = prepare_label(next_label, tf.stack(parsing_out1.get_shape()[1:3]), one_hot=False) # [batch_size, h, w]
label_proc075 = prepare_label(next_label, tf.stack(parsing_out1_075.get_shape()[1:3]), one_hot=False)
label_proc050 = prepare_label(next_label, tf.stack(parsing_out1_050.get_shape()[1:3]), one_hot=False)
raw_gt = tf.reshape(label_proc, [-1,])
raw_gt075 = tf.reshape(label_proc075, [-1,])
raw_gt050 = tf.reshape(label_proc050, [-1,])
indices = tf.squeeze(tf.where(tf.less_equal(raw_gt, N_CLASSES - 1)), 1)
indices075 = tf.squeeze(tf.where(tf.less_equal(raw_gt075, N_CLASSES - 1)), 1)
indices050 = tf.squeeze(tf.where(tf.less_equal(raw_gt050, N_CLASSES - 1)), 1)
gt = tf.cast(tf.gather(raw_gt, indices), tf.int32)
gt075 = tf.cast(tf.gather(raw_gt075, indices075), tf.int32)
gt050 = tf.cast(tf.gather(raw_gt050, indices050), tf.int32)
prediction_p1 = tf.gather(raw_prediction_p1, indices)
prediction_p1_100 = tf.gather(raw_prediction_p1_100, indices)
prediction_p1_075 = tf.gather(raw_prediction_p1_075, indices075)
prediction_p1_050 = tf.gather(raw_prediction_p1_050, indices050)
prediction_p2 = tf.gather(raw_prediction_p2, indices)
prediction_p2_100 = tf.gather(raw_prediction_p2_100, indices)
prediction_p2_075 = tf.gather(raw_prediction_p2_075, indices075)
prediction_p2_050 = tf.gather(raw_prediction_p2_050, indices050)
prediction_p3 = tf.gather(raw_prediction_p3, indices)
prediction_p3_100 = tf.gather(raw_prediction_p3_100, indices)
prediction_p3_075 = tf.gather(raw_prediction_p3_075, indices075)
prediction_p3_050 = tf.gather(raw_prediction_p3_050, indices050)
next_heatmap075 = tf.image.resize_nearest_neighbor(next_heatmap, pose_out1_075.get_shape()[1:3])
next_heatmap050 = tf.image.resize_nearest_neighbor(next_heatmap, pose_out1_050.get_shape()[1:3])
### Pixel-wise softmax loss.
loss_p1 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p1, labels=gt))
loss_p1_100 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p1_100, labels=gt))
loss_p1_075 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p1_075, labels=gt075))
loss_p1_050 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p1_050, labels=gt050))
loss_p2 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p2, labels=gt))
loss_p2_100 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p2_100, labels=gt))
loss_p2_075 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p2_075, labels=gt075))
loss_p2_050 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p2_050, labels=gt050))
loss_p3 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p3, labels=gt))
loss_p3_100 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p3_100, labels=gt))
loss_p3_075 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p3_075, labels=gt075))
loss_p3_050 = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction_p3_050, labels=gt050))
loss_s1 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out1)), [1, 2, 3])))
loss_s1_100 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out1_100)), [1, 2, 3])))
loss_s1_075 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap075, pose_out1_075)), [1, 2, 3])))
loss_s1_050 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap050, pose_out1_050)), [1, 2, 3])))
loss_s2 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out2)), [1, 2, 3])))
loss_s2_100 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out2_100)), [1, 2, 3])))
loss_s2_075 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap075, pose_out2_075)), [1, 2, 3])))
loss_s2_050 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap050, pose_out2_050)), [1, 2, 3])))
loss_s3 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out3)), [1, 2, 3])))
loss_s3_100 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap, pose_out3_100)), [1, 2, 3])))
loss_s3_075 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap075, pose_out3_075)), [1, 2, 3])))
loss_s3_050 = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(next_heatmap050, pose_out3_050)), [1, 2, 3])))
loss_parsing = loss_p1 + loss_p1_100 + loss_p1_075 + loss_p1_050 + loss_p2 + loss_p2_100 + loss_p2_075 + loss_p2_050 + loss_p3 + loss_p3_100 + loss_p3_075 + loss_p3_050
loss_pose = loss_s1 + loss_s1_100 + loss_s1_075 + loss_s1_050 + loss_s2 + loss_s2_100 + loss_s2_075 + loss_s2_050 + loss_s3 + loss_s3_100 + loss_s3_075 + loss_s3_050
reduced_loss = loss_pose * s_Weight + loss_parsing * p_Weight
trainable_variable = tf.trainable_variables()
grads = optim.compute_gradients(reduced_loss, var_list=trainable_variable)
tower_grads.append(grads)
tf.add_to_collection('loss_p1', loss_p1)
tf.add_to_collection('loss_p2', loss_p2)
tf.add_to_collection('loss_p3', loss_p3)
tf.add_to_collection('loss_s1', loss_s1)
tf.add_to_collection('loss_s2', loss_s2)
tf.add_to_collection('loss_s3', loss_s3)
tf.add_to_collection('reduced_loss', reduced_loss)
# Average the gradients
grads_ave = average_gradients(tower_grads)
# apply the gradients with our optimizers
train_op = optim.apply_gradients(grads_ave)
loss_p1_ave = tf.reduce_mean(tf.get_collection('loss_p1'))
loss_p2_ave = tf.reduce_mean(tf.get_collection('loss_p2'))
loss_p3_ave = tf.reduce_mean(tf.get_collection('loss_p3'))
loss_s1_ave = tf.reduce_mean(tf.get_collection('loss_s1'))
loss_s2_ave = tf.reduce_mean(tf.get_collection('loss_s2'))
loss_s3_ave = tf.reduce_mean(tf.get_collection('loss_s3'))
loss_ave = tf.reduce_mean(tf.get_collection('reduced_loss'))
loss_summary_p1 = tf.summary.scalar("loss_p1_ave", loss_p1_ave)
loss_summary_p2 = tf.summary.scalar("loss_p2_ave", loss_p2_ave)
loss_summary_p3 = tf.summary.scalar("loss_p3_ave", loss_p3_ave)
loss_summary_s1 = tf.summary.scalar("loss_s1_ave", loss_s1_ave)
loss_summary_s2 = tf.summary.scalar("loss_s2_ave", loss_s2_ave)
loss_summary_s3 = tf.summary.scalar("loss_s3_ave", loss_s3_ave)
loss_summary_ave = tf.summary.scalar("loss_ave", loss_ave)
loss_summary = tf.summary.merge([loss_summary_ave, loss_summary_s1, loss_summary_s2, loss_summary_s3, loss_summary_p1, loss_summary_p2, loss_summary_p3])
summary_writer = tf.summary.FileWriter(LOG_DIR, graph=tf.get_default_graph())
# Set up tf session and initialize variables.
config = tf.ConfigProto(allow_soft_placement=True,log_device_placement=False)
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
init = tf.global_variables_initializer()
sess.run(init)
# Saver for storing checkpoints of the model.
all_saver_var = tf.global_variables()
restore_var = all_saver_var #[v for v in all_saver_var if 'pose' not in v.name and 'parsing' not in v.name]
saver = tf.train.Saver(var_list=all_saver_var, max_to_keep=50)
loader = tf.train.Saver(var_list=restore_var)
if load(loader, sess, SNAPSHOT_DIR):
print(" [*] Load SUCCESS")
else:
print(" [!] Load failed...")
# Start queue threads.
threads = tf.train.start_queue_runners(coord=coord, sess=sess)
# Iterate over training steps.
for step in range(NUM_STEPS):
start_time = time.time()
loss_value = 0
feed_dict = { step_ph : step }
# Apply gradients.
summary, loss_value, _ = sess.run([loss_summary, reduced_loss, train_op], feed_dict=feed_dict)
summary_writer.add_summary(summary, step)
if step % SAVE_PRED_EVERY == 0:
save(saver, sess, SNAPSHOT_DIR, step)
duration = time.time() - start_time
print('step {:d} \t loss = {:.3f}, ({:.3f} sec/step)'.format(step, loss_value, duration))
coord.request_stop()
coord.join(threads)
def average_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
if __name__ == '__main__':
main()