-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_seg.py
executable file
·349 lines (291 loc) · 11.9 KB
/
main_seg.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import torch
import models
import losses
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 torchsampler import ImbalancedDatasetSampler
from tqdm import tqdm
#%%
## load configs
parser = argparse.ArgumentParser(description="Segmentation")
parser.add_argument('--config', type=str, default="./configs/dtunet_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']
metric_cfg = args['METRICS']
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']
class_num = data_cfg['ClassNum']
in_channels = data_cfg['ImageChannel']
#%%
# 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 model
if model_cfg['Backbone'] == 'DTUNet':
train_cfg['Loss'] = {'dtu loss': 1} # can only use dtu loss
if model_cfg['Backbone'] == 'DTUNet':
model = models.DTUNet(in_channels=in_channels, out_channels=class_num).to(device)
else:
raise NotImplementedError()
exp_name = train_cfg['ExpName']
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(f"Save the checkpoint every {train_cfg['CheckRate']} epochs")
logger.fprint("Model")
# logger.fprint(model)
#%%
## setup optimisers
if train_cfg['UseSGD']:
lr = lr*100
optimizer = torch.optim.SGD(model.parameters(), lr=lr, weight_decay=weight_decay, momentum=train_cfg['Momentum'])
logger.fprint(f"Using SGD, lr is {lr}, momentum is {train_cfg['Momentum']}, weight decay is {weight_decay}")
else:
optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=weight_decay)
logger.fprint(f"Using AdamW, lr is {lr}, weight decay is {weight_decay}")
logger.fprint("Training settings")
logger.fprint(train_cfg)
#%%
## setup schedulers
if train_cfg['Scheduler'] == "ReduceOnPlateau":
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=20, verbose=True)
elif train_cfg['Scheduler'] == "Cosine":
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=epochs, eta_min=0)
else:
print(f"{train_cfg['Scheduler']} has not been implemented")
raise NotImplementedError()
#%%
## build datasets
tfs = []
tfs.append(A.Resize(*train_cfg['TrainSize']))
augs = train_cfg['TrainAugmentations']
for a in augs.keys():
aug = eval("A.%s(**%s)"%(a, augs[a]))
tfs.append(aug)
train_transforms = A.Compose(
tfs
)
tfs = []
tfs.append(A.Resize(*train_cfg['EvalSize']))
augs = train_cfg['EvalAugmentations']
for a in augs.keys():
aug = eval("A.%s(**%s)"%(a, augs[a]))
tfs.append(aug)
eval_transforms = A.Compose(
tfs
)
dataset_cfg = data_cfg['Configs']
if data_cfg['DataSet'] == 'FetalTrim3':
trainset = datasets.FetalSeg(train_transforms, split='train', **dataset_cfg)
valset = datasets.FetalSeg(eval_transforms, split='vali', **dataset_cfg)
testset = datasets.FetalSeg(eval_transforms, split='test', **dataset_cfg)
elif data_cfg['DataSet'] == 'CustomDataset': # your dataset
trainset = datasets.CustomSeg(train_transforms, split='train', **dataset_cfg)
valset = datasets.CustomSeg(eval_transforms, split='vali', **dataset_cfg)
testset = datasets.CustomSeg(eval_transforms, split='test', **dataset_cfg)
else:
logger.fprint(f"Dataset {data_cfg['DataSet']} has not been implemented.")
raise NotImplementedError()
# trainloader = DataLoader(trainset, batch_size=batch_size, sampler=ImbalancedDatasetSampler(trainset), drop_last=True, num_workers=np.min([batch_size, 32]))
trainloader = DataLoader(trainset, batch_size=batch_size, shuffle=True, 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]))
#%%
## build losses
# get weight for cse and focal loss
weight = None
logger.fprint(f"use weight: {weight}")
criterion = losses.Criterion(train_cfg['Loss'], train_cfg['LossConfigs'], weights=weight)
def train_one_epoch(loader, epoch):
model.train()
loss_meter = metrics.AverageMeter()
cls_meter = metrics.ClassMeter()
for x in tqdm(loader):
image, mask = x['gray_image'], x['mask']
image, mask = image.to(device), mask.to(device)
x['image'], x['mask'] = image, mask
lambda_ = train_cfg['InitLambda'] - epoch//train_cfg['ReduceStep']*0.1 # every N epochs reduce 10%
lambda_ = max([lambda_, 0.1]) # minimum lambda is 10%
x['lambda'] = lambda_
optimizer.zero_grad()
outs = model(x)
outs['mask'] = mask
outs['label'] = mask
logit = outs['logit']
loss_unred = criterion(outs)
loss = loss_unred['loss']
loss.backward()
optimizer.step()
batch_size = image.size(0)
loss_metrics = dict(zip(metric_cfg['TrainLossMetrics'], list(map(lambda x: loss_unred[x], metric_cfg['TrainLossMetrics']))))
loss_meter.add(loss_metrics, batch_size)
logit = outs['logit']
cls_meter.add(logit.permute(1,0,2,3).flatten(1),
torch.argmax(logit, dim=1).flatten(),
outs['mask'].flatten())
del logit
loss_metrics = list(map(lambda x: loss_meter.avg[x], metric_cfg['TrainLossMetrics']))
loss_metrics = dict(zip(metric_cfg['TrainLossMetrics'], loss_metrics))
cls_metrics = []
cls_meter.gather()
for m in metric_cfg['TrainClassMetrics']:
cls_metrics.append(eval(f"cls_meter.get_{m}()"))
cls_metrics = dict(zip(metric_cfg['TrainClassMetrics'], cls_metrics))
epoch_metrics = {**loss_metrics, **cls_metrics}
return epoch_metrics
def validate_one_epoch(loader):
model.eval()
loss_meter = metrics.AverageMeter()
cls_meter = metrics.ClassMeter()
preds = []
gts = []
with torch.no_grad():
for x in tqdm(loader):
image, mask = x['gray_image'], x['mask']
image, mask = image.to(device), mask.to(device)
x['image'], x['mask'] = image, mask
outs = model(x)
outs['mask'] = mask
outs['label'] = mask
logit = outs['logit']
loss_unred = criterion(outs)
batch_size = image.size(0)
loss_metrics = dict(zip(metric_cfg['EvalLossMetrics'], list(map(lambda x: loss_unred[x], metric_cfg['EvalLossMetrics']))))
loss_meter.add(loss_metrics, batch_size)
logit = outs['logit']
cls_meter.add(logit.permute(1,0,2,3).flatten(1),
torch.argmax(logit, dim=1).flatten(),
outs['mask'].flatten())
preds.append(logit.detach().cpu().numpy())
gts.append(outs['mask'].detach().cpu().numpy())
del logit
loss_metrics = list(map(lambda x: loss_meter.avg[x], metric_cfg['EvalLossMetrics']))
loss_metrics = dict(zip(metric_cfg['EvalLossMetrics'], loss_metrics))
cls_metrics = []
cls_meter.gather()
if config_args.eval:
preds = np.concatenate(preds, axis=0)
gts = np.concatenate(gts, axis=0)
np.save(f"{exp_folder}/preds.npy", preds)
np.save(f"{exp_folder}/gts.npy", gts)
for m in metric_cfg['EvalClassMetrics']:
cls_metrics.append(eval(f"cls_meter.get_{m}()"))
cls_metrics = dict(zip(metric_cfg['EvalClassMetrics'], cls_metrics))
epoch_metrics = {**loss_metrics, **cls_metrics}
return epoch_metrics
def train():
best_score = 0.0
best_epoch = 0
## load checkpoints
if len(config_args.checkpoint):
if os.path.exists(config_args.checkpoint):
ckp = torch.load(config_args.checkpoint)
# load model statedict
try:
model.load_state_dict(ckp['model_state_dict'])
except:
pass
# 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, epoch)
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)
if train_cfg['Scheduler'] == "ReduceOnPlateau":
scheduler.step(train_metrics['loss'])
else:
scheduler.step()
assert train_cfg['MonitorMetric'] in val_metrics.keys(),f"The monitored metric {train_cfg['MonitorMetric']} is not saved. "
val_score = val_metrics[train_cfg['MonitorMetric']]
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'%(train_cfg['MonitorMetric'], best_score, best_epoch))
## save checkpoints as a dictionary
if not (epoch % train_cfg['CheckRate']):
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():
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()