forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
408 lines (373 loc) · 11.4 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
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
import logging
import os
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
import torch
import torch.distributed as dist
from monai.config import print_config
from monai.handlers import (
CheckpointSaver,
LrScheduleHandler,
MeanDice,
StatsHandler,
ValidationHandler,
from_engine,
)
from monai.inferers import SimpleInferer, SlidingWindowInferer
from monai.losses import DiceCELoss
from monai.utils import set_determinism
from torch.nn.parallel import DistributedDataParallel
from create_dataset import get_data
from create_network import get_network
from evaluator import DynUNetEvaluator
from task_params import data_loader_params, patch_size
from trainer import DynUNetTrainer
def validation(args):
# load hyper parameters
task_id = args.task_id
sw_batch_size = args.sw_batch_size
tta_val = args.tta_val
window_mode = args.window_mode
eval_overlap = args.eval_overlap
multi_gpu_flag = args.multi_gpu
local_rank = args.local_rank
amp = args.amp
# produce the network
checkpoint = args.checkpoint
val_output_dir = "./runs_{}_fold{}_{}/".format(task_id, args.fold, args.expr_name)
if multi_gpu_flag:
dist.init_process_group(backend="nccl", init_method="env://")
device = torch.device(f"cuda:{local_rank}")
torch.cuda.set_device(device)
else:
device = torch.device("cuda")
properties, val_loader = get_data(args, mode="validation")
net = get_network(properties, task_id, val_output_dir, checkpoint)
net = net.to(device)
if multi_gpu_flag:
net = DistributedDataParallel(
module=net, device_ids=[device], find_unused_parameters=True
)
n_classes = len(properties["labels"])
net.eval()
evaluator = DynUNetEvaluator(
device=device,
val_data_loader=val_loader,
network=net,
n_classes=n_classes,
inferer=SlidingWindowInferer(
roi_size=patch_size[task_id],
sw_batch_size=sw_batch_size,
overlap=eval_overlap,
mode=window_mode,
),
postprocessing=None,
key_val_metric={
"val_mean_dice": MeanDice(
include_background=False,
output_transform=from_engine(["pred", "label"]),
)
},
additional_metrics=None,
amp=amp,
tta_val=tta_val,
)
evaluator.run()
if local_rank == 0:
print(evaluator.state.metrics)
results = evaluator.state.metric_details["val_mean_dice"]
if n_classes > 2:
for i in range(n_classes - 1):
print(
"mean dice for label {} is {}".format(i + 1, results[:, i].mean())
)
def train(args):
# load hyper parameters
task_id = args.task_id
fold = args.fold
val_output_dir = "./runs_{}_fold{}_{}/".format(task_id, fold, args.expr_name)
log_filename = "nnunet_task{}_fold{}.log".format(task_id, fold)
log_filename = os.path.join(val_output_dir, log_filename)
interval = args.interval
learning_rate = args.learning_rate
max_epochs = args.max_epochs
multi_gpu_flag = args.multi_gpu
amp_flag = args.amp
lr_decay_flag = args.lr_decay
sw_batch_size = args.sw_batch_size
tta_val = args.tta_val
batch_dice = args.batch_dice
window_mode = args.window_mode
eval_overlap = args.eval_overlap
local_rank = args.local_rank
determinism_flag = args.determinism_flag
determinism_seed = args.determinism_seed
if determinism_flag:
set_determinism(seed=determinism_seed)
if local_rank == 0:
print("Using deterministic training.")
# transforms
train_batch_size = data_loader_params[task_id]["batch_size"]
if multi_gpu_flag:
dist.init_process_group(backend="nccl", init_method="env://")
device = torch.device(f"cuda:{local_rank}")
torch.cuda.set_device(device)
else:
device = torch.device("cuda")
properties, val_loader = get_data(args, mode="validation")
_, train_loader = get_data(args, batch_size=train_batch_size, mode="train")
# produce the network
checkpoint = args.checkpoint
net = get_network(properties, task_id, val_output_dir, checkpoint)
net = net.to(device)
if multi_gpu_flag:
net = DistributedDataParallel(
module=net, device_ids=[device], find_unused_parameters=True
)
optimizer = torch.optim.SGD(
net.parameters(),
lr=learning_rate,
momentum=0.99,
weight_decay=3e-5,
nesterov=True,
)
scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer, lr_lambda=lambda epoch: (1 - epoch / max_epochs) ** 0.9
)
# produce evaluator
val_handlers = [
StatsHandler(output_transform=lambda x: None),
CheckpointSaver(
save_dir=val_output_dir, save_dict={"net": net}, save_key_metric=True
),
]
evaluator = DynUNetEvaluator(
device=device,
val_data_loader=val_loader,
network=net,
n_classes=len(properties["labels"]),
inferer=SlidingWindowInferer(
roi_size=patch_size[task_id],
sw_batch_size=sw_batch_size,
overlap=eval_overlap,
mode=window_mode,
),
postprocessing=None,
key_val_metric={
"val_mean_dice": MeanDice(
include_background=False,
output_transform=from_engine(["pred", "label"]),
)
},
val_handlers=val_handlers,
amp=amp_flag,
tta_val=tta_val,
)
# produce trainer
loss = DiceCELoss(to_onehot_y=True, softmax=True, batch=batch_dice)
train_handlers = []
if lr_decay_flag:
train_handlers += [LrScheduleHandler(lr_scheduler=scheduler, print_lr=True)]
train_handlers += [
ValidationHandler(validator=evaluator, interval=interval, epoch_level=True),
StatsHandler(tag_name="train_loss", output_transform=from_engine(["loss"], first=True)),
]
trainer = DynUNetTrainer(
device=device,
max_epochs=max_epochs,
train_data_loader=train_loader,
network=net,
optimizer=optimizer,
loss_function=loss,
inferer=SimpleInferer(),
postprocessing=None,
key_train_metric=None,
train_handlers=train_handlers,
amp=amp_flag,
)
if local_rank > 0:
evaluator.logger.setLevel(logging.WARNING)
trainer.logger.setLevel(logging.WARNING)
logger = logging.getLogger()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
# Setup file handler
fhandler = logging.FileHandler(log_filename)
fhandler.setLevel(logging.INFO)
fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
if not multi_gpu_flag:
chandler = logging.StreamHandler()
chandler.setLevel(logging.INFO)
chandler.setFormatter(formatter)
logger.addHandler(chandler)
logger.setLevel(logging.INFO)
trainer.run()
if __name__ == "__main__":
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-fold", "--fold", type=int, default=0, help="0-5")
parser.add_argument(
"-task_id", "--task_id", type=str, default="04", help="task 01 to 10"
)
parser.add_argument(
"-root_dir",
"--root_dir",
type=str,
default="/workspace/data/medical/",
help="dataset path",
)
parser.add_argument(
"-expr_name",
"--expr_name",
type=str,
default="expr",
help="the suffix of the experiment's folder",
)
parser.add_argument(
"-datalist_path",
"--datalist_path",
type=str,
default="config/",
)
parser.add_argument(
"-train_num_workers",
"--train_num_workers",
type=int,
default=4,
help="the num_workers parameter of training dataloader.",
)
parser.add_argument(
"-val_num_workers",
"--val_num_workers",
type=int,
default=1,
help="the num_workers parameter of validation dataloader.",
)
parser.add_argument(
"-interval",
"--interval",
type=int,
default=5,
help="the validation interval under epoch level.",
)
parser.add_argument(
"-eval_overlap",
"--eval_overlap",
type=float,
default=0.5,
help="the overlap parameter of SlidingWindowInferer.",
)
parser.add_argument(
"-sw_batch_size",
"--sw_batch_size",
type=int,
default=4,
help="the sw_batch_size parameter of SlidingWindowInferer.",
)
parser.add_argument(
"-window_mode",
"--window_mode",
type=str,
default="gaussian",
choices=["constant", "gaussian"],
help="the mode parameter for SlidingWindowInferer.",
)
parser.add_argument(
"-num_samples",
"--num_samples",
type=int,
default=3,
help="the num_samples parameter of RandCropByPosNegLabeld.",
)
parser.add_argument(
"-pos_sample_num",
"--pos_sample_num",
type=int,
default=1,
help="the pos parameter of RandCropByPosNegLabeld.",
)
parser.add_argument(
"-neg_sample_num",
"--neg_sample_num",
type=int,
default=1,
help="the neg parameter of RandCropByPosNegLabeld.",
)
parser.add_argument(
"-cache_rate",
"--cache_rate",
type=float,
default=1.0,
help="the cache_rate parameter of CacheDataset.",
)
parser.add_argument("-learning_rate", "--learning_rate", type=float, default=1e-2)
parser.add_argument(
"-max_epochs",
"--max_epochs",
type=int,
default=1000,
help="number of epochs of training.",
)
parser.add_argument(
"-mode", "--mode", type=str, default="train", choices=["train", "val"]
)
parser.add_argument(
"-checkpoint",
"--checkpoint",
type=str,
default=None,
help="the filename of weights.",
)
parser.add_argument(
"-amp",
"--amp",
type=bool,
default=False,
help="whether to use automatic mixed precision.",
)
parser.add_argument(
"-lr_decay",
"--lr_decay",
type=bool,
default=False,
help="whether to use learning rate decay.",
)
parser.add_argument(
"-tta_val",
"--tta_val",
type=bool,
default=False,
help="whether to use test time augmentation.",
)
parser.add_argument(
"-batch_dice",
"--batch_dice",
type=bool,
default=False,
help="the batch parameter of DiceCELoss.",
)
parser.add_argument(
"-determinism_flag", "--determinism_flag", type=bool, default=False
)
parser.add_argument(
"-determinism_seed",
"--determinism_seed",
type=int,
default=0,
help="the seed used in deterministic training",
)
parser.add_argument(
"-multi_gpu",
"--multi_gpu",
type=bool,
default=False,
help="whether to use multiple GPUs for training.",
)
parser.add_argument("-local_rank", "--local_rank", type=int, default=0)
args = parser.parse_args()
if args.local_rank == 0:
print_config()
if args.mode == "train":
train(args)
elif args.mode == "val":
validation(args)