-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.py
609 lines (484 loc) · 24.4 KB
/
main.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import os
import numpy as np
import json, time
from functools import partial
import nibabel as nb
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.multiprocessing as mp
import torch.utils.data.distributed
import sys
from os import environ
from monai.inferers import sliding_window_inference
# from monai.data import DataLoader, Dataset
from monai.losses import DiceLoss, DiceCELoss
from monai.metrics import DiceMetric
from monai.utils.enums import MetricReduction
from monai.data import load_decathlon_datalist
from monai.transforms import AsDiscrete,Activations,Compose
from monai import transforms, data
from monai_trainer import AMDistributedSampler, run_training
from datafolds.datafold_read import datafold_read
from optimizers.lr_scheduler import WarmupCosineSchedule,LinearWarmupCosineAnnealingLR
from networks.unetr import UNETR
from networks.swin3d_unetr import SwinUNETR
from networks.swin3d_unetrv2 import SwinUNETR as SwinUNETR_v2
import warnings
warnings.filterwarnings("ignore")
## Online Tumor Generation
from TumorGenerated import TumorGenerated
import argparse
parser = argparse.ArgumentParser(description='brats21 segmentation testing')
parser.add_argument('--syn',action='store_true')
# parser.add_argument('--fold', default=0, type=int)
parser.add_argument('--checkpoint', default=None)
parser.add_argument('--logdir', default=None)
parser.add_argument('--save_checkpoint', action='store_true')
parser.add_argument('--max_epochs', default=5000, type=int)
parser.add_argument('--batch_size', default=1, type=int)
parser.add_argument('--optim_lr', default=1e-4, type=float)
parser.add_argument('--optim_name', default='adamw', type=str)
parser.add_argument('--reg_weight', default=1e-5, type=float)
parser.add_argument('--task', default='brats18')
parser.add_argument('--quick', action='store_true') #distributed multi gpu
parser.add_argument('--noamp', action='store_true') #experimental
parser.add_argument('--val_every', default=1, type=int)
parser.add_argument('--dropout_prob', default=0, type=float)
parser.add_argument('--val_overlap', default=0.5, type=float)
parser.add_argument('--distributed', action='store_true') #distributed multi gpu
parser.add_argument('--world_size', default=1, type=int, help='number of nodes for distributed training')
parser.add_argument('--rank', default=0, type=int, help='node rank for distributed training')
parser.add_argument('--dist-url', default='tcp://127.0.0.1:23456', type=str, help='url used to set up distributed training')
parser.add_argument('--dist-backend', default='nccl', type=str, help='distributed backend')
parser.add_argument('--workers', default=4, type=int)
parser.add_argument('--model_name', default='unet', type=str)
parser.add_argument('--swin_type', default='tiny', type=str)
#segmentation flex params
parser.add_argument('--seg_block', default='basic_pre', type=str)
parser.add_argument('--seg_num_blocks', default = '1,2,2,4', type=str)
parser.add_argument('--seg_base_filters', default=16, type=int)
parser.add_argument('--seg_relu', default='relu', type=str)
parser.add_argument('--seg_lastnorm_init_zero', action='store_true')
parser.add_argument('--seg_mode', default=1, type=int)
parser.add_argument('--seg_use_se', action='store_true')
parser.add_argument('--seg_norm_name', default='instancenorm', type=str)
parser.add_argument('--seg_noskip', action='store_true')
parser.add_argument('--seg_aug_mode', default=0, type=int)
parser.add_argument('--seg_aug_noflip', action='store_true')
parser.add_argument('--seg_norm_mode', default=0, type=int)
parser.add_argument('--seg_crop_mode', default=0, type=int)
parser.add_argument('--optuna', action='store_true')
parser.add_argument('--optuna_study_name', default='optuna_study', type=str)
parser.add_argument('--optuna_sampler', default=None, type=str)
parser.add_argument('--optuna_allfolds', action='store_true')
#unetr params
parser.add_argument('--pos_embedd', default='conv', type=str)
parser.add_argument('--norm_name', default='instance', type=str)
parser.add_argument('--num_steps', default=40000, type=int)
parser.add_argument('--eval_num', default=500, type=int)
parser.add_argument('--warmup_steps', default=500, type=int)
parser.add_argument('--num_heads', default=16, type=int)
parser.add_argument('--mlp_dim', default=3072, type=int)
parser.add_argument('--hidden_size', default=768, type=int)
# parser.add_argument('--feature_size', default=12, type=int)
parser.add_argument('--in_channels', default=1, type=int)
parser.add_argument('--out_channels', default=3, type=int)
parser.add_argument('--num_classes', default=3, type=int)
parser.add_argument('--res_block', action='store_true')
parser.add_argument('--conv_block', action='store_true')
parser.add_argument('--roi_x', default=96, type=int)
parser.add_argument('--roi_y', default=96, type=int)
parser.add_argument('--roi_z', default=96, type=int)
parser.add_argument('--dropout_rate', default=0.0, type=float)
parser.add_argument('--decay', default=1e-5, type=float)
parser.add_argument('--lrdecay', action='store_true')
parser.add_argument('--amp', action='store_true')
parser.add_argument('--amp_scale', action='store_true')
parser.add_argument('--opt_level', default='O2', type=str)
parser.add_argument('--opt', default='adamw', type=str)
parser.add_argument('--lrschedule', default='warmup_cosine', type=str)
parser.add_argument('--randaugment_n', default=0, type=int)
parser.add_argument('--warmup_epochs', default=100, type=int)
parser.add_argument('--resume_ckpt', action='store_true')
parser.add_argument('--pretrained_dir', default=None, type=str)
parser.add_argument('--train_dir', default=None, type=str)
parser.add_argument('--val_dir', default=None, type=str)
parser.add_argument('--json_dir', default=None, type=str)
parser.add_argument('--cache_num', default=500, type=int)
parser.add_argument('--use_pretrained', action='store_true')
def optuna_objective(trial, args):
if args.optuna_study_name == 'feta21_randaugment':
args.seg_aug_mode=5
args.randaugment_n = trial.suggest_categorical("randaugment_n", [1,2,3,4,5,6])
args.randaugment_p = trial.suggest_categorical("randaugment_p", [0.1,0.3,0.5,0.7,0.9])
else:
args.seg_block = trial.suggest_categorical("seg_block", ["basic_pre", "basic"])
args.seg_norm_name = trial.suggest_categorical("seg_norm_name", ["groupnorm", "instancenorm"])
args.seg_relu = trial.suggest_categorical("seg_relu", ["relu", "leaky_relu"])
args.seg_use_se = trial.suggest_categorical("seg_use_se", [True, False])
args.reg_weight = trial.suggest_categorical("reg_weight", [0, 1e-5])
# create the formatted name of log directory
if args.logdir_init is not None:
sall = []
for s in trial.params.values():
if isinstance(s, float):
sall.append('{:.1e}'.format(s) if s < 0.001 else "{:.3f}".format(s))
else:
sall.append(str(s))
args.logdir = args.logdir_init + '/' + str(trial.number) + '_' + '_'.join(sall) #unique logdir name
trial.set_user_attr('logdir', args.logdir)
print("Optuna updated argument values:")
for k, v in vars(args).items():
print(k, '=>', v)
print('-----------------')
if not args.optuna_allfolds:
accuracy = main_worker(gpu=0, args=args)
else:
accuracy = 0
for i in range(5):
print('Running fold', i)
args.fold = i
accuracy += main_worker(gpu=0, args=args)
accuracy = accuracy / 5.0
# trial.report(accuracy, epoch)
# # Handle pruning based on the intermediate value.
# if trial.should_prune():
# raise optuna.exceptions.TrialPruned()
return accuracy
def optuna_run(args):
import optuna
from optuna.trial import TrialState
args.logdir_init = args.logdir
study_name = args.optuna_study_name
sampler = None
if args.optuna_sampler is not None:
if args.optuna_sampler=='tpe':
sampler = optuna.samplers.TPESampler()
elif args.optuna_sampler=='random':
sampler = optuna.samplers.RandomSampler()
elif args.optuna_sampler=='grid':
if args.optuna_study_name == 'feta21_randaugment':
search_space = {"randaugment_n": [1, 2, 3, 4, 5, 6],
"randaugment_p": [0.1, 0.3, 0.5, 0.7, 0.9]
}
else:
search_space = {"seg_block": ["basic_pre", "basic"],
"seg_norm_name": ["groupnorm", "instancenorm"],
"seg_relu" : ["relu", "leaky_relu"],
"seg_use_se" : [False, True],
"reg_weight" : [0, 1e-5],
}
sampler = optuna.samplers.GridSampler(search_space=search_space)
print('Using optuna sampler', sampler, study_name)
objective = partial(optuna_objective, args=args)
study = optuna.create_study(
storage="sqlite:///optuna.db",
sampler=sampler,
study_name=study_name,
direction="maximize",
load_if_exists=True)
#
callbacks=[]
if args.logdir is not None:
from optuna_tensorboard import TensorBoardPTCallback
callbacks.append(TensorBoardPTCallback())
study.optimize(objective, callbacks=callbacks, gc_after_trial=True)
# study.optimize(objective, gc_after_trial=True)
pruned_trials = study.get_trials(deepcopy=False, states=[TrialState.PRUNED])
complete_trials = study.get_trials(deepcopy=False, states=[TrialState.COMPLETE])
print("Study statistics: ")
print(" Number of finished trials: ", len(study.trials))
print(" Number of pruned trials: ", len(pruned_trials))
print(" Number of complete trials: ", len(complete_trials))
print("Best trial:")
trial = study.best_trial
print(" Value: ", trial.value)
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))
def _get_transform(args):
if args.syn:
train_transform = transforms.Compose(
[
transforms.LoadImaged(keys=["image", "label"]),
transforms.AddChanneld(keys=["image", "label"]),
transforms.Orientationd(keys=["image", "label"], axcodes="RAS"),
transforms.Spacingd(keys=["image", "label"], pixdim=(1.0, 1.0, 1.0), mode=("bilinear", "nearest")),
TumorGenerated(keys=["image", "label"], prob=0.9), # here we use online
transforms.ScaleIntensityRanged(
keys=["image"], a_min=-21, a_max=189,
b_min=0.0, b_max=1.0, clip=True,
),
transforms.SpatialPadd(keys=["image", "label"], mode=["minimum", "constant"], spatial_size=[96, 96, 96]),
# transforms.CropForegroundd(keys=["image", "label"], source_key="image", k_divisible=roi_size),
# transforms.RandSpatialCropd(keys=["image", "label"], roi_size=roi_size, random_size=False),
transforms.RandCropByPosNegLabeld(
keys=["image", "label"],
label_key="label",
spatial_size=(96, 96, 96),
pos=1,
neg=1,
num_samples=1,
image_key="image",
image_threshold=0,
),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=0),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=1),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=2),
transforms.RandRotate90d(keys=["image", "label"], prob=0.2, max_k=3),
# transforms.NormalizeIntensityd(keys="image", nonzero=True, channel_wise=True),
transforms.RandScaleIntensityd(keys="image", factors=0.1, prob=0.15),
transforms.RandShiftIntensityd(keys="image", offsets=0.1, prob=0.15),
transforms.ToTensord(keys=["image", "label"]),
]
)
else:
train_transform = transforms.Compose(
[
transforms.LoadImaged(keys=["image", "label"]),
transforms.AddChanneld(keys=["image", "label"]),
transforms.Orientationd(keys=["image", "label"], axcodes="RAS"),
transforms.Spacingd(keys=["image", "label"], pixdim=(1.0, 1.0, 1.0), mode=("bilinear", "nearest")),
transforms.ScaleIntensityRanged(
keys=["image"], a_min=-21, a_max=189,
b_min=0.0, b_max=1.0, clip=True,
),
transforms.SpatialPadd(keys=["image", "label"], mode=["minimum", "constant"], spatial_size=[96, 96, 96]),
# transforms.CropForegroundd(keys=["image", "label"], source_key="image", k_divisible=roi_size),
# transforms.RandSpatialCropd(keys=["image", "label"], roi_size=roi_size, random_size=False),
transforms.RandCropByPosNegLabeld(
keys=["image", "label"],
label_key="label",
spatial_size=(96, 96, 96),
pos=1,
neg=1,
num_samples=1,
image_key="image",
image_threshold=0,
),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=0),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=1),
transforms.RandFlipd(keys=["image", "label"], prob=0.2, spatial_axis=2),
transforms.RandRotate90d(keys=["image", "label"], prob=0.2, max_k=3),
# transforms.NormalizeIntensityd(keys="image", nonzero=True, channel_wise=True),
transforms.RandScaleIntensityd(keys="image", factors=0.1, prob=0.15),
transforms.RandShiftIntensityd(keys="image", offsets=0.1, prob=0.15),
transforms.ToTensord(keys=["image", "label"]),
]
)
val_transform = transforms.Compose(
[
transforms.LoadImaged(keys=["image", "label"]),
# transforms.ConvertToMultiChannelBasedOnBratsClassesd(keys="label"),
# transforms.NormalizeIntensityd(keys="image", nonzero=True, channel_wise=True),
transforms.AddChanneld(keys=["image", "label"]),
transforms.Orientationd(keys=["image", "label"], axcodes="RAS"),
transforms.Spacingd(keys=["image", "label"], pixdim=(1.0, 1.0, 1.0), mode=("bilinear", "nearest")),
transforms.ScaleIntensityRanged(keys=["image"], a_min=-21, a_max=189, b_min=0.0, b_max=1.0, clip=True),
transforms.SpatialPadd(keys=["image", "label"], mode=["minimum", "constant"], spatial_size=[96, 96, 96]),
transforms.ToTensord(keys=["image", "label"]),
]
)
return train_transform, val_transform
def main():
args = parser.parse_args()
args.amp = not args.noamp
if args.randaugment_n > 0:
args.seg_aug_mode=5
print("MAIN Argument values:")
for k, v in vars(args).items():
print(k, '=>', v)
print('-----------------')
if args.optuna:
optuna_run(args)
else:
if args.distributed:
args.ngpus_per_node = torch.cuda.device_count()
print('Found total gpus', args.ngpus_per_node)
args.world_size = args.ngpus_per_node * args.world_size
mp.spawn(main_worker, nprocs=args.ngpus_per_node, args=(args,))
else:
# Simply call main_worker function
main_worker(gpu=0, args=args)
def main_worker(gpu, args):
if args.distributed:
torch.multiprocessing.set_start_method('fork', force=True) #in new Pytorch/python labda functions fail to pickle with spawn
np.set_printoptions(formatter={'float': '{: 0.3f}'.format}, suppress=True)
args.gpu = gpu
if args.distributed:
args.rank = args.rank * args.ngpus_per_node + gpu
dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url, world_size=args.world_size, rank=args.rank)
torch.cuda.set_device(args.gpu) #use this default device (same as args.device if not distributed)
torch.backends.cudnn.benchmark = True
print(args.rank, ' gpu', args.gpu)
if args.rank==0:
print('Batch size is:', args.batch_size, 'epochs', args.max_epochs)
roi_size = [args.roi_x, args.roi_y, args.roi_x]
inf_size = [args.roi_x, args.roi_y, args.roi_x]
num_blocks = list(map(int, args.seg_num_blocks.split(',')))
data_dir = args.train_dir
val_data_dir = args.val_dir
datalist_json = args.json_dir
if environ.get('NGC_JOB_ID') is None:
root_dir = '/data/tmp/brats2021challenge' #my local folder, change to yours
else:
root_dir ='../../../dataset/dataset3' #on ngc mount data to this folder
train_transform, val_transform = _get_transform(args)
## NETWORK
if (args.model_name is None) or args.model_name == 'unet':
from monai.networks.nets import UNet
model = UNet(
spatial_dims=3,
in_channels=1,
out_channels=3,
channels=(16, 32, 64, 128, 256),
strides=(2, 2, 2, 2),
num_res_units=2,
)
elif args.model_name == 'swin_unetrv2':
if args.swin_type == 'tiny':
feature_size=12
elif args.swin_type == 'small':
feature_size=24
elif args.swin_type == 'base':
feature_size=48
model = SwinUNETR_v2(in_channels=1,
out_channels=3,
img_size=(96, 96, 96),
feature_size=feature_size,
patch_size=2,
depths=[2, 2, 2, 2],
num_heads=[3, 6, 12, 24],
window_size=[7, 7, 7])
if args.use_pretrained:
pretrained_add = 'model_swinvit.pt'
model.load_from(weights=torch.load(pretrained_add))
print('Use pretrained ViT weights from: {}'.format(pretrained_add))
else:
raise ValueError('Unsupported model ' + str(args.model_name))
if args.resume_ckpt:
model_dict = torch.load(args.pretrained_dir)
model.load_state_dict(model_dict['state_dict'])
print('Use pretrained weights')
dice_loss = DiceCELoss(to_onehot_y=True, softmax=True, squared_pred=True, smooth_nr=0, smooth_dr=1e-6)
post_label = AsDiscrete(to_onehot=True, n_classes=args.num_classes)
post_pred = AsDiscrete(argmax=True, to_onehot=True, n_classes=args.num_classes)
val_channel_names=['val_liver_dice', 'val_tumor_dice']
print('Crop size', roi_size)
datalist = load_decathlon_datalist(datalist_json, True, "training", base_dir=data_dir)
val_files = load_decathlon_datalist(datalist_json, True, "validation", base_dir=val_data_dir)
new_datalist = []
for item in datalist:
new_item = {}
new_item['image'] = item['image'].replace('.npy', '')
new_item['label'] = item['label'].replace('.npy', '')
new_datalist.append(new_item)
new_val_files = []
for item in val_files:
new_item = {}
new_item['image'] = item['image'].replace('.npy', '.gz')
new_item['label'] = item['label'].replace('.npy', '.gz')
new_val_files.append(new_item)
val_shape_dict = {}
for d in new_val_files:
imagepath = d["image"]
imagename = imagepath.split('/')[-1]
imgnb = nb.load(imagepath)
val_shape_dict[imagename] = [imgnb.shape[0], imgnb.shape[1], imgnb.shape[2]]
print('Totoal number of validation: {}'.format(len(val_shape_dict)))
if args.quick:
train_files = train_files[:4]
validation_files = validation_files[:3]
print('train_files files', len(new_datalist), 'validation files', len(new_val_files))
# train_ds = data.Dataset(data=new_datalist, transform=train_transform)
# cache dataset
# train_ds = data.CacheDataset(
# data=datalist,
# transform=train_transform,
# cache_num=args.cache_num,
# cache_rate=1.0,
# num_workers=args.workers,
# )
train_ds = data.SmartCacheDataset(
data=datalist,
transform=train_transform,
cache_num=args.cache_num,
cache_rate=1.0,
num_init_workers=args.workers//2,
num_replace_workers=4
)
train_sampler = AMDistributedSampler(train_ds) if args.distributed else None
train_loader = data.DataLoader(train_ds, batch_size=args.batch_size, shuffle=(train_sampler is None), num_workers=4, sampler=train_sampler, pin_memory=True)
val_ds = data.Dataset(data=new_val_files, transform=val_transform)
# val_ds = data.CacheDataset(
# data=new_val_files,
# transform=val_transform,
# cache_num=args.cache_num,
# cache_rate=1.0,
# num_workers=args.workers,
# )
val_sampler = AMDistributedSampler(val_ds, shuffle=False) if args.distributed else None
val_loader = data.DataLoader(val_ds, batch_size=1, shuffle=False, num_workers=4, sampler=val_sampler, pin_memory=True)
model_inferer = partial(sliding_window_inference, roi_size=inf_size, sw_batch_size=1, predictor=model, overlap=args.val_overlap, mode='gaussian')
pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('Total parameters count', pytorch_total_params)
best_acc = 0
start_epoch = 0
if args.checkpoint is not None:
checkpoint = torch.load(args.checkpoint, map_location='cpu')
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
new_state_dict[k.replace('backbone.','')] = v
# load params
model.load_state_dict(new_state_dict, strict=False)
if 'epoch' in checkpoint:
start_epoch = checkpoint['epoch']
if 'best_acc' in checkpoint:
best_acc = checkpoint['best_acc']
# optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {}) (bestacc {})".format(args.checkpoint, start_epoch, best_acc))
model.cuda(args.gpu)
if args.distributed:
torch.cuda.set_device(args.gpu)
if args.norm_name=='batch':
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model.cuda(args.gpu) #??
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], output_device=args.gpu, find_unused_parameters=True)
if args.optim_name == 'adam':
optimizer = torch.optim.Adam(model.parameters(), lr=args.optim_lr, weight_decay=args.reg_weight)
elif args.optim_name == 'adamw':
optimizer = torch.optim.AdamW(model.parameters(), lr=args.optim_lr, weight_decay=args.reg_weight)
elif args.optim_name=='sgd':
optimizer = torch.optim.SGD(model.parameters(), lr=args.optim_lr, momentum=0.99, nesterov=True, weight_decay=args.reg_weight) #momentum 0.99, nestorov=True, following nnUnet
else:
raise ValueError('Unsupported optim_name' + str(args.optim_name))
if args.lrschedule == 'warmup_cosine':
scheduler = LinearWarmupCosineAnnealingLR(
optimizer, warmup_epochs=args.warmup_epochs, max_epochs=args.max_epochs
)
elif args.lrschedule == 'cosine_anneal':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.max_epochs)
if args.checkpoint is not None:
scheduler.step(epoch=start_epoch)
else:
scheduler = None
accuracy = run_training(model=model,
train_loader=train_loader,
val_loader=val_loader,
optimizer=optimizer,
loss_func=dice_loss,
args=args,
model_inferer=model_inferer,
scheduler=scheduler,
start_epoch=start_epoch,
val_channel_names=val_channel_names,
val_shape_dict=val_shape_dict,
post_label=post_label,
post_pred=post_pred)
return accuracy
if __name__ == '__main__':
main()