-
Notifications
You must be signed in to change notification settings - Fork 26
/
monai_trainer.py
456 lines (324 loc) · 15.6 KB
/
monai_trainer.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import os
import time
import shutil
import json
# import tempfile
# import matplotlib.pyplot as plt
import numpy as np
# import json
import torch
from torch.utils.tensorboard import SummaryWriter
from torch.cuda.amp import GradScaler, autocast #native AMP
import torch.nn.parallel
import torch.distributed as dist
import torch.nn.functional as F
import torch.multiprocessing as mp
import torch.utils.data.distributed
import scipy.ndimage as ndimage
from monai.transforms import AsDiscrete,Activations,Compose
import sys
sys.path.append('../../pipextra/lib/python3.6/site-packages') #add missing packages
def json_get_fold(datalist, basedir, fold=0, key='training'):
with open(datalist) as f:
json_data = json.load(f)
json_data = json_data[key]
for d in json_data:
for k, v in d.items():
if isinstance(d[k], list):
d[k] = [os.path.join(basedir, iv) for iv in d[k]]
elif isinstance(d[k], str):
d[k] = os.path.join(basedir, d[k]) if len(d[k]) > 0 else d[k]
tr=[]
val=[]
for d in json_data:
if 'fold' in d and d['fold'] == fold:
val.append(d)
else:
tr.append(d)
return tr, val
import math
#template copied from torch.utils.data.distributed.DistributedSampler
class AMDistributedSampler(torch.utils.data.Sampler):
def __init__(self, dataset, num_replicas=None, rank=None,
shuffle=True, make_even=True):
if num_replicas is None:
if not torch.distributed.is_available():
raise RuntimeError("Requires distributed package to be available")
num_replicas = torch.distributed.get_world_size()
if rank is None:
if not torch.distributed.is_available():
raise RuntimeError("Requires distributed package to be available")
rank = torch.distributed.get_rank()
self.shuffle = shuffle
self.make_even = make_even
self.dataset = dataset
self.num_replicas = num_replicas
self.rank = rank
self.epoch = 0
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas))
self.total_size = self.num_samples * self.num_replicas
#to track of smaller batches
indices = list(range(len(self.dataset)))
self.valid_length = len(indices[self.rank:self.total_size:self.num_replicas])
def __iter__(self):
# deterministically shuffle based on epoch
if self.shuffle:
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = list(range(len(self.dataset)))
# add extra samples to make it evenly divisible (otherwise will return last batch smaller)
if self.make_even:
if len(indices) < self.total_size:
if self.total_size - len(indices) < len(indices):
indices += indices[:(self.total_size - len(indices))]
else:
extra_ids = np.random.randint(low=0,high=len(indices), size=self.total_size - len(indices)) #this ensures we get valid ids (if dataset is much smaller then world_size
indices += [indices[ids] for ids in extra_ids]
assert len(indices) == self.total_size
# subsample
indices = indices[self.rank:self.total_size:self.num_replicas]
self.num_samples = len(indices)
return iter(indices)
def __len__(self):
return self.num_samples
def set_epoch(self, epoch):
self.epoch = epoch
def distributed_all_gather(tensor_list, valid_batch_size=None, out_numpy=False, world_size=None, no_barrier=False, is_valid=None):
if world_size is None:
world_size = torch.distributed.get_world_size()
if valid_batch_size is not None:
valid_batch_size = min(valid_batch_size, world_size) #it can't be more then world_size
elif is_valid is not None:
is_valid = torch.tensor(bool(is_valid), dtype=torch.bool, device=tensor_list[0].device)
if not no_barrier:
torch.distributed.barrier() # synch processess, do we need it??
tensor_list_out = []
with torch.no_grad(): #? do we need it
if is_valid is not None:
is_valid_list = [torch.zeros_like(is_valid) for _ in range(world_size)]
torch.distributed.all_gather(is_valid_list, is_valid)
is_valid = [x.item() for x in is_valid_list] #list of bools
# print('is_valid list', is_valid)
for tensor in tensor_list:
gather_list = [torch.zeros_like(tensor) for _ in range(world_size)]
torch.distributed.all_gather(gather_list, tensor)
if valid_batch_size is not None:
gather_list = gather_list[:valid_batch_size] #keep only valid elements
elif is_valid is not None:
gather_list = [g for g,v in zip(gather_list, is_valid_list) if v]
# print('updated gather list', gather_list)
if out_numpy:
gather_list = [t.cpu().numpy() for t in gather_list] #convert to numpy
tensor_list_out.append(gather_list)
return tensor_list_out
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
# self.avg = self.sum / self.count if self.count > 0 else self.sum
self.avg = np.where(self.count > 0, self.sum / self.count, self.sum)
def train_epoch(model, loader, optimizer, scaler, epoch, loss_func, args):
model.train()
start_time = time.time()
run_loss = AverageMeter()
run_acc = AverageMeter()
for idx, batch_data in enumerate(loader):
if isinstance(batch_data, list):
data, target = batch_data
else:
data, target = batch_data['image'], batch_data['label']
data, target = data.cuda(args.rank), target.cuda(args.rank)
# optimizer.zero_grad()
for param in model.parameters(): param.grad = None
with autocast(enabled=args.amp):
logits = model(data)
loss = loss_func(logits, target)
if args.amp:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
optimizer.step()
if args.distributed:
loss_list = distributed_all_gather([loss], out_numpy=True, is_valid=idx < loader.sampler.valid_length)
run_loss.update(np.mean(np.mean(np.stack(loss_list, axis=0), axis=0), axis=0), n=args.batch_size * args.world_size)
else:
# run_acc.update(acc.cpu().numpy(), n=not_nans.cpu().numpy())
run_loss.update(loss.item(), n=args.batch_size)
if args.rank==0:
# print(not_nans)
print('Epoch {}/{} {}/{}'.format(epoch, args.max_epochs, idx, len(loader)),
'loss: {:.4f}'.format(run_loss.avg),
# 'acc', run_acc.avg,
'time {:.2f}s'.format(time.time() - start_time))
start_time = time.time()
for param in model.parameters(): param.grad = None #just in case
return run_loss.avg#, run_acc.avg
def resample(img, target_size):
imx, imy, imz = img.shape
tx, ty, tz = target_size
zoom_ratio = ( float(tx) / float(imx), float(ty) / float(imy), float(tz) / float(imz))
img_resampled = ndimage.zoom( img, zoom_ratio, order=0, prefilter=False)
return img_resampled
def dice(x, y):
intersect = np.sum(np.sum(np.sum(x * y)))
y_sum = np.sum(np.sum(np.sum(y)))
if y_sum == 0:
return 0.0
x_sum = np.sum(np.sum(np.sum(x)))
return 2 * intersect / (x_sum + y_sum)
def val_epoch(model, loader, val_shape_dict, epoch, loss_func, args, model_inferer=None,post_label=None,post_pred=None):
model.eval()
start_time = time.time()
run_loss = AverageMeter()
run_acc = AverageMeter()
with torch.no_grad():
for idx, batch_data in enumerate(loader):
if isinstance(batch_data, list):
data, target = batch_data
else:
data, target = batch_data['image'], batch_data['label']
data, target = data.cuda(args.rank), target.cuda(args.rank)
with autocast(enabled=args.amp):
if model_inferer is not None:
logits = model_inferer(data) #another inferer (e.g. sliding window)
else:
logits = model(data)
loss = loss_func(logits, target)
logits = torch.softmax(logits, 1).cpu().numpy()
logits = np.argmax(logits, axis = 1).astype(np.uint8)
target = target.cpu().numpy()[:,0,:,:,:]
name = batch_data["image_meta_dict"]['filename_or_obj'][0].split('/')[-1]
val_shape = val_shape_dict[name]
pred = resample(logits[0], val_shape)
y = resample(target[0], val_shape)
dice_list_sub = []
for i in range(1, args.num_classes):
organ_Dice = dice(pred == i, y == i)
dice_list_sub.append(organ_Dice)
if args.distributed:
torch.distributed.barrier()
gather_list_sub = [[0]*len(dice_list_sub) for _ in range(dist.get_world_size())]
torch.distributed.all_gather_object(gather_list_sub, dice_list_sub)
classes_metriclist = []
for i in range(args.num_classes-1):
class_metric = [s[i] for s in gather_list_sub]
classes_metriclist.append(class_metric)
avg_classes = np.mean(classes_metriclist, 1)
ave_all = np.mean(avg_classes)
# if not loss.is_cuda:
loss = loss.cuda(args.rank)
loss_list = distributed_all_gather([loss], out_numpy=True, is_valid=idx < loader.sampler.valid_length)
run_loss.update(np.mean(np.mean(np.stack(loss_list, axis=0), axis=0)), n=args.batch_size * args.world_size)
run_acc.update(avg_classes, n=1)
# If you do not use distributed, this program will raise error.
else:
avg_classes = np.array(dice_list_sub)
run_acc.update(avg_classes, n=args.batch_size)
run_loss.update(loss.item(), n=args.batch_size)
# print(args.rank, 'end1')
if args.rank == 0:
print('Batch mean: Liver: {}, Tumor: {}, all:{}'.format(avg_classes[0], avg_classes[1], np.mean(avg_classes)))
print('Val {}/{} {}/{}'.format(epoch, args.max_epochs, idx, len(loader)),
'loss: {:.4f}'.format(run_loss.avg),
'acc', run_acc.avg,
'acc_avg: {:.4f}'.format(np.mean(run_acc.avg)),
'time {:.2f}s'.format(time.time() - start_time))
start_time = time.time()
return run_loss.avg, run_acc.avg
def save_checkpoint(model, epoch, args, filename='model.pt', best_acc=0, optimizer=None, scheduler=None):
state_dict = model.state_dict() if not args.distributed else model.module.state_dict()
save_dict = {
'epoch': epoch,
'best_acc': best_acc,
'state_dict': state_dict
}
if optimizer is not None:
save_dict['optimizer'] = optimizer.state_dict()
if scheduler is not None:
save_dict['scheduler'] = scheduler.state_dict()
filename=os.path.join(args.logdir, filename)
torch.save(save_dict, filename)
print('Saving checkpoint', filename)
def run_training(model,
train_loader,
val_loader,
optimizer,
loss_func,
args,
val_shape_dict,
model_inferer=None,
scheduler=None,
start_epoch=0,
val_channel_names=None,
post_label=None,
post_pred=None
):
# np.set_printoptions(formatter={'float': '{: 0.3f}'.format}, suppress=True)
writer = None
if args.logdir is not None and args.rank == 0:
writer = SummaryWriter(log_dir=args.logdir)
if args.rank==0: print('Writing Tensorboard logs to ', writer.log_dir)
scaler = None
if args.amp: # new native amp
scaler = GradScaler()
val_acc_max = 0.
for epoch in range(start_epoch, args.max_epochs):
if args.distributed:
train_loader.sampler.set_epoch(epoch)
torch.distributed.barrier()
print(args.rank, time.ctime(), 'Epoch:', epoch)
epoch_time = time.time()
train_loss = train_epoch(model, train_loader, optimizer, scaler=scaler, epoch=epoch, loss_func=loss_func, args=args)
if args.rank == 0:
print('Final training {}/{}'.format(epoch, args.max_epochs - 1), 'loss: {:.4f}'.format(train_loss),
'time {:.2f}s'.format(time.time() - epoch_time))
if args.rank==0 and writer is not None:
writer.add_scalar('train_loss', train_loss, epoch)
b_new_best=False
val_acc = 0
if ( (epoch+1) % args.val_every == 0):
if args.distributed:
torch.distributed.barrier() # sync processes
epoch_time = time.time()
val_loss, val_acc = val_epoch(model, val_loader, val_shape_dict, epoch=epoch, loss_func=loss_func, model_inferer=model_inferer, args=args,post_label=post_label,post_pred=post_pred)
if args.rank == 0:
print('Final validation {}/{}'.format(epoch, args.max_epochs - 1), 'loss: {:.4f}'.format(val_loss),
'acc', val_acc, 'time {:.2f}s'.format(time.time() - epoch_time))
with open("test_online.txt", 'a') as f:
print('Final validation {}/{}'.format(epoch, args.max_epochs - 1), 'loss: {:.4f}'.format(val_loss),
'acc', val_acc, 'time {:.2f}s'.format(time.time() - epoch_time), file=f)
if writer is not None:
writer.add_scalar('val_loss', val_loss, epoch)
writer.add_scalar('val_mean_dice', np.mean(val_acc), epoch)
if val_channel_names is not None:
for val_channel_ind in range(len(val_channel_names)):
if val_channel_ind < val_acc.size:
writer.add_scalar(val_channel_names[val_channel_ind], val_acc[val_channel_ind], epoch)
if np.mean(val_acc) > val_acc_max:
print('new best ({:.6f} --> {:.6f}). '.format(val_acc_max, np.mean(val_acc)))
val_acc_max = np.mean(val_acc)
b_new_best = True
if args.rank == 0 and args.logdir is not None and args.save_checkpoint:
save_checkpoint(model, epoch, args, best_acc=val_acc_max, optimizer=optimizer, scheduler=scheduler)
if args.rank == 0 and args.logdir is not None and args.save_checkpoint:
save_checkpoint(model, epoch, args, best_acc=np.mean(val_acc), filename='model_final.pt')
if b_new_best:
print('Copying to model.pt new best model!!!!')
shutil.copyfile(os.path.join(args.logdir, 'model_final.pt'), os.path.join(args.logdir, 'model.pt'))
if scheduler is not None:
scheduler.step()
print('monai_trainer DONE, best acc', val_acc_max)
return val_acc_max