-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
136 lines (120 loc) · 5.42 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
# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from functools import partial
import numpy as np
import paddle
from monai.inferers import sliding_window_inference
from monai.losses import DiceCELoss
from monai.metrics import DiceMetric
from monai.transforms import AsDiscrete
from monai.utils.enums import MetricReduction
from networks.unetr import UNETR
from optimizers.lr_scheduler import LinearWarmupCosineAnnealingLR
from trainer import run_training
from utils.data_utils import get_loader
import opts
import paddle.optimizer as optim
def main(args):
args.logdir = './runs/' + args.logdir
np.set_printoptions(formatter={'float': '{: 0.3f}'.format}, suppress=True)
args.test_mode = False
loader = get_loader(args)
if args.rank == 0:
print('Batch size is:', args.batch_size, 'epochs', args.max_epochs)
inf_size = [args.roi_x, args.roi_y, args.roi_x]
pretrained_dir = args.pretrained_dir
if (args.model_name is None) or args.model_name == 'unetr':
model = UNETR(
in_channels=args.in_channels,
out_channels=args.out_channels,
img_size=(args.roi_x, args.roi_y, args.roi_z),
feature_size=args.feature_size,
hidden_size=args.hidden_size,
mlp_dim=args.mlp_dim,
num_heads=args.num_heads,
pos_embed=args.pos_embed,
norm_name=args.norm_name,
conv_block=True,
res_block=True,
dropout_rate=args.dropout_rate)
if args.resume_ckpt:
model_dict = paddle.load(os.path.join(pretrained_dir, args.pretrained_model_name))
model.load_state_dict(model_dict)
print('Use pretrained weights')
else:
raise ValueError('Unsupported model ' + str(args.model_name))
dice_loss = DiceCELoss(to_onehot_y=True,
softmax=True,
squared_pred=True,
smooth_nr=args.smooth_nr,
smooth_dr=args.smooth_dr)
post_label = AsDiscrete(to_onehot=True,
num_classes=args.out_channels)
post_pred = AsDiscrete(argmax=True,
to_onehot=True,
num_classes=args.out_channels)
dice_acc = DiceMetric(include_background=True,
reduction=MetricReduction.MEAN,
get_not_nans=True)
model_inferer = partial(sliding_window_inference,
roi_size=inf_size,
sw_batch_size=args.sw_batch_size,
predictor=model,
overlap=args.infer_overlap)
pytorch_total_params = sum(p.numel() for p in model.parameters() if not p.stop_gradient)
print('Total parameters count', pytorch_total_params)
best_acc = 0
start_epoch = 0
if args.checkpoint is not None:
checkpoint = paddle.load(args.checkpoint)
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
new_state_dict[k.replace('backbone.', '')] = v
model.load_dict(new_state_dict)
if 'epoch' in checkpoint:
start_epoch = checkpoint['epoch'] + 1
if 'best_acc' in checkpoint:
best_acc = checkpoint['best_acc']
print("=> loaded checkpoint '{}' (epoch {}) (bestacc {})".format(args.checkpoint, start_epoch, best_acc))
if args.lrschedule == 'warmup_cosine':
scheduler = LinearWarmupCosineAnnealingLR(args.optim_lr,
warmup_epochs=args.warmup_epochs,
max_epochs=args.max_epochs)
else:
scheduler = args.optim_lr
if args.optim_name == 'adam':
optimizer = optim.Adam(learning_rate=scheduler,
parameters=model.parameters(),
weight_decay=args.reg_weight)
elif args.optim_name == 'adamw':
optimizer = optim.AdamW(learning_rate=scheduler,
parameters=model.parameters(),
weight_decay=args.reg_weight)
else:
raise ValueError('Unsupported Optimization Procedure: ' + str(args.optim_name))
accuracy = run_training(model=model,
train_loader=loader[0],
val_loader=loader[1],
optimizer=optimizer,
loss_func=dice_loss,
acc_func=dice_acc,
args=args,
model_inferer=model_inferer,
scheduler=scheduler,
start_epoch=start_epoch,
post_label=post_label,
post_pred=post_pred)
return accuracy
if __name__ == '__main__':
args = opts.main_opt()
main(args)