-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.py
executable file
·295 lines (238 loc) · 12.7 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
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
import sys
sys.dont_write_bytecode = True
import os
import json
import torch
import torchvision
import torch.nn.parallel
import torch.optim as optim
import numpy as np
from tensorboardX import SummaryWriter
import opts
from dataset import VideoDataSet,ProposalDataSet
from models import TEM,PEM
from loss_function import TEM_loss_function,PEM_loss_function
import pandas as pd
from pgm import PGM_proposal_generation,PGM_feature_generation
from post_processing import BSN_post_processing
from eval import evaluation_proposal
def train_TEM(data_loader,model,optimizer,epoch,writer,opt):
model.train()
epoch_action_loss = 0
epoch_start_loss = 0
epoch_end_loss = 0
epoch_cost = 0
for n_iter,(input_data,label_action,label_start,label_end) in enumerate(data_loader):
TEM_output = model(input_data)
loss = TEM_loss_function(label_action,label_start,label_end,TEM_output,opt)
cost = loss["cost"]
optimizer.zero_grad()
cost.backward()
optimizer.step()
epoch_action_loss += loss["loss_action"].cpu().detach().numpy()
epoch_start_loss += loss["loss_start"].cpu().detach().numpy()
epoch_end_loss += loss["loss_end"].cpu().detach().numpy()
epoch_cost += loss["cost"].cpu().detach().numpy()
writer.add_scalars('data/action', {'train': epoch_action_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/start', {'train': epoch_start_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/end', {'train': epoch_end_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/cost', {'train': epoch_cost/(n_iter+1)}, epoch)
print "TEM training loss(epoch %d): action - %.03f, start - %.03f, end - %.03f" %(epoch,epoch_action_loss/(n_iter+1),
epoch_start_loss/(n_iter+1),
epoch_end_loss/(n_iter+1))
def test_TEM(data_loader,model,epoch,writer,opt):
model.eval()
epoch_action_loss = 0
epoch_start_loss = 0
epoch_end_loss = 0
epoch_cost = 0
for n_iter,(input_data,label_action,label_start,label_end) in enumerate(data_loader):
TEM_output = model(input_data)
loss = TEM_loss_function(label_action,label_start,label_end,TEM_output,opt)
epoch_action_loss += loss["loss_action"].cpu().detach().numpy()
epoch_start_loss += loss["loss_start"].cpu().detach().numpy()
epoch_end_loss += loss["loss_end"].cpu().detach().numpy()
epoch_cost += loss["cost"].cpu().detach().numpy()
writer.add_scalars('data/action', {'test': epoch_action_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/start', {'test': epoch_start_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/end', {'test': epoch_end_loss/(n_iter+1)}, epoch)
writer.add_scalars('data/cost', {'test': epoch_cost/(n_iter+1)}, epoch)
print "TEM testing loss(epoch %d): action - %.03f, start - %.03f, end - %.03f" %(epoch,epoch_action_loss/(n_iter+1),
epoch_start_loss/(n_iter+1),
epoch_end_loss/(n_iter+1))
state = {'epoch': epoch + 1,
'state_dict': model.state_dict()}
torch.save(state, opt["checkpoint_path"]+"/tem_checkpoint.pth.tar" )
if epoch_cost< model.module.tem_best_loss:
model.module.tem_best_loss = np.mean(epoch_cost)
torch.save(state, opt["checkpoint_path"]+"/tem_best.pth.tar" )
def train_PEM(data_loader,model,optimizer,epoch,writer,opt):
model.train()
epoch_iou_loss = 0
for n_iter,(input_data,label_iou) in enumerate(data_loader):
PEM_output = model(input_data)
iou_loss = PEM_loss_function(PEM_output,label_iou,model,opt)
optimizer.zero_grad()
iou_loss.backward()
optimizer.step()
epoch_iou_loss += iou_loss.cpu().detach().numpy()
writer.add_scalars('data/iou_loss', {'train': epoch_iou_loss/(n_iter+1)}, epoch)
print "PEM training loss(epoch %d): iou - %.04f" %(epoch,epoch_iou_loss/(n_iter+1))
def test_PEM(data_loader,model,epoch,writer,opt):
model.eval()
epoch_iou_loss = 0
for n_iter,(input_data,label_iou) in enumerate(data_loader):
PEM_output = model(input_data)
iou_loss = PEM_loss_function(PEM_output,label_iou,model,opt)
epoch_iou_loss += iou_loss.cpu().detach().numpy()
writer.add_scalars('data/iou_loss', {'validation': epoch_iou_loss/(n_iter+1)}, epoch)
print "PEM testing loss(epoch %d): iou - %.04f" %(epoch,epoch_iou_loss/(n_iter+1))
state = {'epoch': epoch + 1,
'state_dict': model.state_dict()}
torch.save(state, opt["checkpoint_path"]+"/pem_checkpoint.pth.tar" )
if epoch_iou_loss<model.module.pem_best_loss :
model.module.pem_best_loss = np.mean(epoch_iou_loss)
torch.save(state, opt["checkpoint_path"]+"/pem_best.pth.tar" )
def BSN_Train_TEM(opt):
writer = SummaryWriter()
model = TEM(opt)
model = torch.nn.DataParallel(model, device_ids=[0]).cuda()
optimizer = optim.Adam(model.parameters(),lr=opt["tem_training_lr"],weight_decay = opt["tem_weight_decay"])
train_loader = torch.utils.data.DataLoader(VideoDataSet(opt,subset="train"),
batch_size=model.module.batch_size, shuffle=True,
num_workers=8, pin_memory=True,drop_last=True)
test_loader = torch.utils.data.DataLoader(VideoDataSet(opt,subset="validation"),
batch_size=model.module.batch_size, shuffle=False,
num_workers=8, pin_memory=True,drop_last=True)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size = opt["tem_step_size"], gamma = opt["tem_step_gamma"])
for epoch in range(opt["tem_epoch"]):
scheduler.step()
train_TEM(train_loader,model,optimizer,epoch,writer,opt)
test_TEM(test_loader,model,epoch,writer,opt)
writer.close()
def BSN_Train_PEM(opt):
writer = SummaryWriter()
model = PEM(opt)
model = torch.nn.DataParallel(model, device_ids=[0]).cuda()
optimizer = optim.Adam(model.parameters(),lr=opt["pem_training_lr"],weight_decay = opt["pem_weight_decay"])
def collate_fn(batch):
batch_data = torch.cat([x[0] for x in batch])
batch_iou = torch.cat([x[1] for x in batch])
return batch_data,batch_iou
train_loader = torch.utils.data.DataLoader(ProposalDataSet(opt,subset="train"),
batch_size=model.module.batch_size, shuffle=True,
num_workers=8, pin_memory=True,drop_last=True,collate_fn=collate_fn)
test_loader = torch.utils.data.DataLoader(ProposalDataSet(opt,subset="validation"),
batch_size=model.module.batch_size, shuffle=True,
num_workers=8, pin_memory=True,drop_last=True,collate_fn=collate_fn)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size = opt["pem_step_size"], gamma = opt["pem_step_gamma"])
for epoch in range(opt["pem_epoch"]):
scheduler.step()
train_PEM(train_loader,model,optimizer,epoch,writer,opt)
test_PEM(test_loader,model,epoch,writer,opt)
writer.close()
def BSN_inference_TEM(opt):
model = TEM(opt)
checkpoint = torch.load(opt["checkpoint_path"]+"/tem_best.pth.tar")
base_dict = {'.'.join(k.split('.')[1:]): v for k,v in list(checkpoint['state_dict'].items())}
model.load_state_dict(base_dict)
model = torch.nn.DataParallel(model, device_ids=[0]).cuda()
model.eval()
test_loader = torch.utils.data.DataLoader(VideoDataSet(opt,subset="full"),
batch_size=model.module.batch_size, shuffle=False,
num_workers=8, pin_memory=True,drop_last=False)
columns=["action","start","end","xmin","xmax"]
for index_list,input_data,anchor_xmin,anchor_xmax in test_loader:
TEM_output = model(input_data).detach().cpu().numpy()
batch_action = TEM_output[:,0,:]
batch_start = TEM_output[:,1,:]
batch_end = TEM_output[:,2,:]
index_list = index_list.numpy()
anchor_xmin = np.array([x.numpy()[0] for x in anchor_xmin])
anchor_xmax = np.array([x.numpy()[0] for x in anchor_xmax])
for batch_idx,full_idx in enumerate(index_list):
video = test_loader.dataset.video_list[full_idx]
video_action = batch_action[batch_idx]
video_start = batch_start[batch_idx]
video_end = batch_end[batch_idx]
video_result = np.stack((video_action,video_start,video_end,anchor_xmin,anchor_xmax),axis=1)
video_df = pd.DataFrame(video_result,columns=columns)
video_df.to_csv("./output/TEM_results/"+video+".csv",index=False)
def BSN_inference_PEM(opt):
model = PEM(opt)
checkpoint = torch.load(opt["checkpoint_path"]+"/pem_best.pth.tar")
base_dict = {'.'.join(k.split('.')[1:]): v for k,v in list(checkpoint['state_dict'].items())}
model.load_state_dict(base_dict)
model = torch.nn.DataParallel(model, device_ids=[0]).cuda()
model.eval()
test_loader = torch.utils.data.DataLoader(ProposalDataSet(opt,subset=opt["pem_inference_subset"]),
batch_size=model.module.batch_size, shuffle=False,
num_workers=8, pin_memory=True,drop_last=False)
for idx,(video_feature,video_xmin,video_xmax,video_xmin_score,video_xmax_score) in enumerate(test_loader):
video_name = test_loader.dataset.video_list[idx]
video_conf = model(video_feature).view(-1).detach().cpu().numpy()
video_xmin = video_xmin.view(-1).cpu().numpy()
video_xmax = video_xmax.view(-1).cpu().numpy()
video_xmin_score = video_xmin_score.view(-1).cpu().numpy()
video_xmax_score = video_xmax_score.view(-1).cpu().numpy()
df=pd.DataFrame()
df["xmin"]=video_xmin
df["xmax"]=video_xmax
df["xmin_score"]=video_xmin_score
df["xmax_score"]=video_xmax_score
df["iou_score"]=video_conf
df.to_csv("./output/PEM_results/"+video_name+".csv",index=False)
def main(opt):
if opt["module"] == "TEM":
if opt["mode"] == "train":
print "TEM training start"
BSN_Train_TEM(opt)
print "TEM training finished"
elif opt["mode"] == "inference":
print "TEM inference start"
if not os.path.exists("output/TEM_results"):
os.makedirs("output/TEM_results")
BSN_inference_TEM(opt)
print "TEM inference finished"
else:
print "Wrong mode. TEM has two modes: train and inference"
elif opt["module"] == "PGM":
if not os.path.exists("output/PGM_proposals"):
os.makedirs("output/PGM_proposals")
print "PGM: start generate proposals"
PGM_proposal_generation(opt)
print "PGM: finish generate proposals"
if not os.path.exists("output/PGM_feature"):
os.makedirs("output/PGM_feature")
print "PGM: start generate BSP feature"
PGM_feature_generation(opt)
print "PGM: finish generate BSP feature"
elif opt["module"] == "PEM":
if opt["mode"] == "train":
print "PEM training start"
BSN_Train_PEM(opt)
print "PEM training finished"
elif opt["mode"] == "inference":
if not os.path.exists("output/PEM_results"):
os.makedirs("output/PEM_results")
print "PEM inference start"
BSN_inference_PEM(opt)
print "PEM inference finished"
else:
print "Wrong mode. PEM has two modes: train and inference"
elif opt["module"] == "Post_processing":
print "Post processing start"
BSN_post_processing(opt)
print "Post processing finished"
elif opt["module"] == "Evaluation":
evaluation_proposal(opt)
print ""
if __name__ == '__main__':
opt = opts.parse_opt()
opt = vars(opt)
if not os.path.exists(opt["checkpoint_path"]):
os.makedirs(opt["checkpoint_path"])
opt_file=open(opt["checkpoint_path"]+"/opts.json","w")
json.dump(opt,opt_file)
opt_file.close()
main(opt)