-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathsegmentation.py
107 lines (96 loc) · 3.48 KB
/
segmentation.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
# 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.
from typing import Callable, Sequence
from lib.transforms.transforms import GetCentroidsd
from monai.inferers import Inferer, SlidingWindowInferer
from monai.transforms import (
Activationsd,
AsDiscreted,
EnsureChannelFirstd,
EnsureTyped,
GaussianSmoothd,
KeepLargestConnectedComponentd,
LoadImaged,
NormalizeIntensityd,
Orientationd,
ScaleIntensityd,
Spacingd,
)
from monailabel.interfaces.tasks.infer_v2 import InferType
from monailabel.tasks.infer.basic_infer import BasicInferTask
from monailabel.transform.post import Restored
class Segmentation(BasicInferTask):
"""
This provides Inference Engine for pre-trained Segmentation (SegResNet) model.
"""
def __init__(
self,
path,
network=None,
target_spacing=(1.0, 1.0, 1.0),
type=InferType.SEGMENTATION,
labels=None,
dimension=3,
description="A pre-trained model for volumetric (3D) Segmentation from CT image",
**kwargs,
):
super().__init__(
path=path,
network=network,
type=type,
labels=labels,
dimension=dimension,
description=description,
load_strict=False,
**kwargs,
)
self.target_spacing = target_spacing
def pre_transforms(self, data=None) -> Sequence[Callable]:
t = [
LoadImaged(keys="image"),
EnsureTyped(keys="image", device=data.get("device") if data else None),
EnsureChannelFirstd(keys="image"),
Orientationd(keys="image", axcodes="RAS"),
Spacingd(keys="image", pixdim=self.target_spacing, allow_missing_keys=True),
NormalizeIntensityd(keys="image", nonzero=True),
GaussianSmoothd(keys="image", sigma=0.4),
ScaleIntensityd(keys="image", minv=-1.0, maxv=1.0),
]
return t
def inferer(self, data=None) -> Inferer:
return SlidingWindowInferer(
roi_size=self.roi_size,
sw_batch_size=2,
overlap=0.4,
padding_mode="replicate",
mode="gaussian",
)
def inverse_transforms(self, data=None):
return []
def post_transforms(self, data=None) -> Sequence[Callable]:
t = [
EnsureTyped(keys="image", device=data.get("device") if data else None),
Activationsd(keys="pred", softmax=True),
AsDiscreted(keys="pred", argmax=True),
]
if data and data.get("largest_cc", False):
t.append(KeepLargestConnectedComponentd(keys="pred"))
t.extend(
[
Restored(
keys="pred",
ref_image="image",
config_labels=self.labels if data.get("restore_label_idx", False) else None,
),
GetCentroidsd(keys="pred", centroids_key="centroids"),
]
)
return t