-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·216 lines (193 loc) · 7.67 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
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
import logging
import os
import shutil
from typing import Dict
from monai.networks.nets import BasicUNet
from monailabel.interfaces.app import MONAILabelApp
from monailabel.interfaces.tasks.infer import InferTask, InferType
from monailabel.interfaces.tasks.strategy import Strategy
from monailabel.tasks.activelearning.random import Random
from lib.infer import InferCovid
from lib.scribbles import (
InteractiveGraphCut,
OnlineAdaptiveMONetBasedGraphCut,
OnlineECONetBasedGraphCut,
)
logger = logging.getLogger(__name__)
class MyApp(MONAILabelApp):
def __init__(self, app_dir, studies, conf):
self.network_covid = BasicUNet(
dimensions=3,
in_channels=1,
out_channels=2,
features=(32, 32, 64, 128, 256, 32),
dropout=0.1,
)
self.model_dir = os.path.join(app_dir, "model")
self.pretrained_model_covid = os.path.join(
self.model_dir, "pretrained_covid.pt"
)
if not os.path.exists(self.pretrained_model_covid):
raise IOError(
"COVID segmentation model not found at {}\n"
"Please download and place in above location".format(
self.pretrained_model_covid
)
)
self.final_model_covid = os.path.join(self.model_dir, "model_covid.pt")
self.singlescale_econet_pretrained_path = (
"./model/econet_singlescale_model=[72.44].pt"
)
if not os.path.exists(self.singlescale_econet_pretrained_path):
raise IOError(
"Missing pretrained files for online model {}".format(
self.singlescale_econet_pretrained_path
)
)
self.current_ss_econet_model_path = "./model/ss_econet_current_sample_temp.pt"
if os.path.exists(self.current_ss_econet_model_path):
logging.info(
"clearing old online model at: {}".format(
self.current_ss_econet_model_path
)
)
os.unlink(self.current_ss_econet_model_path)
shutil.copy(
self.singlescale_econet_pretrained_path,
self.current_ss_econet_model_path,
)
self.multiscale_monet_pretrained_path = (
"./model/econet_multiscale_model=[78.53].pt"
)
if not os.path.exists(self.multiscale_monet_pretrained_path):
raise IOError(
"Missing pretrained files for online model {}".format(
self.multiscale_monet_pretrained_path
)
)
self.current_ms_monet_model_path = "./model/ms_monet_current_sample_temp.pt"
if os.path.exists(self.current_ms_monet_model_path):
logging.info(
"clearing old online model at: {}".format(
self.current_ms_monet_model_path
)
)
os.unlink(self.current_ms_monet_model_path)
shutil.copy(
self.multiscale_monet_pretrained_path, self.current_ms_monet_model_path
)
self.intensity_range_covid = (-1000, 400, 0.0, 1.0, True)
self.pix_dim = (2.0, 2.0, 1.0)
self.lamda = 2.5
self.sigma = 0.15
self.epochs = 200
self.tau = 0.3
super().__init__(
app_dir=app_dir,
studies=studies,
conf=conf,
name="Segmentation - Adaptive Online Likelihood Model",
description="Active Learning solution to label Online Likelihood Model over 3D CT Images",
)
def init_infers(self) -> Dict[str, InferTask]:
models = {
"COVID_3D_UNet_Segmentation": InferCovid(
[self.pretrained_model_covid, self.final_model_covid],
self.network_covid,
),
}
interactive_models = {}
interactive_models["MONet"] = OnlineAdaptiveMONetBasedGraphCut(
intensity_range=self.intensity_range_covid,
pix_dim=self.pix_dim,
model="MSFEAT",
loss="CE",
train_feat=True,
lamda=self.lamda,
sigma=self.sigma,
tau=self.tau,
model_path=self.current_ms_monet_model_path,
lr_scheduler="COSINE",
epochs=self.epochs,
)
interactive_models["MONet-NoMS"] = OnlineAdaptiveMONetBasedGraphCut(
intensity_range=self.intensity_range_covid,
pix_dim=self.pix_dim,
model="FEAT",
loss="CE",
train_feat=True,
lamda=self.lamda,
sigma=self.sigma,
tau=self.tau,
model_path=self.current_ss_econet_model_path,
lr_scheduler="COSINE",
epochs=self.epochs,
)
interactive_models["ECONet"] = OnlineECONetBasedGraphCut(
intensity_range=self.intensity_range_covid,
pix_dim=self.pix_dim,
model="FEAT",
loss="CE",
train_feat=True,
lamda=self.lamda,
sigma=self.sigma,
model_path=self.current_ss_econet_model_path,
lr_scheduler="COSINE",
epochs=self.epochs,
)
interactive_models["InteractiveGraphcut"] = InteractiveGraphCut(
intensity_range=self.intensity_range_covid,
pix_dim=self.pix_dim,
lamda=self.lamda,
sigma=self.sigma,
)
models.update(interactive_models)
return models
def init_strategies(self) -> Dict[str, Strategy]:
strategies: Dict[str, Strategy] = {}
strategies["random"] = Random()
return strategies
def infer(self, request, datastore=None):
image = request.get("image")
label = request.get("label")
# add saved logits into request
if self._infers[request.get("model")].type == InferType.SCRIBBLES:
saved_labels = self.datastore().get_labels_by_image_id(image)
for tag, label in saved_labels.items():
if tag == "logits":
request["logits"] = self.datastore().get_label_uri(label, tag)
logger.info(f"Updated request: {request}")
result = super().infer(request)
result_params = result.get("params")
# save logits
logits = result_params.get("logits")
if logits and self._infers[request.get("model")].type == InferType.SEGMENTATION:
self.datastore().save_label(image, logits, "logits", {})
os.unlink(logits)
result_params.pop("logits", None)
logger.info(f"Final Result: {result}")
return result
def next_sample(self, request):
# Reset MONet/MONet-NoMS/ECONet weights to pretrained weights on next sample
if os.path.exists(self.current_ms_monet_model_path):
logging.info(
"Resetting multi-scale online model for previous sample to pretrained model: {}".format(
self.current_ms_monet_model_path
)
)
os.unlink(self.current_ms_monet_model_path)
shutil.copy(
self.multiscale_monet_pretrained_path, self.current_ms_monet_model_path
)
if os.path.exists(self.current_ss_econet_model_path):
logging.info(
"Resetting single-scale online model for previous sample to pretrained model: {}".format(
self.current_ss_econet_model_path
)
)
os.unlink(self.current_ss_econet_model_path)
shutil.copy(
self.singlescale_econet_pretrained_path,
self.current_ss_econet_model_path,
)
return super().next_sample(request)