forked from yjh0410/FreeYOLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
266 lines (224 loc) · 8.56 KB
/
engine.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
import torch
import torch.distributed as dist
import time
import os
import numpy as np
from utils import distributed_utils
def rescale_image_targets(images, targets, new_img_size):
"""
Deployed for Multi scale trick.
"""
# During training phase, the shape of input image is square.
old_img_size = images.shape[-1]
# interpolate
images = torch.nn.functional.interpolate(
input=images,
size=new_img_size,
mode='bilinear',
align_corners=False)
# rescale targets
for tgt in targets:
boxes = tgt["boxes"].clone()
boxes[:, [0, 2]] = boxes[:, [0, 2]] / old_img_size * new_img_size
boxes[:, [1, 3]] = boxes[:, [1, 3]] / old_img_size * new_img_size
tgt["boxes"] = boxes
return images, targets
def train_with_warmup(epoch,
total_epochs,
args,
device,
ema,
model,
criterion,
cfg,
dataloader,
optimizer,
warmup_scheduler,
scaler,
accumulate):
epoch_size = len(dataloader)
img_size = cfg['train_size']
t0 = time.time()
# train one epoch
for iter_i, (images, targets) in enumerate(dataloader):
ni = iter_i + epoch * epoch_size
# warmup
warmup_scheduler.warmup(ni, optimizer)
# to device
images = images.to(device)
# multi scale
# # choose a new image size
if ni % 10 == 0 and cfg['random_size']:
idx = np.random.randint(len(cfg['random_size']))
img_size = cfg['random_size'][idx]
# # rescale data with new image size
if cfg['random_size']:
images, targets = rescale_image_targets(images, targets, img_size)
# inference
with torch.cuda.amp.autocast(enabled=args.fp16):
outputs = model(images)
# loss
loss_dict = criterion(outputs=outputs, targets=targets)
losses = loss_dict['losses']
# reduce
loss_dict_reduced = distributed_utils.reduce_dict(loss_dict)
# check loss
if torch.isnan(losses):
print('loss is NAN !!')
continue
if args.distributed:
# gradient averaged between devices in DDP mode
losses *= distributed_utils.get_world_size()
# backward
scaler.scale(losses).backward()
# Optimize
if ni % accumulate == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
# ema
if ema:
ema.update(model)
# 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, total_epochs)
log += '[Iter: {}/{}]'.format(iter_i, epoch_size)
log += '[lr: {:.6f}]'.format(cur_lr[0])
# loss infor
for k in loss_dict_reduced.keys():
if k == 'losses' and args.distributed:
world_size = distributed_utils.get_world_size()
log += '[{}: {:.2f}]'.format(k, loss_dict[k] / world_size)
else:
log += '[{}: {:.2f}]'.format(k, loss_dict[k])
# other infor
log += '[time: {:.2f}]'.format(t1 - t0)
log += '[size: {}]'.format(img_size)
# print log infor
print(log, flush=True)
t0 = time.time()
def train_one_epoch(epoch,
total_epochs,
args,
device,
ema,
model,
criterion,
cfg,
dataloader,
optimizer,
scaler,
accumulate):
epoch_size = len(dataloader)
img_size = cfg["train_size"]
t0 = time.time()
# train one epoch
for iter_i, (images, targets) in enumerate(dataloader):
ni = iter_i + epoch * epoch_size
# to device
images = images.to(device)
# multi scale
# # choose a new image size
if ni % 10 == 0 and cfg['random_size']:
idx = np.random.randint(len(cfg['random_size']))
img_size = cfg['random_size'][idx]
# # rescale data with new image size
if cfg['random_size']:
images, targets = rescale_image_targets(images, targets, img_size)
# inference
with torch.cuda.amp.autocast(enabled=args.fp16):
outputs = model(images)
# loss
loss_dict = criterion(outputs=outputs, targets=targets)
losses = loss_dict['losses']
# reduce
loss_dict_reduced = distributed_utils.reduce_dict(loss_dict)
# check loss
if torch.isnan(losses):
print('loss is NAN !!')
continue
if args.distributed:
# gradient averaged between devices in DDP mode
losses *= distributed_utils.get_world_size()
# backward
scaler.scale(losses).backward()
# Optimize
if ni % accumulate == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
# ema
if ema:
ema.update(model)
# 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, total_epochs)
log += '[Iter: {}/{}]'.format(iter_i, epoch_size)
log += '[lr: {:.6f}]'.format(cur_lr[0])
# loss infor
for k in loss_dict_reduced.keys():
if k == 'losses' and args.distributed:
world_size = distributed_utils.get_world_size()
log += '[{}: {:.2f}]'.format(k, loss_dict[k] / world_size)
else:
log += '[{}: {:.2f}]'.format(k, loss_dict[k])
# other infor
log += '[time: {:.2f}]'.format(t1 - t0)
log += '[size: {}]'.format(img_size)
# print log infor
print(log, flush=True)
t0 = time.time()
def val_one_epoch(args,
model,
evaluator,
optimizer,
epoch,
best_map,
path_to_save):
# check evaluator
if distributed_utils.is_main_process():
if evaluator is None:
print('No evaluator ... save model and go on training.')
print('Saving state, epoch: {}'.format(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.state_dict(),
'mAP': -1.,
'optimizer': optimizer.state_dict(),
'epoch': epoch,
'args': args},
checkpoint_path)
else:
print('eval ...')
# set eval mode
model.trainable = False
model.eval()
# evaluate
evaluator.evaluate(model)
cur_map = evaluator.map
if cur_map > best_map:
# update best-map
best_map = cur_map
# save model
print('Saving state, epoch:', epoch + 1)
weight_name = '{}_epoch_{}_{:.2f}.pth'.format(args.version, epoch + 1, best_map*100)
checkpoint_path = os.path.join(path_to_save, weight_name)
torch.save({'model': model.state_dict(),
'mAP': round(best_map*100, 1),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
'args': args},
checkpoint_path)
# set train mode.
model.trainable = True
model.train()
if args.distributed:
# wait for all processes to synchronize
dist.barrier()
return best_map