-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
296 lines (251 loc) · 12.4 KB
/
train.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import _init_paths
import os
import pprint
import argparse
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
import torch.optim as optim
from tqdm import tqdm
import datasets
import models
from core.config import config, update_config
from core.engine import Engine
from core.utils import AverageMeter
from core import eval
from core.utils import create_logger
import models.loss as loss
import math
torch.manual_seed(0)
torch.cuda.manual_seed(0)
torch.autograd.set_detect_anomaly(True)
def parse_args():
parser = argparse.ArgumentParser(description='Train localization network')
# general
parser.add_argument('--cfg', help='experiment configure file name', required=True, type=str)
args, rest = parser.parse_known_args()
# update config
update_config(args.cfg)
# training
parser.add_argument('--gpus', help='gpus', type=str)
parser.add_argument('--workers', help='num of dataloader workers', type=int)
parser.add_argument('--dataDir', help='data path', type=str)
parser.add_argument('--modelDir', help='model path', type=str)
parser.add_argument('--logDir', help='log path', type=str)
parser.add_argument('--verbose', default=False, action="store_true", help='print progress bar')
parser.add_argument('--tag', help='tags shown in log', type=str)
args = parser.parse_args()
return args
def reset_config(config, args):
if args.gpus:
config.GPUS = args.gpus
if args.workers:
config.WORKERS = args.workers
if args.dataDir:
config.DATA_DIR = args.dataDir
if args.modelDir:
config.MODEL_DIR = args.modelDir
if args.logDir:
config.LOG_DIR = args.logDir
if args.verbose:
config.VERBOSE = args.verbose
if args.tag:
config.TAG = args.tag
if __name__ == '__main__':
args = parse_args()
reset_config(config, args)
logger, final_output_dir = create_logger(config, args.cfg, config.TAG)
logger.info('\n'+pprint.pformat(args))
logger.info('\n'+pprint.pformat(config))
# cudnn related setting
cudnn.benchmark = config.CUDNN.BENCHMARK
torch.backends.cudnn.deterministic = config.CUDNN.DETERMINISTIC
torch.backends.cudnn.enabled = config.CUDNN.ENABLED
dataset_name = config.DATASET.NAME
model_name = config.MODEL.NAME
train_dataset = getattr(datasets, dataset_name)('train')
if config.TEST.EVAL_TRAIN:
eval_train_dataset = getattr(datasets, dataset_name)('train')
if not config.DATASET.NO_VAL:
val_dataset = getattr(datasets, dataset_name)('val')
test_dataset = getattr(datasets, dataset_name)('test')
model = getattr(models, model_name)()
print("model have {} paramerters in total".format(sum(x.numel() for x in model.parameters())))
if config.MODEL.CHECKPOINT and config.TRAIN.CONTINUE:
model_checkpoint = torch.load(config.MODEL.CHECKPOINT)
model.load_state_dict(model_checkpoint)
if torch.cuda.device_count() > 1:
print("Using", torch.cuda.device_count(), "GPUs")
model = torch.nn.DataParallel(model)
device = ("cuda" if torch.cuda.is_available() else "cpu" )
model = model.to(device)
optimizer = optim.Adam(model.parameters(),lr=config.TRAIN.LR, betas=(0.9, 0.999), weight_decay=config.TRAIN.WEIGHT_DECAY)
# optimizer = optim.SGD(model.parameters(), lr=config.TRAIN.LR, momentum=0.9, weight_decay=config.TRAIN.WEIGHT_DECAY)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=config.TRAIN.FACTOR, patience=config.TRAIN.PATIENCE, verbose=config.VERBOSE)
def iterator(split):
if split == 'train':
dataloader = DataLoader(train_dataset,
batch_size=config.TRAIN.BATCH_SIZE,
shuffle=config.TRAIN.SHUFFLE,
num_workers=config.WORKERS,
pin_memory=False,
collate_fn=datasets.collate_fn)
elif split == 'val':
dataloader = DataLoader(val_dataset,
batch_size=config.TEST.BATCH_SIZE,
shuffle=False,
num_workers=config.WORKERS,
pin_memory=False,
collate_fn=datasets.collate_fn)
elif split == 'test':
dataloader = DataLoader(test_dataset,
batch_size=config.TEST.BATCH_SIZE,
shuffle=False,
num_workers=config.WORKERS,
pin_memory=False,
collate_fn=datasets.collate_fn)
elif split == 'train_no_shuffle':
dataloader = DataLoader(eval_train_dataset,
batch_size=config.TEST.BATCH_SIZE,
shuffle=False,
num_workers=config.WORKERS,
pin_memory=False,
collate_fn=datasets.collate_fn)
else:
raise NotImplementedError
return dataloader
def network(sample):
anno_idxs = sample['batch_anno_idxs']
textual_input = sample['batch_word_vectors'].cuda()
textual_mask = sample['batch_txt_mask'].cuda()
visual_input = sample['batch_vis_input'].cuda()
map_gt = sample['batch_map_gt'].cuda()
duration = sample['batch_duration']
sent_input = sample['batch_sent_vectors'].cuda()
prediction, map_mask = model(textual_input, textual_mask, visual_input, sent_input)
loss_value, joint_prob = getattr(loss, config.LOSS.NAME)(prediction, map_mask, map_gt, config.LOSS.PARAMS)
sorted_times = None if model.training else get_proposal_results(joint_prob, duration)
return loss_value, sorted_times
def get_proposal_results(scores, durations):
# assume all valid scores are larger than one
out_sorted_times = []
for score, duration in zip(scores, durations):
T = score.shape[-1]
sorted_indexs = np.dstack(np.unravel_index(np.argsort(score.cpu().detach().numpy().ravel())[::-1], (T, T))).tolist()
#origin -
sorted_indexs = np.array([item for item in sorted_indexs[0] if item[0] <= item[1]]).astype(float)
# #reverse -
# sorted_indexs = np.array([item if item[0] <= item[1] else [item[1],item[0]] for item in sorted_indexs[0]]).astype(float)
sorted_indexs[:,1] = sorted_indexs[:,1] + 1
sorted_indexs = torch.from_numpy(sorted_indexs).cuda()
target_size = config.DATASET.NUM_SAMPLE_CLIPS // config.DATASET.TARGET_STRIDE
out_sorted_times.append((sorted_indexs.float() / target_size * duration).tolist())
return out_sorted_times
def on_start(state):
state['loss_meter'] = AverageMeter()
state['test_interval'] = int(len(train_dataset)/config.TRAIN.BATCH_SIZE*config.TEST.INTERVAL)
state['t'] = 1
model.train()
if config.VERBOSE:
state['progress_bar'] = tqdm(total=state['test_interval'])
def on_forward(state):
torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
state['loss_meter'].update(state['loss'].item(), 1)
def on_update(state):# Save All
if config.VERBOSE:
state['progress_bar'].update(1)
if state['t'] % state['test_interval'] == 0:
model.eval()
if config.VERBOSE:
state['progress_bar'].close()
loss_message = '\niter: {} train loss {:.4f}'.format(state['t'], state['loss_meter'].avg)
table_message = ''
if config.TEST.EVAL_TRAIN:
train_state = engine.test(network, iterator('train_no_shuffle'), 'train')
train_table = eval.display_results(train_state['Rank@N,mIoU@M'], train_state['miou'],
'performance on training set')
table_message += '\n'+ train_table
if not config.DATASET.NO_VAL:
val_state = engine.test(network, iterator('val'), 'val')
state['scheduler'].step(-val_state['loss_meter'].avg)
loss_message += ' val loss {:.4f}'.format(val_state['loss_meter'].avg)
val_state['loss_meter'].reset()
val_table = eval.display_results(val_state['Rank@N,mIoU@M'], val_state['miou'],
'performance on validation set')
table_message += '\n'+ val_table
test_state = engine.test(network, iterator('test'), 'test')
loss_message += ' test loss {:.4f}'.format(test_state['loss_meter'].avg)
test_state['loss_meter'].reset()
test_table = eval.display_results(test_state['Rank@N,mIoU@M'], test_state['miou'],
'performance on testing set')
table_message += '\n' + test_table
message = loss_message+table_message+'\n'
logger.info(message)
saved_model_filename = os.path.join(config.MODEL_DIR,'{}/{}/iter{:06d}-{:.4f}-{:.4f}.pkl'.format(
dataset_name, model_name+'_'+config.DATASET.VIS_INPUT_TYPE,
state['t'], test_state['Rank@N,mIoU@M'][0,0], test_state['Rank@N,mIoU@M'][0,1]))
rootfolder1 = os.path.dirname(saved_model_filename)
rootfolder2 = os.path.dirname(rootfolder1)
rootfolder3 = os.path.dirname(rootfolder2)
if not os.path.exists(rootfolder3):
print('Make directory %s ...' % rootfolder3)
os.mkdir(rootfolder3)
if not os.path.exists(rootfolder2):
print('Make directory %s ...' % rootfolder2)
os.mkdir(rootfolder2)
if not os.path.exists(rootfolder1):
print('Make directory %s ...' % rootfolder1)
os.mkdir(rootfolder1)
if torch.cuda.device_count() > 1:
torch.save(model.module.state_dict(), saved_model_filename)
else:
torch.save(model.state_dict(), saved_model_filename)
if config.VERBOSE:
state['progress_bar'] = tqdm(total=state['test_interval'])
model.train()
state['loss_meter'].reset()
def on_end(state):
if config.VERBOSE:
state['progress_bar'].close()
def on_test_start(state):
state['loss_meter'] = AverageMeter()
state['sorted_segments_list'] = []
if config.VERBOSE:
if state['split'] == 'train':
state['progress_bar'] = tqdm(total=math.ceil(len(train_dataset)/config.TEST.BATCH_SIZE))
elif state['split'] == 'val':
state['progress_bar'] = tqdm(total=math.ceil(len(val_dataset)/config.TEST.BATCH_SIZE))
elif state['split'] == 'test':
state['progress_bar'] = tqdm(total=math.ceil(len(test_dataset)/config.TEST.BATCH_SIZE))
else:
raise NotImplementedError
def on_test_forward(state):
if config.VERBOSE:
state['progress_bar'].update(1)
state['loss_meter'].update(state['loss'].item(), 1)
min_idx = min(state['sample']['batch_anno_idxs'])
batch_indexs = [idx - min_idx for idx in state['sample']['batch_anno_idxs']]
sorted_segments = [state['output'][i] for i in batch_indexs]
state['sorted_segments_list'].extend(sorted_segments)
def on_test_end(state):
annotations = state['iterator'].dataset.annotations
state['Rank@N,mIoU@M'], state['miou'] = eval.eval_predictions(state['sorted_segments_list'], annotations, verbose=False)
if config.VERBOSE:
state['progress_bar'].close()
engine = Engine()
engine.hooks['on_start'] = on_start
engine.hooks['on_forward'] = on_forward
engine.hooks['on_update'] = on_update
engine.hooks['on_end'] = on_end
engine.hooks['on_test_start'] = on_test_start
engine.hooks['on_test_forward'] = on_test_forward
engine.hooks['on_test_end'] = on_test_end
engine.train(network,
iterator('train'),
maxepoch=config.TRAIN.MAX_EPOCH,
optimizer=optimizer,
scheduler=scheduler)