-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_ucf11_recognition1.py
361 lines (328 loc) · 18.9 KB
/
train_ucf11_recognition1.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
__author__ = 'liuyu' ### check
import os.path
import time
import numpy as np
import tensorflow as tf
import cv2
import sys
import random
from nets import models_factory_action
from data_provider import datasets_factory_action
from utils import preprocess
from utils import metrics
from skimage.measure import compare_ssim
import tensorflow.contrib.slim as slim
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
# -----------------------------------------------------------------------------
FLAGS = tf.app.flags.FLAGS
# data I/O
tf.app.flags.DEFINE_string('dataset_name', 'ucf11_action',
'The name of dataset.')
tf.app.flags.DEFINE_string('train_data_paths',
'data/ucf101/ucf11/',
'train data paths.')
tf.app.flags.DEFINE_string('valid_data_paths',
'data/ucf101/ucf11/',
'validation data paths.')
tf.app.flags.DEFINE_string('save_dir', 'checkpoints_ucf11_action/ucf11_action_stdlstm_v6',
'dir to store trained net.')
tf.app.flags.DEFINE_string('gen_frm_dir', 'results_ucf11_action/ucf11_action_stdlstm_v6',
'dir to store result.')
# model
tf.app.flags.DEFINE_string('model_name', 'st_dlstm_my_action',
'The name of the architecture.')
tf.app.flags.DEFINE_string('pretrained_model', 'checkpoints_ucf101/ucf101_stdlstm_v2/model.ckpt-80000',
'file of a pretrained model to initialize from.')
tf.app.flags.DEFINE_integer('input_length', 16,
'encoder hidden states.')
tf.app.flags.DEFINE_integer('seq_length', 16, # 20 for train
'total input and output length.')
tf.app.flags.DEFINE_integer('classes', 11,
'total classes.')
tf.app.flags.DEFINE_integer('img_width', 80,
'input image width.')
tf.app.flags.DEFINE_integer('img_channel', 3,
'number of image channel.')
tf.app.flags.DEFINE_integer('stride', 1,
'stride of a convlstm layer.')
tf.app.flags.DEFINE_integer('filter_size', 5, #5
'filter of a convlstm layer.')
tf.app.flags.DEFINE_string('num_hidden', '128,64,64,64',
'COMMA separated number of units in a convlstm layer.')
tf.app.flags.DEFINE_integer('patch_size', 8, #8
'patch size on one dimension.')
tf.app.flags.DEFINE_boolean('layer_norm', True,
'whether to apply tensor layer norm.')
# optimization
tf.app.flags.DEFINE_float('lr', 1e-4,
'base learning rate.')
tf.app.flags.DEFINE_boolean('reverse_input', True,
'whether to reverse the input frames while training.')
tf.app.flags.DEFINE_integer('batch_size', 32,
'batch size for training.')
# tf.app.flags.DEFINE_integer('batch_size', 128,
# 'batch size for training.')
tf.app.flags.DEFINE_integer('max_iterations', 80, # 80000
'max num of steps.')
# tf.app.flags.DEFINE_integer('max_iterations', 4,
# 'max num of steps.')
tf.app.flags.DEFINE_integer('display_interval', 1,
'number of iters showing training loss.')
tf.app.flags.DEFINE_integer('test_interval', 1, # 2000
'number of iters for test.')
# tf.app.flags.DEFINE_integer('test_interval', 2,
# 'number of iters for test.')
tf.app.flags.DEFINE_integer('snapshot_interval', 1,
'number of iters saving models.')
# tf.app.flags.DEFINE_integer('snapshot_interval', 5,
# 'number of iters saving models.')
include = ['predrnn_pp/lstm_4/c2mb', 'predrnn_pp/lstm_2/x2cs', 'predrnn_pp/lstm_3/conv_spatial/kernel',
'predrnn_pp/lstm_4/c2ms',
'predrnn_pp/lstm_2/x2cb', 'predrnn_pp/lstm_2/conv_distance/bias', 'predrnn_pp/lstm_2/c2cb',
'predrnn_pp/lstm_1/h2cb',
'predrnn_pp/lstm_1/h2cs', 'predrnn_pp/lstm_1/m2mb',
'predrnn_pp/lstm_4/spatial_memory_transition/kernel',
'predrnn_pp/lstm_3/temporal_memory_transition/bias', 'predrnn_pp/lstm_4/conv_spatial/bias',
'predrnn_pp/lstm_3/memory_reduce/bias', 'predrnn_pp/lstm_2/conv_spatial/kernel',
'predrnn_pp/lstm_1/input_to_state/kernel',
'predrnn_pp/lstm_2/conv_distance/kernel', 'predrnn_pp/lstm_4/m2ms',
'predrnn_pp/lstm_1/conv_distance/bias',
'predrnn_pp/lstm_4/m2mb', 'predrnn_pp/lstm_2/c2cs',
'predrnn_pp/lstm_2/temporal_memory_transition/kernel',
'predrnn_pp/lstm_2/conv_spatial/bias', 'predrnn_pp/lstm_3/x2cs',
'predrnn_pp/highway/state_to_state/bias',
'predrnn_pp/lstm_3/x2cb', 'predrnn_pp/lstm_3/temporal_state_transition/kernel',
'predrnn_pp/lstm_3/temporal_memory_transition/kernel',
'predrnn_pp/lstm_4/c2cs', 'predrnn_pp/lstm_1/c2mb', 'predrnn_pp/lstm_1/c2ms',
'predrnn_pp/lstm_4/c2cb',
'predrnn_pp/lstm_2/c_cs', 'predrnn_pp/lstm_3/m2mb',
'predrnn_pp/lstm_1/temporal_state_transition/bias',
'predrnn_pp/lstm_4/c_cb', 'predrnn_pp/lstm_4/c_cs', 'predrnn_pp/lstm_4/memory_reduce/bias',
'predrnn_pp/lstm_3/conv_spatial/bias', 'predrnn_pp/highway/input_to_stateb',
'predrnn_pp/lstm_4/conv_distance/bias',
'predrnn_pp/highway/input_to_states', 'predrnn_pp/lstm_4/spatial_memory_transition/bias',
'predrnn_pp/lstm_1/m2ms', 'predrnn_pp/lstm_4/temporal_memory_transition/bias',
'predrnn_pp/lstm_3/m_to_o/kernel',
'predrnn_pp/lstm_1/spatial_memory_transition/bias', 'predrnn_pp/lstm_1/x2cs',
'predrnn_pp/lstm_2/temporal_state_transition/kernel', 'predrnn_pp/lstm_4/c2m/kernel',
'predrnn_pp/lstm_1/x2cb', 'predrnn_pp/lstm_1/m_mb', 'predrnn_pp/lstm_2/m2os',
'predrnn_pp/lstm_1/m_ms',
'predrnn_pp/lstm_1/temporal_state_transition/kernel', 'predrnn_pp/lstm_3/memory_reduce/kernel',
'predrnn_pp/lstm_2/m2ob', 'predrnn_pp/lstm_1/m_to_o/kernel',
'predrnn_pp/lstm_1/conv_distance/kernel',
'predrnn_pp/lstm_3/c2mb', 'predrnn_pp/lstm_4/input_to_state/kernel', 'predrnn_pp/lstm_3/c2ms',
'predrnn_pp/lstm_1/c2m/bias', 'predrnn_pp/lstm_2/temporal_memory_transition/bias',
'predrnn_pp/lstm_2/m_ms', 'predrnn_pp/lstm_2/m_mb,predrnn_pp/lstm_1/c_cs',
'predrnn_pp/lstm_1/temporal_memory_transition/bias', 'predrnn_pp/lstm_1/c_cb',
'predrnn_pp/lstm_1/c2m/kernel', 'predrnn_pp/lstm_2/memory_reduce/kernel',
'predrnn_pp/highway/input_to_state/kernel', 'predrnn_pp/highway/input_to_state/bias',
'predrnn_pp/lstm_4/conv_distance/kernel', 'predrnn_pp/lstm_2/memory_reduce/bias',
'predrnn_pp/lstm_3/c2m/kernel', 'predrnn_pp/lstm_3/conv_distance/kernel',
'predrnn_pp/lstm_3/m2ms', 'predrnn_pp/lstm_1/conv_spatial/bias',
'predrnn_pp/lstm_3/spatial_memory_transition/bias',
'predrnn_pp/lstm_2/spatial_memory_transition/kernel',
'predrnn_pp/lstm_2/c2m/bias,predrnn_pp/lstm_3/h2cb', 'predrnn_pp/lstm_3/conv_distance/bias',
'predrnn_pp/lstm_3/h2cs', 'predrnn_pp/lstm_1/input_to_state/bias',
'predrnn_pp/lstm_4/temporal_state_transition/bias',
'predrnn_pp/lstm_4/m2os', 'predrnn_pp/lstm_4/m2ob', 'predrnn_pp/lstm_2/m_to_o/kernel',
'predrnn_pp/lstm_3/c_cs', 'predrnn_pp/lstm_3/c_cb', 'predrnn_pp/lstm_3/c2m/bias',
'predrnn_pp/lstm_2/temporal_state_transition/bias', 'predrnn_pp/lstm_4/conv_spatial/kernel',
'predrnn_pp/lstm_4/m_to_o/kernel', 'predrnn_pp/lstm_4/input_to_state/bias',
'predrnn_pp/lstm_1/memory_reduce/bias', 'predrnn_pp/lstm_1/temporal_memory_transition/kernel',
'predrnn_pp/lstm_4/h2cs', 'predrnn_pp/lstm_3/c2cs', 'predrnn_pp/lstm_4/h2cb',
'predrnn_pp/lstm_3/c2cb',
'predrnn_pp/highway/state_to_stateb', 'predrnn_pp/lstm_2/spatial_memory_transition/bias',
'predrnn_pp/highway/state_to_states', 'predrnn_pp/lstm_3/temporal_state_transition/bias',
'predrnn_pp/lstm_3/input_to_state/kernel', 'predrnn_pp/lstm_4/temporal_memory_transition/kernel',
'predrnn_pp/lstm_2/input_to_state/bias', 'predrnn_pp/lstm_2/input_to_state/kernel',
'predrnn_pp/lstm_1/m2os', 'predrnn_pp/lstm_3/spatial_memory_transition/kernel',
'predrnn_pp/lstm_1/m2ob', 'predrnn_pp/lstm_2/m2ms', 'predrnn_pp/lstm_4/m_mb',
'predrnn_pp/lstm_3/input_to_state/bias', 'predrnn_pp/lstm_2/m_to_o/bias',
'predrnn_pp/lstm_3/m2ob',
'predrnn_pp/lstm_4/m_to_o/bias', 'predrnn_pp/lstm_3/m2os', ' predrnn_pp/lstm_2/c2m/kernel',
'predrnn_pp/lstm_3/m_to_o/bias', 'predrnn_pp/lstm_4/temporal_state_transition/kernel',
'predrnn_pp/lstm_1/conv_spatial/kernel', 'predrnn_pp/lstm_4/c2m/bias',
'predrnn_pp/lstm_4/memory_reduce/kernel',
'predrnn_pp/lstm_2/c_cb', 'predrnn_pp/lstm_1/c2cs', 'predrnn_pp/lstm_1/c2cb',
'predrnn_pp/lstm_3/m_mb',
'predrnn_pp/lstm_1/spatial_memory_transition/kernel', 'predrnn_pp/lstm_2/h2cb',
'predrnn_pp/lstm_3/m_ms',
'predrnn_pp/lstm_2/h2cs', 'predrnn_pp/lstm_4/m_ms', 'predrnn_pp/lstm_4/x2cb',
'predrnn_pp/highway/state_to_state/kernel', 'predrnn_pp/lstm_2/m2mb', 'predrnn_pp/lstm_4/x2cs',
'predrnn_pp/lstm_1/m_to_o/bias', 'predrnn_pp/lstm_2/c2mb', 'predrnn_pp/lstm_2/c2ms',
'predrnn_pp/lstm_1/memory_reduce/kernel']
def get_variable_via_scope(scope_lst):
vars = []
for sc in scope_lst:
sc_variable = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope=sc)
vars.extend(sc_variable)
return vars
class Model(object):
def __init__(self):
# inputs
self.x = tf.placeholder(tf.float32,[FLAGS.batch_size,FLAGS.seq_length,
FLAGS.img_width / FLAGS.patch_size,
FLAGS.img_width / FLAGS.patch_size,
FLAGS.patch_size * FLAGS.patch_size * FLAGS.img_channel])
self.y = tf.placeholder(tf.int32,[FLAGS.batch_size])
grads = []
loss_train = []
self.tf_lr = tf.placeholder(tf.float32, shape=[])
num_hidden = [int(x) for x in FLAGS.num_hidden.split(',')]
print(num_hidden)
num_layers = len(num_hidden)
with tf.variable_scope(tf.get_variable_scope()):
# define a model
loss, acc, pred, label, top5_acc = models_factory_action.construct_model(
FLAGS.model_name, self.x,
self.y,
num_layers, num_hidden,
FLAGS.filter_size, FLAGS.stride,FLAGS.classes,
FLAGS.seq_length, FLAGS.input_length,
FLAGS.layer_norm)
#self.loss_train = loss / FLAGS.batch_size
self.loss_train = loss
self.accuracy = acc
self.pred = pred
self.label = label
self.top5_acc = top5_acc
# gradients
all_params = tf.trainable_variables()
no_change_scope = include
no_change_vars = get_variable_via_scope(no_change_scope)
for v in no_change_vars:
all_params.remove(v)
grads.append(tf.gradients(loss, all_params))
self.train_op = tf.train.AdamOptimizer(FLAGS.lr).minimize(loss)
#self.train_op = tf.train.GradientDescentOptimizer(FLAGS.lr).minimize(loss)
# session
variables = tf.global_variables()
self.saver = tf.train.Saver(variables)
init = tf.global_variables_initializer()
configProt = tf.ConfigProto()
configProt.gpu_options.allow_growth = True
configProt.allow_soft_placement = True
self.sess = tf.Session(config=configProt)
self.sess.run(init)
if FLAGS.pretrained_model:
#exclude = ['back_to_pixel']
variables_to_restore = slim.get_variables_to_restore(include=include)
self.saver = tf.train.Saver(variables_to_restore)
self.saver.restore(self.sess, FLAGS.pretrained_model)
#self.saver.restore(self.sess, FLAGS.pretrained_model)
def train(self, inputs, lr, targets):
feed_dict = {self.x: inputs}
feed_dict.update({self.tf_lr: lr})
feed_dict.update({self.y: targets})
loss, acc, pred, label, top5_acc, _ = self.sess.run((self.loss_train, self.accuracy, self.pred, self.label, self.top5_acc, self.train_op), feed_dict)
return loss, acc, pred, label, top5_acc
def test(self, inputs, targets):
feed_dict = {self.x: inputs}
feed_dict.update({self.y: targets})
loss, acc, pred, label, top5_acc = self.sess.run((self.loss_train, self.accuracy, self.pred, self.label, self.top5_acc), feed_dict)
return loss, acc, pred, label, top5_acc
def save(self, itr):
checkpoint_path = os.path.join(FLAGS.save_dir, 'model.ckpt')
self.saver.save(self.sess, checkpoint_path, global_step=itr)
print('saved to ' + FLAGS.save_dir)
def main(argv=None):
if tf.gfile.Exists(FLAGS.save_dir):
tf.gfile.DeleteRecursively(FLAGS.save_dir) ## if file is not none, clean all recursively - note by liuyu
tf.gfile.MakeDirs(FLAGS.save_dir)
if tf.gfile.Exists(FLAGS.gen_frm_dir):
tf.gfile.DeleteRecursively(FLAGS.gen_frm_dir)
tf.gfile.MakeDirs(FLAGS.gen_frm_dir)
# load data
train_input_handle, test_input_handle = datasets_factory_action.data_provider(
FLAGS.dataset_name, FLAGS.train_data_paths, FLAGS.valid_data_paths,
FLAGS.batch_size, FLAGS.img_width, FLAGS.seq_length)
f = open(FLAGS.save_dir + '/summary.txt', 'a') #### add
print("Initializing models")
model = Model()
### total parameters
total_parameters = 0
for variable in tf.trainable_variables():
variable_parameters = 1
for dim in variable.get_shape():
variable_parameters *= dim.value
total_parameters += variable_parameters
print("Total number of trainable parameters: %d" % total_parameters)
f.write('\nTotal number of trainable parameters: %d' % total_parameters)
lr = FLAGS.lr
#acc_s = []
#acc_s.append(0)
for itr in xrange(1, FLAGS.max_iterations + 1):
if train_input_handle.no_batch_left():
train_input_handle.begin(do_shuffle=True)
while (train_input_handle.no_batch_left() == False):
ims, labels = train_input_handle.get_batch()
ims = preprocess.reshape_patch(ims, FLAGS.patch_size)
cost, accuracy, pred, label, top5_acc = model.train(ims, lr, labels)
train_input_handle.next()
print('img shape: ', ims.shape)
#ims = preprocess.reshape_patch(ims, FLAGS.patch_size)
#print('img reshape: ', ims.shape)
print('learning rate: ', lr)
for i in range(8):
print('labels: ', labels[i])
print('predictions: ', pred[i])
#print('one hot label: ', label[i])
if itr % FLAGS.display_interval == 0:
print('itr: ' + str(itr))
print('training loss: ' + str(cost))
print('training accuracy: ' + str(accuracy))
print('training top5 accuracy: ' + str(top5_acc))
#### add
if itr % FLAGS.test_interval == 0:
f.write('\nitr: %d' % itr)
f.write('\ntraining loss: %f' % cost)
f.write('\ntraining accuracy: %f' % accuracy)
f.write('\ntraining top5 accuracy: %f' % top5_acc)
if itr % FLAGS.test_interval == 0:
print('test...')
test_input_handle.begin(do_shuffle=False)
res_path = os.path.join(FLAGS.gen_frm_dir, str(itr))
os.mkdir(res_path)
avg_ce = 0
avg_acc = 0
avg_top5_acc = 0
batch_id = 0
while (test_input_handle.no_batch_left() == False):
batch_id = batch_id + 1
test_ims, test_labels = test_input_handle.get_batch()
test_dat = preprocess.reshape_patch(test_ims, FLAGS.patch_size)
#test_dat = test_ims
ce, acc, pred, label, top5_acc = model.test(test_dat, test_labels)
avg_ce += ce
avg_acc += acc
avg_top5_acc += top5_acc
# concat outputs of different gpus along batch
test_input_handle.next()
# save prediction examples
#if batch_id <= 10:
#path = os.path.join(res_path, str(batch_id))
#os.mkdir(path)
#for i in xrange(FLAGS.seq_length):
#name = 'gt' + str(i + 1) + '.png'
#file_name = os.path.join(path, name)
#img_gt = np.uint8(test_ims[0, i, :, :, :] * 255)
#cv2.imwrite(file_name, img_gt)
#print('-----------prediction examples labels: ', test_labels[0])
avg_ce = avg_ce / batch_id
avg_acc = avg_acc / batch_id
avg_top5_acc = avg_top5_acc / batch_id
print('ce per seq: ' + str(avg_ce))
f.write('\ntest ce: %f' % avg_ce) #### add
print('acc per seq: ' + str(avg_acc))
f.write('\ntest acc: %f' % avg_acc) #### add
print('top5 acc per seq: ' + str(top5_acc))
f.write('\ntest top5 acc: %f' % top5_acc) #### add
if avg_acc > 0.3:
model.save(itr)
#acc_s.append(avg_acc)
#if itr % FLAGS.snapshot_interval == 0 and avg_acc > 0.3:
#model.save(itr)
f.close()
if __name__ == '__main__':
tf.app.run()