-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
187 lines (161 loc) · 6.59 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
import argparse
import os
import torch
import torch.optim as optim
import torch.nn as nn
from utils import mkdir_p, parse_args
from utils import get_lr, save_checkpoint, create_save_path, savefinal
from utils import crl_utils
from solvers.runners import train, test, train_CRL, test_CRL
from solvers.loss import loss_dict
from models import model_dict
from datasets import dataloader_dict, dataset_nclasses_dict, dataset_classname_dict
from time import localtime, strftime
import json
import logging
args = parse_args()
torch.manual_seed(args.seed)
if __name__ == "__main__":
current_time = strftime("%d-%b", localtime())
# prepare save path
username = os.getlogin()
model_save_pth = f"{args.checkpoint}/{args.dataset}/{current_time}{create_save_path(args)}_{username}_{str(args.seed)}"
checkpoint_dir_name = model_save_pth
if not os.path.isdir(model_save_pth):
mkdir_p(model_save_pth)
logging.basicConfig(level=logging.INFO,
format="%(levelname)s: %(message)s",
handlers=[
logging.FileHandler(filename=os.path.join(
model_save_pth, "train.log")),
logging.StreamHandler()
])
logging.info(f"Setting up logging folder : {model_save_pth}")
num_classes = dataset_nclasses_dict[args.dataset]
classes_name_list = dataset_classname_dict[args.dataset]
# prepare model
logging.info(f"Using model : {args.model}")
model = model_dict[args.model](num_classes=num_classes,args=args)
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
model.cuda()
# set up dataset
logging.info(f"Using dataset : {args.dataset}")
trainloader, valloader, testloader = dataloader_dict[args.dataset](args)
logging.info(f"Setting up optimizer : {args.optimizer}")
if args.optimizer == "sgd":
optimizer = optim.SGD(model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
elif args.optimizer == "adam":
optimizer = optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=args.weight_decay)
history = crl_utils.History(len(trainloader.dataset))
criterion = loss_dict[args.loss](gamma=args.gamma, alpha=args.alpha, beta=args.beta,
loss=args.loss, delta=args.delta, history=history, arguments=args)
test_criterion = loss_dict["cross_entropy"]()
logging.info(
f"Step sizes : {args.schedule_steps} | lr-decay-factor : {args.lr_decay_factor}")
scheduler = optim.lr_scheduler.MultiStepLR(
optimizer, milestones=args.schedule_steps, gamma=args.lr_decay_factor)
start_epoch = args.start_epoch
best_acc = 0.
best_acc_stats = {"top1": 0.0}
if("CRL" in args.loss):
train = train_CRL
test = test_CRL
for epoch in range(start_epoch, args.epochs):
logging.info('Epoch: [%d | %d] LR: %f' %
(epoch + 1, args.epochs, get_lr(optimizer)))
train_loss, top1_train = train(
trainloader, model, optimizer, criterion)
val_loss, top1_val, _, _, sce_score_val, ece_score_val, _ = test(
valloader, model, test_criterion)
test_loss, top1, top3, top5, sce_score, ece_score, all_metrics = test(
testloader, model, test_criterion)
scheduler.step()
logging.info("End of epoch {} stats: train_loss: {:.4f} | val_loss: {:.4f} | top1_train: {:.4f} | top1: {:.4f} | SCE: {:.5f} | ECE: {:.5f} | AUROC: {:5f} | FPR-AT-95: {:5f} | AUPR-S: {:5f} | AUPR-E: {:5f} | AURC: {:5f} | EAURC: {:5f}".format(
epoch+1,
train_loss,
test_loss,
top1_train,
top1,
sce_score,
ece_score,
all_metrics["auroc"],
all_metrics["fpr-at-95"],
all_metrics["aupr-success"],
all_metrics["aupr-error"],
all_metrics["aurc"],
all_metrics["eaurc"]
# "\n".join("{}\t{}".format(k, v) for k, v in auroc.items())
))
# save best accuracy model
is_best = top1_val > best_acc
best_acc = max(best_acc, top1_val)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'dataset': args.dataset,
'model': args.model
}, is_best, checkpoint=model_save_pth)
# Update best stats
if is_best:
best_acc_stats = {
"top1": top1,
"top3": top3,
"top5": top5,
"SCE": sce_score,
"ECE": ece_score,
"metrics": all_metrics,
"epoch": epoch
}
try:
savefinal(checkpoint=model_save_pth)
except:
pass
# save results to train_results.json
jsonfile = args.trainresultsfile+"_"+username+".json"
if not os.path.isfile(jsonfile):
with open(jsonfile, 'w') as f:
json.dump({}, f)
data = []
if os.stat(jsonfile).st_size != 0:
data = json.load(open(jsonfile))
data.append({
"model": args.model,
"dataset": args.dataset,
"loss": args.loss+"_"+args.pairing,
"alpha": args.alpha,
"beta": args.beta,
"gamma": args.gamma,
"theta": args.theta,
"scaling": args.scalefactor,
"total_epochs": args.epochs,
"scheduler steps": args.schedule_steps,
"top3": best_acc_stats["top3"],
"top5": best_acc_stats["top5"],
"SCE": best_acc_stats["SCE"],
"ECE": best_acc_stats["ECE"],
"top1": best_acc_stats["top1"],
"AUROC": best_acc_stats["metrics"]["auroc"],
"FPR-AT-95": best_acc_stats["metrics"]["fpr-at-95"],
"AUPR-S": best_acc_stats["metrics"]["aupr-success"],
"AUPR-E": best_acc_stats["metrics"]["aupr-error"],
"AURC": best_acc_stats["metrics"]["aurc"],
"EAURC": best_acc_stats["metrics"]["eaurc"],
"bestepoch": best_acc_stats["epoch"],
"date": strftime("%d-%b", localtime())
})
# "loss": args.loss,
with open(jsonfile, 'w') as f:
json.dump(data, f, indent=4)
logging.info("training completed...")
logging.info("The stats for best trained model on test set are as below:")
best_acc_stats["tpr"]=None
best_acc_stats["fpr"]=None
logging.info(best_acc_stats)