-
Notifications
You must be signed in to change notification settings - Fork 24
/
train.py
318 lines (256 loc) · 10.5 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import cv2
cv2.setNumThreads(0)
cv2.ocl.setUseOpenCL(False)
import os
import random
import time
import argparse
import numpy as np
from copy import deepcopy
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.cuda.amp as amp
from utils import distributed_utils
from utils.com_flops_params import FLOPs_and_Params
from utils.misc import CollateFunc, build_dataset, build_dataloader
from utils.solver.optimizer import build_optimizer
from utils.solver.warmup_schedule import build_warmup
from config import build_dataset_config, build_model_config
from models.detector import build_model
GLOBAL_SEED = 42
def parse_args():
parser = argparse.ArgumentParser(description='YOWO')
# CUDA
parser.add_argument('--cuda', action='store_true', default=False,
help='use cuda.')
# Visualization
parser.add_argument('--tfboard', action='store_true', default=False,
help='use tensorboard')
parser.add_argument('--save_folder', default='./weights/', type=str,
help='path to save weight')
parser.add_argument('--vis_data', action='store_true', default=False,
help='use tensorboard')
# Mix precision training
parser.add_argument('--fp16', dest="fp16", action="store_true", default=False,
help="Adopting mix precision training.")
# Evaluation
parser.add_argument('--eval', action='store_true', default=False,
help='do evaluation during training.')
parser.add_argument('--eval_epoch', default=1, type=int,
help='after eval epoch, the model is evaluated on val dataset.')
parser.add_argument('--save_dir', default='inference_results/',
type=str, help='save inference results.')
# Model
parser.add_argument('-v', '--version', default='yowo', type=str, choices=['yowo', 'yowo_nano'],
help='build YOWO')
parser.add_argument('--topk', default=40, type=int,
help='topk candidates for evaluation')
parser.add_argument('-p', '--coco_pretrained', default=None, type=str,
help='coco pretrained weight')
parser.add_argument('-r', '--resume', default=None, type=str,
help='keep training')
parser.add_argument('--ema', dest="ema", action="store_true", default=False,
help="use model EMA.")
# Dataset
parser.add_argument('-d', '--dataset', default='ucf24',
help='ucf24, jhmdb')
parser.add_argument('--num_workers', default=4, type=int,
help='Number of workers used in dataloading')
# DDP train
parser.add_argument('-dist', '--distributed', action='store_true', default=False,
help='distributed training')
parser.add_argument('--dist_url', default='env://',
help='url used to set up distributed training')
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--sybn', action='store_true', default=False,
help='use sybn.')
return parser.parse_args()
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
np.random.seed(seed)
def worker_init_fn(dump):
set_seed(GLOBAL_SEED)
def train():
args = parse_args()
print("Setting Arguments.. : ", args)
print("----------------------------------------------------------")
# dist
print('World size: {}'.format(distributed_utils.get_world_size()))
if args.distributed:
distributed_utils.init_distributed_mode(args)
print("git:\n {}\n".format(distributed_utils.get_sha()))
# path to save model
path_to_save = os.path.join(args.save_folder, args.dataset, args.version)
os.makedirs(path_to_save, exist_ok=True)
# cuda
if args.cuda:
print('use cuda')
cudnn.benchmark = True
device = torch.device("cuda")
else:
device = torch.device("cpu")
# amp
scaler = amp.GradScaler(enabled=args.fp16)
# config
d_cfg = build_dataset_config(args)
m_cfg = build_model_config(args)
# dataset and evaluator
dataset, evaluator, num_classes = build_dataset(d_cfg, args, is_train=True)
# dataloader
batch_size = d_cfg['batch_size'] * distributed_utils.get_world_size()
dataloader = build_dataloader(args, dataset, batch_size, CollateFunc(), is_train=True)
# build model
net = build_model(args=args,
d_cfg=d_cfg,
m_cfg=m_cfg,
device=device,
num_classes=num_classes,
trainable=True,
resume=args.resume)
model = net
model = model.to(device).train()
# SyncBatchNorm
if args.sybn and args.distributed:
print('use SyncBatchNorm ...')
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
# DDP
model_without_ddp = model
if args.distributed:
model = DDP(model, device_ids=[args.gpu])
model_without_ddp = model.module
# Compute FLOPs and Params
if distributed_utils.is_main_process():
model_copy = deepcopy(model_without_ddp)
FLOPs_and_Params(
model=model_copy,
img_size=d_cfg['test_size'],
len_clip=d_cfg['len_clip'],
device=device)
del model_copy
# optimizer
base_lr = d_cfg['base_lr']
optimizer, start_epoch = build_optimizer(
model=model_without_ddp,
base_lr=base_lr,
name=d_cfg['optimizer'],
momentum=d_cfg['momentum'],
weight_decay=d_cfg['weight_decay'],
resume=args.resume
)
# lr scheduler
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer=optimizer,
milestones=d_cfg['lr_epoch'],
gamma=d_cfg['lr_decay_ratio']
)
# warmup scheduler
warmup_scheduler = build_warmup(
name=d_cfg['warmup'],
base_lr=base_lr,
wp_iter=d_cfg['wp_iter'],
warmup_factor=d_cfg['warmup_factor']
)
# training configuration
max_epoch = d_cfg['max_epoch']
epoch_size = len(dataloader)
warmup = True
t0 = time.time()
for epoch in range(start_epoch, max_epoch):
if args.distributed:
dataloader.batch_sampler.sampler.set_epoch(epoch)
# train one epoch
for iter_i, (frame_ids, video_clips, targets) in enumerate(dataloader):
ni = iter_i + epoch * epoch_size
# warmup
if ni < d_cfg['wp_iter'] and warmup:
warmup_scheduler.warmup(ni, optimizer)
elif ni == d_cfg['wp_iter'] and warmup:
# warmup is over
print('Warmup is over')
warmup = False
warmup_scheduler.set_lr(optimizer, lr=base_lr, base_lr=base_lr)
# to device
video_clips = video_clips.to(device)
# inference
if args.fp16:
with torch.cuda.amp.autocast(enabled=args.fp16):
loss_dict = model(video_clips, targets=targets)
else:
loss_dict = model(video_clips, targets=targets)
losses = loss_dict['losses']
losses = losses / d_cfg['accumulate']
# reduce
loss_dict_reduced = distributed_utils.reduce_dict(loss_dict)
# check loss
if torch.isnan(losses):
print('loss is NAN !!')
continue
# Backward and Optimize
if args.fp16:
scaler.scale(losses).backward()
# Optimize
if ni % d_cfg['accumulate'] == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
else:
# Backward
losses.backward()
# Optimize
if ni % d_cfg['accumulate'] == 0:
optimizer.step()
optimizer.zero_grad()
# Display
if distributed_utils.is_main_process() and iter_i % 10 == 0:
t1 = time.time()
cur_lr = [param_group['lr'] for param_group in optimizer.param_groups]
# basic infor
log = '[Epoch: {}/{}]'.format(epoch+1, max_epoch)
log += '[Iter: {}/{}]'.format(iter_i, epoch_size)
log += '[lr: {:.6f}]'.format(cur_lr[0])
# loss infor
for k in loss_dict_reduced.keys():
log += '[{}: {:.2f}]'.format(k, loss_dict[k])
# other infor
log += '[time: {:.2f}]'.format(t1 - t0)
log += '[size: {}]'.format(d_cfg['train_size'])
# print log infor
print(log, flush=True)
t0 = time.time()
lr_scheduler.step()
# evaluation
if epoch % args.eval_epoch == 0 or (epoch + 1) == max_epoch:
# check evaluator
model_eval = model_without_ddp
if distributed_utils.is_main_process():
if evaluator is None:
print('No evaluator ... save model and go on training.')
else:
print('eval ...')
# set eval mode
model_eval.trainable = False
model_eval.eval()
# evaluate
evaluator.evaluate_frame_map(model_eval, epoch + 1)
# set train mode.
model_eval.trainable = True
model_eval.train()
# save model
print('Saving state, epoch:', epoch + 1)
weight_name = '{}_epoch_{}.pth'.format(args.version, epoch+1)
checkpoint_path = os.path.join(path_to_save, weight_name)
torch.save({'model': model_eval.state_dict(),
'epoch': epoch,
'args': args},
checkpoint_path)
if args.distributed:
# wait for all processes to synchronize
dist.barrier()
if __name__ == '__main__':
train()