-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_asl_reproduce.py
314 lines (259 loc) · 11.8 KB
/
train_asl_reproduce.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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 1 09:07:31 2021
@author: 31049
"""
import argparse
import random
import time
from copy import deepcopy
import torch
import torch.nn.parallel
import torch.optim
import torch.utils.data.distributed
import torchvision.transforms as transforms
import os
from torch.optim import lr_scheduler, Adam
from src.helper_functions.helper_functions import mAP, AverageMeter, CocoDetection
from src.models import create_model
from src.loss_functions.losses import AsymmetricLoss
import numpy as np
from randaugment import RandAugment
from PIL import ImageDraw
from torch.cuda.amp import GradScaler, autocast
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('--lr', default=2e-4, type=float)
parser.add_argument('--model-name', default='tresnet_m')
parser.add_argument('--model-path', default='./tresnet_m.pth', type=str)
parser.add_argument('--num-classes', default=80)
parser.add_argument('-j', '--workers', default=8, type=int, metavar='N',
help='number of data loading workers (default: 16)')
parser.add_argument('--image-size', default=224, type=int,
metavar='N', help='input image size (default: 448)')
parser.add_argument('--thre', default=0.8, type=float,
metavar='N', help='threshold value')
parser.add_argument('-b', '--batch-size', default=128, type=int,
metavar='N', help='mini-batch size (default: 16)')
parser.add_argument('--print-freq', '-p', default=64, type=int,
metavar='N', help='print frequency (default: 64)')
class AsymmetricLoss(torch.nn.Module):
def __init__(self, gamma_neg=4, gamma_pos=0, clip=0.05, eps=1e-8, disable_torch_grad_focal_loss=False):
super(AsymmetricLoss, self).__init__()
self.gamma_neg = gamma_neg
self.gamma_pos = gamma_pos
self.clip = clip
self.disable_torch_grad_focal_loss = disable_torch_grad_focal_loss
self.eps = eps
def forward(self, x, y):
""""
Parameters
----------
x: input logits
y: targets (multi-label binarized vector)
"""
# Calculating Probabilities
x_sigmoid = torch.sigmoid(x)
xs_pos = x_sigmoid
xs_neg = 1 - x_sigmoid
# Asymmetric Clipping
if self.clip is not None and self.clip > 0:
xs_neg = (xs_neg + self.clip).clamp(max=1)
# Basic CE calculation
los_pos = y * torch.log(xs_pos.clamp(min=self.eps))
los_neg = (1 - y) * torch.log(xs_neg.clamp(min=self.eps))
loss = los_pos + los_neg
# Asymmetric Focusing
if self.gamma_neg > 0 or self.gamma_pos > 0:
if self.disable_torch_grad_focal_loss:
torch.set_grad_enabled(False)
pt0 = xs_pos * y
pt1 = xs_neg * (1 - y) # pt = p if t > 0 else 1-p
pt = pt0 + pt1
one_sided_gamma = self.gamma_pos * y + self.gamma_neg * (1 - y)
one_sided_w = torch.pow(1 - pt, one_sided_gamma)
if self.disable_torch_grad_focal_loss:
torch.set_grad_enabled(True)
loss *= one_sided_w
return -loss.sum()
class ModelEma(torch.nn.Module):
def __init__(self, model, decay=0.9997, device=None):
super(ModelEma, self).__init__()
# make a copy of the model for accumulating moving average of weights
self.module = deepcopy(model)
self.module.eval()
self.decay = decay
self.device = device # perform ema on different device from model if set
if self.device is not None:
self.module.to(device=device)
def _update(self, model, update_fn):
with torch.no_grad():
for ema_v, model_v in zip(self.module.state_dict().values(), model.state_dict().values()):
if self.device is not None:
model_v = model_v.to(device=self.device)
ema_v.copy_(update_fn(ema_v, model_v))
def update(self, model):
self._update(model, update_fn=lambda e, m: self.decay * e + (1. - self.decay) * m)
def set(self, model):
self._update(model, update_fn=lambda e, m: m)
class CutoutPIL(object):
def __init__(self, cutout_factor=0.5):
self.cutout_factor = cutout_factor
def __call__(self, x):
img_draw = ImageDraw.Draw(x)
h, w = x.size[0], x.size[1] # HWC
h_cutout = int(self.cutout_factor * h + 0.5)
w_cutout = int(self.cutout_factor * w + 0.5)
y_c = np.random.randint(h)
x_c = np.random.randint(w)
y1 = np.clip(y_c - h_cutout // 2, 0, h)
y2 = np.clip(y_c + h_cutout // 2, 0, h)
x1 = np.clip(x_c - w_cutout // 2, 0, w)
x2 = np.clip(x_c + w_cutout // 2, 0, w)
fill_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
img_draw.rectangle([x1, y1, x2, y2], fill=fill_color)
return x
def add_weight_decay(model, weight_decay=1e-4, skip_list=()):
decay = []
no_decay = []
for name, param in model.named_parameters():
if not param.requires_grad:
continue # frozen weights
if len(param.shape) == 1 or name.endswith(".bias") or name in skip_list:
no_decay.append(param)
else:
decay.append(param)
return [
{'params': no_decay, 'weight_decay': 0.},
{'params': decay, 'weight_decay': weight_decay}]
def main():
args = parser.parse_args()
args.batch_size = args.batch_size
args.do_bottleneck_head = False
# setup model
print('creating model...')
model = create_model(args).cuda()
if args.model_path:
state = torch.load(args.model_path, map_location='cpu')
filtered_dict = {k: v for k, v in state['model'].items() if (k in model.state_dict() and 'head.fc' not in k)}
model.load_state_dict(filtered_dict, strict=False)
# model.load_state_dict(state['model'], strict=False)
print('done\n')
# Data loading code
normalize = transforms.Normalize(mean=[0, 0, 0], std=[1, 1, 1])
instances_path_val = os.path.join(args.data, 'annotations/instances_val2014.json')
instances_path_train = os.path.join(args.data, 'annotations/instances_train2014.json')
# data_path_val = os.path.join(args.data, 'val2014')
# data_path_train = os.path.join(args.data, 'train2014')
data_path_val = args.data
data_path_train = args.data
val_dataset = CocoDetection(data_path_val,
instances_path_val,
transforms.Compose([
transforms.Resize((args.image_size, args.image_size)),
transforms.ToTensor(),
# normalize, # no need, toTensor does normalization
]))
train_dataset = CocoDetection(data_path_train,
instances_path_train,
transforms.Compose([
transforms.Resize((args.image_size, args.image_size)),
CutoutPIL(cutout_factor=0.5),
RandAugment(),
transforms.ToTensor(),
# normalize,
]))
print("len(val_dataset)): ", len(val_dataset))
print("len(train_dataset)): ", len(train_dataset))
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=False)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=False)
train_multi_label_coco(model, train_loader, val_loader, args.lr, gamma_neg=4, gamma_pos=0, clip=0.05)
def train_multi_label_coco(model, train_loader, val_loader, lr=2e-4, gamma_neg=4, gamma_pos=0, clip=0.05):
ema = ModelEma(model, 0.9997) # 0.9997^641=0.82
# set optimizer
Epochs = 40
weight_decay = 1e-4
criterion = AsymmetricLoss(gamma_neg=gamma_neg, gamma_pos=gamma_pos, clip=clip,
disable_torch_grad_focal_loss=True)
parameters = add_weight_decay(model, weight_decay)
optimizer = torch.optim.Adam(params=parameters, lr=lr, weight_decay=0) # true wd, filter_bias_and_bn
steps_per_epoch = len(train_loader)
scheduler = lr_scheduler.OneCycleLR(optimizer, max_lr=lr, steps_per_epoch=steps_per_epoch, epochs=Epochs,
pct_start=0.2)
highest_mAP = 0
trainInfoList = []
Sig = torch.nn.Sigmoid()
scaler = GradScaler()
for epoch in range(Epochs):
for i, (inputData, target) in enumerate(train_loader):
inputData = inputData.cuda()
target = target.cuda() # (batch,3,num_classes)
target = target.max(dim=1)[0]
with autocast(): # mixed precision
output = model(inputData).float() # sigmoid will be done in loss !
loss = criterion(output, target)
model.zero_grad()
scaler.scale(loss).backward()
# loss.backward()
scaler.step(optimizer)
scaler.update()
# optimizer.step()
scheduler.step()
ema.update(model)
# store information
if i % 100 == 0:
trainInfoList.append([epoch, i, loss.item()])
print('Epoch [{}/{}], Step [{}/{}], LR {:.1e}, Loss: {:.1f}'
.format(epoch, Epochs, str(i).zfill(3), str(steps_per_epoch).zfill(3),
scheduler.get_last_lr()[0], \
loss.item()))
try:
torch.save(model.state_dict(), os.path.join(
'models/', 'model-{}-{}.ckpt'.format(epoch + 1, i + 1)))
#save ema module (edited 21/02/12)
torch.save(ema.module.state_dict(), os.path.join(
'models/', 'ema_model-{}-{}.ckpt'.format(epoch + 1, i + 1)))
except:
pass
# modelName = 'models/' + 'decoder-{}-{}.ckpt'.format(epoch+1, i+1)
model.eval()
mAP_score = validate_multi(val_loader, model, ema)
model.train()
if mAP_score > highest_mAP:
highest_mAP = mAP_score
try:
torch.save(model.state_dict(), os.path.join(
'models/', 'model-highest.ckpt'))
#save ema module (edited 21/02/12)
torch.save(ema.module.state_dict(), os.path.join(
'models/', 'ema_model-{}-{}.ckpt'.format(epoch + 1, i + 1)))
except:
pass
print('current_mAP = {:.2f}, highest_mAP = {:.2f}\n'.format(mAP_score, highest_mAP))
def validate_multi(val_loader, model, ema_model):
print("starting validation")
Sig = torch.nn.Sigmoid()
preds_regular = []
preds_ema = []
targets = []
for i, (input, target) in enumerate(val_loader):
target = target
# compute output
with torch.no_grad():
with autocast():
output_regular = Sig(model(input.cuda())).cpu()
output_ema = Sig(ema_model.module(input.cuda())).cpu()
# for mAP calculation
preds_regular.append(output_regular.cpu().detach())
preds_ema.append(output_ema.cpu().detach())
targets.append(target.cpu().detach())
mAP_score_regular = mAP(torch.cat(targets).numpy(), torch.cat(preds_regular).numpy())
mAP_score_ema = mAP(torch.cat(targets).numpy(), torch.cat(preds_ema).numpy())
print("mAP score regular {:.2f}, mAP score EMA {:.2f}".format(mAP_score_regular, mAP_score_ema))
return max(mAP_score_regular, mAP_score_ema)
if __name__ == '__main__':
main()