-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
198 lines (151 loc) · 6.45 KB
/
common.py
File metadata and controls
198 lines (151 loc) · 6.45 KB
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
#! /usr/bin/python
from configparser import ConfigParser
from monai.transforms import (
AsDiscrete,
AddChanneld,
Compose,
CropForegroundd,
LoadImaged,
Orientationd,
# RandCropByPosNegLabeld,
# RandAffined,
ScaleIntensityRanged,
ScaleIntensityRangePercentilesd,
Spacingd,
ToTensord,
)
from monai.utils import first, set_determinism
from monai.networks.nets import UNet
from monai.networks.layers import Norm
import glob
import os
import shutil
from monai.data import CacheDataset, DataLoader, Dataset
#--------------------------------------------------------------------------------
# Load configurations
#--------------------------------------------------------------------------------
class Param():
def __init__(self, filename='config.ini'):
self.config = ConfigParser()
self.config.read(filename)
self.readParameters()
def getvector(self, config, section, key):
value = config.get(section, key)
if value:
value = value.split(',')
value = [float(s) for s in value]
value = tuple(value)
return value
else:
return None
def readParameters(self):
self.data_dir = self.config.get('common', 'data_dir')
self.root_dir = self.config.get('common', 'root_dir')
self.pixel_dim = self.getvector(self.config, 'common', 'pixel_dim')
if self.pixel_dim == None:
self.pixel_dim = (1.0,1.0,1.0)
self.window_size = self.getvector(self.config, 'common', 'window_size')
if self.window_size:
self.window_size = [int(s) for s in self.window_size]
self.window_size = tuple(self.window_size)
else:
self.window_size = (160,160,160)
self.pixel_intensity_min = self.config.getfloat('common', 'pixel_intensity_min')
self.pixel_intensity_max = self.config.getfloat('common', 'pixel_intensity_max')
self.pixel_intensity_percentile_min = self.config.getfloat('common', 'pixel_intensity_percentile_min')
self.pixel_intensity_percentile_max = self.config.getfloat('common', 'pixel_intensity_percentile_max')
self.model_file = self.config.get('common', 'model_file')
class TrainingParam(Param):
def __init__(self, filename='config.ini'):
super().__init__(filename)
def readParameters(self):
super().readParameters()
self.use_tensorboard = int(self.config.get('training', 'use_tensorboard'))
self.use_matplotlib = int(self.config.get('training', 'use_matplotlib'))
self.max_epochs = int(self.config.get('training', 'max_epochs'))
self.training_device_name = self.config.get('training', 'training_device_name')
class InferenceParam(Param):
def __init__(self, filename='config.ini'):
super().__init__(filename)
def readParameters(self):
super().readParameters()
self.inference_device_name = self.config.get('inference', 'inference_device_name')
#--------------------------------------------------------------------------------
# Load Transforms
#--------------------------------------------------------------------------------
def loadValidationTransforms(param):
val_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
Spacingd(keys=["image", "label"], pixdim=param.pixel_dim, mode=("bilinear", "nearest")),
Orientationd(keys=["image", "label"], axcodes="LPS"),
ScaleIntensityRanged(
keys=["image"], a_min=param.pixel_intensity_min, a_max=param.pixel_intensity_max,
b_min=0.0, b_max=1.0, clip=True,
),
# ScaleIntensityRangePercentilesd(
# keys=["image"], lower=param.pixel_intensity_percentile_min, upper=param.pixel_intensity_percentile_max,
# b_min=0.0, b_max=1.0, clip=True,
# ),
CropForegroundd(keys=["image", "label"], source_key="image"),
ToTensord(keys=["image", "label"]),
]
)
return val_transforms
import torch
def loadInferenceTransforms(param):
val_transforms = Compose(
[
LoadImaged(keys=["image"]),
AddChanneld(keys=["image"]),
Spacingd(keys=["image"], pixdim=param.pixel_dim, mode=("bilinear")),
Orientationd(keys=["image"], axcodes="LPS"),
ScaleIntensityRanged(
keys=["image"], a_min=param.pixel_intensity_min, a_max=param.pixel_intensity_max,
b_min=0.0, b_max=1.0, clip=True,
),
# ScaleIntensityRangePercentilesd(
# keys=["image"], lower=param.pixel_intensity_percentile_min, upper=param.pixel_intensity_percentile_max,
# b_min=0.0, b_max=1.0, clip=True,
# ),
CropForegroundd(keys=["image"], source_key="image"),
ToTensord(keys=["image"]),
]
)
return val_transforms
#--------------------------------------------------------------------------------
# Generate a file list
#--------------------------------------------------------------------------------
def generateLabeledFileList(param, prefix):
print('Reading labeled images from: ' + param.data_dir)
images = sorted(glob.glob(os.path.join(param.data_dir, prefix + "_images", "*.nii.gz")))
labels = sorted(glob.glob(os.path.join(param.data_dir, prefix + "_labels", "*.nii.gz")))
data_dicts = [
{"image": image_name, "label": label_name}
for image_name, label_name in zip(images, labels)
]
return data_dicts
def generateFileList(srcdir):
print('Reading images from: ' + srcdir)
images = sorted(glob.glob(os.path.join(srcdir, "*.nii.gz")))
data_dicts = [
{"image": image_name} for image_name in images
]
return data_dicts
#--------------------------------------------------------------------------------
# Model
#--------------------------------------------------------------------------------
def setupModel():
model_unet = UNet(
dimensions=3,
in_channels=1,
out_channels=2,
channels=(16, 32, 64, 128, 256),
strides=(2, 2, 2, 2),
num_res_units=2,
norm=Norm.BATCH,
)
post_pred = AsDiscrete(argmax=True, to_onehot=True, n_classes=2)
post_label = AsDiscrete(to_onehot=True, n_classes=2)
return (model_unet, post_pred, post_label)