-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_predictor.py
285 lines (237 loc) · 9.06 KB
/
train_predictor.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
import torch
from tqdm import tqdm
import metrics
import datasets
import albumentations as A
from torch.utils.data import DataLoader
import numpy as np
import argparse
import yaml
from misc import Logger
import os
import random
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from torchsampler import ImbalancedDatasetSampler
import math
import models
from pathlib import PurePath
#%%
## load configs
parser = argparse.ArgumentParser(description="Predictor")
parser.add_argument('--config', type=str, default="./configs/exp.yaml", metavar='-c')
parser.add_argument('--eval', type=bool, default=False, metavar='-e')
parser.add_argument('--checkpoint', type=str, metavar='-ckp', default='')
parser.add_argument('--model_path', type=str, metavar='-m', default='')
config_args = parser.parse_args()
with open(config_args.config, 'r') as f:
args = yaml.load(f, Loader=yaml.FullLoader)
data_cfg = args['DATA']
train_cfg = args['TRAINING']
model_cfg = args['MODEL']
device = "cuda" if torch.cuda.is_available() else "cpu"
if not train_cfg['UseCUDA']:
device = "cpu"
epochs = train_cfg['Epochs']
batch_size = train_cfg['BatchSize']
lr = train_cfg['LearningRate']
weight_decay = train_cfg['WeightDecay']
concept_num = data_cfg['ConceptNum']
cat_index = data_cfg['CatIndex']
class_num = data_cfg['ClassNum']
expand_dim = model_cfg['ExpandDim']
head_num = model_cfg['HeadNum']
#%%
# # fix random seed
seed = train_cfg['Seed']
torch.manual_seed(seed)
# np.random.seed(seed)
# random.seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# build the predictor
model = models.Predictor(
num_classes=class_num,
concept_num=concept_num,
expand_dim=expand_dim,
head_num=head_num,
cat_index=cat_index
)
model = model.to(device)
# set up model and dataset here
if data_cfg['DataSet'] == 'FetalTrim3':
# fetal dataset
## build datasets
eval_transforms = A.Resize(224, 288)
dataset_cfg = data_cfg['Configs']
trainset = datasets.FetalSeg(eval_transforms, split='train', **dataset_cfg)
valset = datasets.FetalSeg(eval_transforms, split='vali', **dataset_cfg)
testset = datasets.FetalSeg(eval_transforms, split='test', **dataset_cfg)
else:
raise NotImplementedError() # add your dataset here
exp_name = PurePath(config_args.config).parts[-2] + PurePath(config_args.config).parts[-1].split('.')[0]
exp_folder = os.path.join("./logs", exp_name)
model_path = os.path.join(exp_folder, "model.t7")
if not os.path.exists(exp_folder):
os.system(f"mkdir {exp_folder}")
os.system(f"cp {config_args.config} {os.path.join(exp_folder, 'config.yaml')}")
ckp_folder = os.path.join(exp_folder, 'checkpoints')
if not os.path.exists(ckp_folder):
os.system(f"mkdir {ckp_folder}")
# initialize logger
logger = Logger(os.path.join(exp_folder, 'logs.log'), 'a')
logger.fprint(f"Start experiment {exp_name}")
logger.fprint(f'Fix random seed at {seed}')
logger.fprint("Model")
#%%
## setup optimisers
optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=lr, weight_decay=weight_decay, momentum=0.9)
logger.fprint(f"Using SGD, lr is {lr}, momentum is {0.9}, weight decay is {weight_decay}")
#%%
## setup schedulers
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs)
trainloader = DataLoader(trainset, batch_size=batch_size, sampler=ImbalancedDatasetSampler(trainset), drop_last=False, num_workers=np.min([batch_size, 32]))
valloader = DataLoader(valset, batch_size=batch_size, shuffle=False, num_workers=np.min([batch_size, 32]))
testloader = DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=np.min([batch_size, 32]))
criterion = torch.nn.CrossEntropyLoss()
def train_one_epoch(loader):
model.train()
preds = []
gts = []
epoch_loss = 0
for x in tqdm(loader):
concept_logit = x['concept']
concept_logit = concept_logit.to(device)
# concept_logit[:,23] = 0
label = x['label']
label = label.to(device)
x['concept_logit'] = concept_logit
optimizer.zero_grad()
x = model(x)
logit = x['logit']
loss = criterion(logit, label)
loss.backward()
optimizer.step()
batch_size = concept_logit.size(0)
epoch_loss += loss.item()
pred = torch.argmax(logit, dim=1)
preds.append(pred.flatten().detach().cpu().numpy())
gts.append(label.cpu().numpy())
epoch_metrics = {'loss': epoch_loss/len(loader)}
preds = np.concatenate(preds, axis=0)
gts = np.concatenate(gts, axis=0)
acc = accuracy_score(gts, preds)
avg_acc = balanced_accuracy_score(gts, preds)
epoch_metrics['acc'] = acc
epoch_metrics['avg_acc'] = avg_acc
return epoch_metrics
def validate_one_epoch(loader):
model.eval()
preds = []
gts = []
epoch_loss = 0
for x in tqdm(loader):
concept_logit = x['concept']
concept_logit = concept_logit.to(device)
# concept_logit[:,23] = 0
label = x['label']
label = label.to(device)
x['concept_logit'] = concept_logit
with torch.no_grad():
x = model(x)
logit = x['logit']
loss = criterion(logit, label)
batch_size = concept_logit.size(0)
epoch_loss += loss.item()
pred = torch.argmax(logit, dim=1)
preds.append(pred.flatten().detach().cpu().numpy())
gts.append(label.cpu().numpy())
epoch_metrics = {'loss': epoch_loss/len(loader)}
preds = np.concatenate(preds, axis=0)
gts = np.concatenate(gts, axis=0)
acc = accuracy_score(gts, preds)
avg_acc = balanced_accuracy_score(gts, preds)
epoch_metrics['acc'] = acc
epoch_metrics['avg_acc'] = avg_acc
return epoch_metrics
def train():
best_score = 0
best_epoch = 0
## load checkpoints
if len(config_args.checkpoint):
if os.path.exists(config_args.checkpoint):
try:
ckp = torch.load(config_args.checkpoint)
# load model statedict
model.load_state_dict(ckp['model_state_dict'])
except:
logger.fprint("model parameters might not be loaded correctly")
# load optimiser statedict
optimizer.load_state_dict(ckp['optimizer_state_dict'])
# load scheduler statedict
scheduler.load_state_dict(ckp['scheduler_state_dict'])
# load epoch
start_epoch = ckp['epoch'] + 1
# best epoch and best score
best_epoch, best_score = ckp['best_epoch'], ckp['best_score']
logger.fprint(f"Loaded checkpoint {config_args.checkpoint}, start at epoch {start_epoch}. ")
del ckp
else:
logger.fprint(f"checkpoint {config_args.checkpoint} does not exist")
raise NameError()
else:
start_epoch = 0
for epoch in range(start_epoch, epochs):
train_metrics = train_one_epoch(trainloader)
log_info = f"epoch: {epoch: d}"
for k, v in train_metrics.items():
log_info += f", train_{k}: {v: .4f}"
logger.fprint(log_info)
val_metrics = validate_one_epoch(valloader)
log_info = f"epoch: {epoch: d}"
for k, v in val_metrics.items():
log_info += f", eval_{k}: {v: .4f}"
logger.fprint(log_info)
scheduler.step()
key = 'avg_acc'
val_score = val_metrics[key]
if val_score > best_score:
best_score = val_score
best_epoch = epoch
torch.save(model.state_dict(), model_path)
print(f'Model is saved at {model_path}!')
logger.fprint('Best %s: %.4f at epoch %d'%(key, best_score, best_epoch))
## save checkpoints as a dictionary
if not (epoch % 50):
ckp_name = os.path.join(ckp_folder, f"checkpoint_{epoch}.t7")
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'scheduler_state_dict': scheduler.state_dict(),
'loss': train_metrics['loss'],
'epoch': epoch,
'device': device,
'best_score': best_score,
'best_epoch': best_epoch
},
ckp_name)
logger.fprint(f"checkpoint saved at {ckp_name}")
def test():
try:
model.load_state_dict(torch.load(config_args.model_path if os.path.exists(config_args.model_path) else model_path))
except RuntimeError:
logger.fprint(f"The given model '{model_path}' is not valid.")
val_metrics = validate_one_epoch(testloader)
log_info = f"Test on Testset"
for k, v in val_metrics.items():
if k == 'confusion_matrix' or k == "classification_report":
log_info += f", eval_{k}: {v}"
else:
log_info += f", eval_{k}: {v: .4f}"
logger.fprint(log_info)
if __name__ == "__main__":
if config_args.eval:
logger.fprint("Start Testing")
test()
else:
logger.fprint("Start Training")
train()