forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
204 lines (189 loc) · 5.83 KB
/
inference.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
# Copyright (c) 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 argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
import torch
import torch.distributed as dist
from monai.inferers import SlidingWindowInferer
from torch.nn.parallel import DistributedDataParallel
from create_dataset import get_data
from create_network import get_network
from inferrer import DynUNetInferrer
from task_params import patch_size, task_name
def inference(args):
# load hyper parameters
task_id = args.task_id
checkpoint = args.checkpoint
val_output_dir = "./runs_{}_fold{}_{}/".format(args.task_id, args.fold, args.expr_name)
sw_batch_size = args.sw_batch_size
infer_output_dir = os.path.join(val_output_dir, task_name[task_id])
window_mode = args.window_mode
eval_overlap = args.eval_overlap
amp = args.amp
tta_val = args.tta_val
multi_gpu_flag = args.multi_gpu
local_rank = args.local_rank
if not os.path.exists(infer_output_dir):
os.makedirs(infer_output_dir)
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, test_loader = get_data(args, mode="test")
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)
net.eval()
inferrer = DynUNetInferrer(
device=device,
val_data_loader=test_loader,
network=net,
output_dir=infer_output_dir,
num_classes=len(properties["labels"]),
inferer=SlidingWindowInferer(
roi_size=patch_size[task_id],
sw_batch_size=sw_batch_size,
overlap=eval_overlap,
mode=window_mode,
),
amp=amp,
tta_val=tta_val,
)
inferrer.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="02", 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(
"-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(
"-tta_val",
"--tta_val",
type=bool,
default=False,
help="whether to use test time augmentation.",
)
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()
inference(args)