forked from Alibaba-MIIL/ML_Decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
185 lines (154 loc) · 7.24 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
import os
import argparse
import torch
import torch.nn.parallel
import torch.optim
import torch.utils.data.distributed
import torchvision.transforms as transforms
from torch.optim import lr_scheduler
from src_files.helper_functions.helper_functions import mAP, CocoDetection, CutoutPIL, ModelEma, \
add_weight_decay
from src_files.models import create_model
from src_files.loss_functions.losses import AsymmetricLoss
from randaugment import RandAugment
from torch.cuda.amp import GradScaler, autocast
parser = argparse.ArgumentParser(description='PyTorch MS_COCO Training')
parser.add_argument('--data', type=str, default='/home/muhammad.ali/Desktop/Research/MLDECODER/coco')
parser.add_argument('--lr', default=1e-4, type=float)
parser.add_argument('--model-name', default='tresnet_l')
parser.add_argument('--model-path', default='https://miil-public-eu.oss-eu-central-1.aliyuncs.com/model-zoo/ML_Decoder/tresnet_l.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')
parser.add_argument('--image-size', default=448, type=int,
metavar='N', help='input image size (default: 448)')
parser.add_argument('--batch-size', default=56, type=int,
metavar='N', help='mini-batch size')
# ML-Decoder
parser.add_argument('--use-ml-decoder', default=1, type=int)
parser.add_argument('--num-of-groups', default=-1, type=int) # full-decoding
parser.add_argument('--decoder-embedding', default=768, type=int)
parser.add_argument('--zsl', default=0, type=int)
# CLIP
parser.add_argument('--use-clip-encoder',default= 1,type=int )
def main():
args = parser.parse_args()
# Setup model
print('creating model {}...'.format(args.model_name))
model = create_model(args).cuda()
# local_rank = torch.distributed.get_rank()
# torch.cuda.set_device(0)
# model = torch.nn.DataParallel(model,device_ids=[0])
print('done')
# COCO Data loading
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 = args.data
#data_path_train = args.data
data_path_val = f'{args.data}/val2014' # args.data
data_path_train = f'{args.data}/train2014' # 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))
# Pytorch Data loader
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=False)
# Actuall Training
train_multi_label_coco(model, train_loader, val_loader, args.lr)
def train_multi_label_coco(model, train_loader, val_loader, lr):
ema = ModelEma(model, 0.9997) # 0.9997^641=0.82
# set optimizer
Epochs = 40
weight_decay = 1e-4
criterion = AsymmetricLoss(gamma_neg=4, gamma_pos=0, clip=0.05, 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 = []
scaler = GradScaler()
for epoch in range(Epochs):
for i, (inputData, target) in enumerate(train_loader):
inputData = inputData.cuda()
target = target.cuda()
target = target.max(dim=1)[0]
with autocast(): # mixed precision
output = model(inputData).float() # sigmoid will be done in loss !
#import pdb; pdb.set_trace()
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)))
except:
pass
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'))
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
target = target.max(dim=1)[0]
# 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()