-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
221 lines (161 loc) · 8.89 KB
/
main.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
import tensorflow as tf
import time, os, sys
from tqdm import tqdm
import math
import numpy as np
import pickle
from model import MultiDenseNet
from dataloader import DataLoader
from plot import draw_training_curve, draw_actionness, draw_proposals
from configuration import Logger, parse_base_args
from generate_proposals import generate_proposals
from eval import evaluation_proposal
def run_training(args):
with tf.Graph().as_default():
global_step = tf.Variable(0, trainable=False)
# build model
model_input = tf.placeholder(tf.float32, shape=[args.batch,args.in_window,2048])
anno_action = tf.placeholder(tf.float32, shape=[args.batch,args.out_window,1])
anno_point = tf.placeholder(tf.float32, shape=[args.batch,2,args.out_window,1])
anno_bias = tf.placeholder(tf.float32, shape=[args.batch,2,args.out_window,1])
anno_mask = tf.placeholder(tf.float32, shape=[args.batch,args.out_window,1])
model = MultiDenseNet(args, model_input, anno_action, anno_point, anno_bias, anno_mask)
saver = tf.train.Saver(max_to_keep=args.epochs)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
record = {'test':{}, 'train':{}, 'epochs':args.epochs, 'record_name':[i for i in model.loss.keys()]}
with tf.Session(config=config) as sess:
init = tf.global_variables_initializer()
sess.run(init)
summary_writer = tf.summary.FileWriter(os.path.join(args.save_path, 'checkpoint/'), graph=sess.graph)
start_time = time.time()
for epoch in range(args.epochs):
print('-----------Epoch {} -------------'.format(epoch+1))
# training
if epoch < 10:
lr = [1e-3]
else:
lr = [1e-4]
DL = DataLoader(args=args, mode='validation', batch=args.batch, shuffle=True)
print('Start training, total batches in train set is: %d'%(DL.nbatch))
loss_name = [i for i in model.loss.keys()]
loss_tensor = [i for i in model.loss.values()]
loss_record = {i:[] for i in loss_name}
with tqdm (total=DL.nbatch) as count:
for step in range(DL.nbatch):
aa, pp, bb, ff, mm = DL.gen_train_batch(step)
values = sess.run(loss_tensor+[model.solver], feed_dict={model_input:ff, anno_action:aa, anno_point:pp, anno_bias:bb, anno_mask:mm, model.lr:lr})
for i, v in enumerate(values):
if i < len(loss_name):
loss_record[loss_name[i]].append(v)
count.update(1)
print('Training results:')
for name, value in loss_record.items():
loss_record[name] = np.mean(value)
print(name, np.mean(value))
record['train'][epoch+1] = loss_record
# testing and save
if (epoch+1) % 1 == 0:
DL = DataLoader(args=args, mode='test', batch=args.batch, shuffle=False)
print('Start testing, total batches in test set is: %d'%(DL.nbatch))
loss_name = [i for i in model.loss.keys()]
loss_tensor = [i for i in model.loss.values()]
heat_name = ['action_heat', 'start_heat', 'end_heat']
heat_tensor = [model.action_heat, model.start_heat, model.end_heat]
gt_name = ['gt_action', 'gt_start', 'gt_end']
gt_tensor = [model.gt_action, model.gt_point[:,0,:,:], model.gt_point[:,1,:,:]]
loss_record = {i:[] for i in loss_name}
heat_record = {i:[] for i in heat_name}
gt_record = {i:[] for i in gt_name}
with tqdm (total=DL.nbatch) as count:
for step in range(DL.nbatch):
aa, pp, bb, ff, mm = DL.gen_train_batch(step)
values = sess.run(loss_tensor+heat_tensor+gt_tensor, feed_dict={model_input:ff, anno_action:aa, anno_point:pp, anno_bias:bb, anno_mask:mm})
for i, v in enumerate(values):
if i < len(loss_name):
loss_record[loss_name[i]].append(v)
elif i < len(loss_name) + len(heat_name):
heat_record[heat_name[i-len(loss_name)]].append(v)
else:
gt_record[gt_name[i-len(loss_name)-len(heat_name)]].append(v)
count.update(1)
print('Testing results:')
for name, value in loss_record.items():
loss_record[name] = np.mean(value)
print(name, np.mean(value))
record['test'][epoch+1] = loss_record
checkpoint_path = os.path.join(args.save_path, 'checkpoint', 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=(epoch+1))
with open(os.path.join(args.save_path, 'checkpoint', 'record.pickle'), 'wb') as f:
pickle.dump(record, f)
def run_evaluating(args, epoch, mode):
with tf.Graph().as_default():
# build model
model_input = tf.placeholder(tf.float32, shape=[1,None,2048])
anno_action = tf.placeholder(tf.float32, shape=[1,None,1])
anno_point = tf.placeholder(tf.float32, shape=[1,2,None,1])
anno_bias = tf.placeholder(tf.float32, shape=[1,2,None,1])
anno_mask = tf.placeholder(tf.float32, shape=[1,None,1])
model = MultiDenseNet(args, model_input, anno_action, anno_point, anno_bias, anno_mask)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
saver.restore(sess, os.path.join(args.save_path, 'checkpoint', 'model.ckpt-{}'.format(epoch)))
DL = DataLoader(args=args, mode=mode, batch=1, shuffle=False)
results = {}
out_names = ['action_heat', 'start_heat', 'end_heat', 'gt_action', 'gt_start', 'gt_end', 'start_regr', 'end_regr']
out_tensors = [model.action_heat, model.start_heat, model.end_heat, model.gt_action, model.gt_point[:,0,:,:], model.gt_point[:,1,:,:], model.start_bias, model.end_bias]
with tqdm (total=DL.size) as count:
for video in range(DL.size):
key, aa, pp, bb, ff, mm = DL.gen_eval_batch(video)
values = sess.run(out_tensors, feed_dict={model_input:ff, anno_action:aa, anno_point:pp, anno_bias:bb, anno_mask:mm})
out_record = {}
for i, v in enumerate(values):
out_record[out_names[i]] = np.squeeze(v)
results[key] = out_record
count.update(1)
if not os.path.exists(os.path.join(args.save_path, 'predicts')):
os.makedirs(os.path.join(args.save_path, 'predicts'))
with open(os.path.join(args.save_path, 'predicts', '{}_results_epoch{}.pickle'.format(mode, epoch)), 'wb') as f:
pickle.dump(results, f)
if __name__ == '__main__':
args = parse_base_args()
# constraint GPU
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
# set log file
sys.stdout = Logger(os.path.join(args.save_path, 'log.log'))
# step1 train models
run_training(args)
# step2 evaluate models with different epoch
for epoch in range(1, args.epochs+1):
# test
run_evaluating(args, epoch, 'test')
# train
if epoch == 10:
run_evaluating(args, epoch, 'validation')
# step3 draw loss curve
draw_training_curve(args)
# step4 generate proposals
for epoch in range(1, args.epochs+1):
generate_proposals(args, epoch, 'test', prepare_pemdata=False)
# step5 evaluation
AR_AN = {}
aran = []
for epoch in range(1, args.epochs+1):
eval_file = os.path.join(args.save_path, 'proposals', 'results_softnms_n5_score_se_epoch{}.json'.format(epoch))
results = evaluation_proposal(args, eval_file)
AR_AN[epoch] = results
aran.append(results[1])
aran = np.array(aran)
mean_10 = np.mean(aran[10:,:],0)
with open(os.path.join(args.save_path, 'result_softnms_n5_score_se.txt'), 'w') as f:
for key, item in AR_AN.items():
s = '{}\t{}\t{}\t{}\t{}\t{}\n'.format(key,item[1][0],item[1][1],item[1][2],item[1][3],item[1][4])
f.write(s)
s = '{}\t{}\t{}\t{}\t{}\t{}\n'.format('averaged_last10',mean_10[0],mean_10[1],mean_10[2],mean_10[3],mean_10[4])
f.write(s)
# step6 plot actionness and proposals
# epoch = 20
# draw_actionness('test', epoch)
# draw_proposals('test', epoch)