-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_step1.py
376 lines (329 loc) · 16.9 KB
/
train_step1.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
import os
import time
import numpy
from six.moves import xrange # pylint: disable=redefined-builtin
from get_data_step1 import input_data
import tensorflow as tf
import model
import math
import numpy as np
flags = tf.app.flags
flags.DEFINE_integer('max_steps',40000, 'Number of steps to run trainer.')
flags.DEFINE_integer('batch_size',16 , 'Batch size.')
FLAGS = flags.FLAGS
MOVING_AVERAGE_DECAY = 0.9999
model_save_dir = './models/step1_models'
gpu_num = 1
def placeholder_inputs(batch_size):
#bulit placeholder_inputs
actions_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
model.INPUT_SIZE,
))
scenes_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
model.INPUT_SIZE,
))
motivations_placeholder = tf.placeholder(tf.float32, shape=(batch_size,
model.INPUT_SIZE,
))
ac_labels_placeholder = tf.placeholder(tf.int64, shape=(batch_size))
sc_labels_placeholder = tf.placeholder(tf.int64, shape=(batch_size))
mc_labels_placeholder = tf.placeholder(tf.int64, shape=(batch_size))
keep_pro = tf.placeholder(tf.float32)
return actions_placeholder,scenes_placeholder,motivations_placeholder,ac_labels_placeholder,sc_labels_placeholder,mc_labels_placeholder,keep_pro
def average_gradients(tower_grads):
average_grads = []
for grad_and_vars in zip(*tower_grads):
grads = []
for g, _ in grad_and_vars:
expanded_g = tf.expand_dims(g, 0)
grads.append(expanded_g)
grad = tf.concat(grads, 0)
grad = tf.reduce_mean(grad, 0)
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
def tower_loss(name_scope, logit, labels, classic):
labels=tf.one_hot(labels,classic,on_value=1,off_value=None,axis=1)
#print labels.shape
cross_entropy_mean = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=labels,logits=logit)
)
tf.summary.scalar(
name_scope + '_cross_entropy',
cross_entropy_mean
)
weight_decay_loss = tf.get_collection('weightdecay_losses')
tf.summary.scalar(name_scope + '_weight_decay_loss', tf.reduce_mean(weight_decay_loss) )
# Calculate the total loss for the current tower.
total_loss = cross_entropy_mean
tf.summary.scalar(name_scope + '_total_loss', tf.reduce_mean(total_loss) )
return total_loss
def tower_acc(logit, labels):
correct_pred = tf.equal(tf.argmax(logit, 1), labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
return accuracy
def topk_acc(logit, labels , k):
list=tf.nn.in_top_k(logit,labels,k)
in_top1 = tf.to_float(list)
num_correct = tf.reduce_sum(in_top1)
return num_correct/ 16
def _variable_on_cpu(name, shape, initializer):
with tf.device('/cpu:0'):
var = tf.get_variable(name, shape, initializer=initializer)
return var
def _variable_with_weight_decay(name, shape, wd):
var = _variable_on_cpu(name, shape, tf.contrib.layers.xavier_initializer())
if wd is not None:
weight_decay = tf.nn.l2_loss(var)*wd
tf.add_to_collection('weightdecay_losses', weight_decay)
return var
def run_training():
# Get the sets of images and labels for training, validation, and
# Tell TensorFlow that the model will be built into the default Graph.
# Create model directory
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
use_pretrained_model = True
with tf.Graph().as_default():
global_step = tf.get_variable(
'global_step',
[],
initializer=tf.constant_initializer(0),
trainable=False
)
actions_placeholder,scenes_placeholder,motivations_placeholder,ac_labels_placeholder,sc_labels_placeholder,mc_labels_placeholder,keep_pro = placeholder_inputs(
FLAGS.batch_size * gpu_num
)
tower_grads1 = []
tower_grads2 = []
tower_grads3 = []
sc_logits = []
ac_logits = []
mc_logits = []
learning_rate_sc=tf.train.exponential_decay(1e-4,global_step,decay_steps=FLAGS.max_steps/50,decay_rate=0.98,staircase=True)
learning_rate_ac=tf.train.exponential_decay(1e-4,global_step,decay_steps=FLAGS.max_steps/50,decay_rate=0.98,staircase=True)
learning_rate_mc=tf.train.exponential_decay(1e-4,global_step,decay_steps=FLAGS.max_steps/50,decay_rate=0.98,staircase=True)
tf.summary.scalar('learning_rate', learning_rate_sc)
#tf.summary.scalar('learning_rate2', learning_rate2)
opt_sc = tf.train.AdamOptimizer(learning_rate_sc)
opt_ac = tf.train.AdamOptimizer(learning_rate_ac)
opt_mc = tf.train.AdamOptimizer(learning_rate_mc)
with tf.variable_scope('var_name') as var_scope:
sc_weights = {
'w1': _variable_with_weight_decay('sc_w1', [4800, 4096], 0.005),
'w2': _variable_with_weight_decay('sc_w2', [4096, 2048], 0.005),
'out': _variable_with_weight_decay('sc_wout', [2048, model.SCNUM_CLASSES], 0.005)
}
sc_biases = {
'b1': _variable_with_weight_decay('sc_b1', [4096], 0.000),
'b2': _variable_with_weight_decay('sc_b2', [2048], 0.000),
'out': _variable_with_weight_decay('sc_bout', [model.SCNUM_CLASSES], 0.000),
}
ac_weights = {
'w1': _variable_with_weight_decay('ac_w1', [4800, 4096], 0.005),
'w2': _variable_with_weight_decay('ac_w2', [4096, 2048], 0.005),
'out': _variable_with_weight_decay('ac_wout', [2048, model.ACNUM_CLASSES], 0.005),
'W_alpha': _variable_with_weight_decay('alpha_learn', [100,100], 0.005),
'W_Ua': _variable_with_weight_decay('Ua', [100,100], 0.005),
}
ac_biases = {
'b1': _variable_with_weight_decay('ac_b1', [4096], 0.000),
'b2': _variable_with_weight_decay('ac_b2', [2048], 0.000),
'out': _variable_with_weight_decay('ac_bout', [model.ACNUM_CLASSES], 0.000),
}
mc_weights = {
'w1': _variable_with_weight_decay('mc_w1', [4800, 4096], 0.005),
'w2': _variable_with_weight_decay('mc_w2', [4096, 2048], 0.005),
'out': _variable_with_weight_decay('mc_wout', [2048, model.MCNUM_CLASSES], 0.005),
'W_beta': _variable_with_weight_decay('beta_learn', [256,100], 0.005),
'W_gama': _variable_with_weight_decay('gama_learn', [256,100], 0.005),
'W_Um': _variable_with_weight_decay('Um', [256,256], 0.005),
}
mc_biases = {
'b1': _variable_with_weight_decay('mc_b1', [4096], 0.000),
'b2': _variable_with_weight_decay('mc_b2', [2048], 0.000),
'out': _variable_with_weight_decay('mc_bout', [model.MCNUM_CLASSES], 0.000),
}
for gpu_index in range(0, gpu_num):
with tf.device('/gpu:%d' % gpu_index):
varlist1 = [ sc_weights.values(),sc_biases.values()]
varlist2 = [ ac_weights.values(),ac_biases.values()]
varlist3 = [ mc_weights.values(),mc_biases.values()]
sc_logit = model.sc_model(
scenes_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:],
keep_pro,
FLAGS.batch_size,
sc_weights,
sc_biases
)
ac_logit = model.ac_model(
actions_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:],
sc_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
keep_pro,
FLAGS.batch_size,
ac_weights,
ac_biases
)
mc_logit = model.mc_model(
motivations_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size,:],
sc_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
ac_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
keep_pro,
FLAGS.batch_size,
mc_weights,
mc_biases
)
loss_name_scope = ('gpud_%d_loss' % gpu_index)
sc_loss = tower_loss(
loss_name_scope+'_scene',
sc_logit,
sc_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
model.SCNUM_CLASSES
)
ac_loss = tower_loss(
loss_name_scope+'_action',
ac_logit,
ac_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
model.ACNUM_CLASSES
)
mc_loss = tower_loss(
loss_name_scope+'_motivation',
mc_logit,
mc_labels_placeholder[gpu_index * FLAGS.batch_size:(gpu_index + 1) * FLAGS.batch_size],
model.MCNUM_CLASSES
)
grads1 = opt_sc.compute_gradients(sc_loss, varlist1)
grads2 = opt_ac.compute_gradients(ac_loss, varlist2)
grads3 = opt_mc.compute_gradients(mc_loss, varlist3)
tower_grads1.append(grads1)
tower_grads2.append(grads2)
tower_grads3.append(grads3)
sc_logits.append(sc_logit)
ac_logits.append(ac_logit)
mc_logits.append(mc_logit)
sc_logits = tf.concat(sc_logits,0)
ac_logits = tf.concat(ac_logits,0)
mc_logits = tf.concat(mc_logits,0)
#sc_accuracy = tower_acc(sc_logits, sc_labels_placeholder)
sc_accuracy = topk_acc(sc_logits, sc_labels_placeholder ,5)
tf.summary.scalar('sc_accuracy', sc_accuracy)
#ac_accuracy = tower_acc(ac_logits, ac_labels_placeholder)
ac_accuracy = topk_acc(ac_logits, ac_labels_placeholder ,5)
tf.summary.scalar('ac_accuracy', ac_accuracy)
#mc_accuracy = tower_acc(mc_logits, mc_labels_placeholder)
mc_accuracy = topk_acc(mc_logits, mc_labels_placeholder ,5)
tf.summary.scalar('mc_accuracy', mc_accuracy)
grads1 = average_gradients(tower_grads1)
grads2 = average_gradients(tower_grads2)
grads3 = average_gradients(tower_grads3)
apply_gradient_sc = opt_sc.apply_gradients(grads1, global_step=global_step)
apply_gradient_ac = opt_ac.apply_gradients(grads2, global_step=global_step)
apply_gradient_mc = opt_mc.apply_gradients(grads3, global_step=global_step)
train_sc = tf.group(apply_gradient_sc)
train_ac = tf.group(apply_gradient_ac)
train_mc = tf.group(apply_gradient_mc)
null_op = tf.no_op()
# Create a saver for writing training checkpoints.
saver = tf.train.Saver(sc_weights.values() + sc_biases.values()+ac_weights.values() + ac_biases.values()+mc_weights.values() + mc_biases.values())
init = tf.global_variables_initializer()
# Create a session for running Ops on the Graph.
sess = tf.Session(
config=tf.ConfigProto(allow_soft_placement=True)
)
sess.run(init)
# Create summary writter
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('./visual_logs/step1_visual_logs/train', sess.graph)
test_writer = tf.summary.FileWriter('./visual_logs/step1_visual_logs/test', sess.graph)
for step in xrange(FLAGS.max_steps):
start_time = time.time()
train_actions,train_scenes,train_motivations,train_ac_labels,train_sc_labels,train_mc_labels, _, _= input_data(
filename='./list/train.list',
batch_size=FLAGS.batch_size * gpu_num,
start_pos=-1,
shuffle=True
)
sess.run(train_sc, feed_dict={
actions_placeholder: train_actions,
scenes_placeholder:train_scenes,
motivations_placeholder:train_motivations,
ac_labels_placeholder: train_ac_labels,
sc_labels_placeholder: train_sc_labels,
mc_labels_placeholder: train_mc_labels,
keep_pro : 0.5
})
sess.run(train_ac, feed_dict={
actions_placeholder: train_actions,
scenes_placeholder:train_scenes,
motivations_placeholder:train_motivations,
ac_labels_placeholder: train_ac_labels,
sc_labels_placeholder: train_sc_labels,
mc_labels_placeholder: train_mc_labels,
keep_pro : 0.5
})
sess.run(train_mc, feed_dict={
actions_placeholder: train_actions,
scenes_placeholder:train_scenes,
motivations_placeholder:train_motivations,
ac_labels_placeholder: train_ac_labels,
sc_labels_placeholder: train_sc_labels,
mc_labels_placeholder: train_mc_labels,
keep_pro : 0.5
})
duration = time.time() - start_time
print('Batchnum %d: %.3f sec' % (step, duration))
if (step) %50 == 0 or (step + 1) == FLAGS.max_steps:
print('Step %d/%d: %.3f sec' % (step,FLAGS.max_steps, duration))
print('Training Data Eval:')
summary,sc_acc,ac_acc,mc_acc,sc_loss_value,ac_loss_value,mc_loss_value = sess.run(
[merged,sc_accuracy,ac_accuracy,mc_accuracy,sc_loss,ac_loss,mc_loss],
feed_dict={ actions_placeholder: train_actions,
scenes_placeholder:train_scenes,
motivations_placeholder:train_motivations,
ac_labels_placeholder: train_ac_labels,
sc_labels_placeholder: train_sc_labels,
mc_labels_placeholder: train_mc_labels,
keep_pro : 1
})
print ("sc_accuracy: " + "{:.5f}".format(sc_acc))
print 'sc_loss= %.2f'% np.mean(sc_loss_value)
print ("ac_accuracy: " + "{:.5f}".format(ac_acc))
print 'ac_loss= %.2f'% np.mean(ac_loss_value)
print ("mc_accuracy: " + "{:.5f}".format(mc_acc))
print 'mc_loss= %.2f'% np.mean(mc_loss_value)
train_writer.add_summary(summary, step)
if (step) %100 == 0 or (step + 1) == FLAGS.max_steps:
print('Validation Data Eval:')
val_actions,val_scenes,val_motivations,val_ac_labels,val_sc_labels,val_mc_labels, _, _= input_data(
filename='./list/test.list',
start_pos=-1,
batch_size=FLAGS.batch_size * gpu_num,
shuffle=True)
summary,sc_acc,ac_acc,mc_acc,sc_loss_value,ac_loss_value,mc_loss_value = sess.run(
[merged,sc_accuracy,ac_accuracy,mc_accuracy,sc_loss,ac_loss,mc_loss],
feed_dict={
actions_placeholder: val_actions,
scenes_placeholder:val_scenes,
motivations_placeholder:val_motivations,
ac_labels_placeholder: val_ac_labels,
sc_labels_placeholder: val_sc_labels,
mc_labels_placeholder: val_mc_labels,
keep_pro : 1
})
print ("sc_accuracy: " + "{:.5f}".format(sc_acc))
print 'sc_loss= %.2f'% np.mean(sc_loss_value)
print ("ac_accuracy: " + "{:.5f}".format(ac_acc))
print 'ac_loss= %.2f'% np.mean(ac_loss_value)
print ("mc_accuracy: " + "{:.5f}".format(mc_acc))
print 'mc_loss= %.2f'% np.mean(mc_loss_value)
test_writer.add_summary(summary, step)
# Save the model checkpoint periodically.
if step > 1 and step % 2000 == 0:
checkpoint_path = os.path.join('./models/step1_models', 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=global_step)
print("done")
def main(_):
run_training()
if __name__ == '__main__':
tf.app.run()