-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinference.py
232 lines (202 loc) · 7.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from __future__ import annotations
import os.path
from tqdm import tqdm
from collections import OrderedDict
import torch
from torch import nn
from torch.utils.data import DataLoader
import numpy as np
from scipy.ndimage import zoom
from medpy import metric
from loguru import logger
from utils import CLASS_COLOR_MAPS
from plot import save_x_y, save_x_y_hat, class2colormap
from typing import Any
def calc_metric_per_case(pred: np.ndarray, gt: np.ndarray) -> tuple[float, float, float, float]:
"""
input ndarray shape:
pred: [depth, height, width]; gt: [depth, height, width]
output float: (dice, hd95, jaccard, asd)
"""
pred[pred > 0] = 1
gt[gt > 0] = 1
if pred.sum() > 0 and gt.sum() > 0:
dice = metric.binary.dc(pred, gt)
hd95 = metric.binary.hd95(pred, gt)
jaccard = metric.binary.jc(pred, gt)
asd = np.mean([metric.binary.asd(pred, gt), metric.binary.asd(gt, pred)])
return dice, hd95, jaccard, asd
elif pred.sum() > 0 and gt.sum() == 0:
return 1, 0, 1, 0
else:
return 0, 0, 0, 0
def test_single_volume(
model: nn.Module,
volume: torch.Tensor,
label: torch.Tensor,
case_name: str,
num_classes: int = 9,
patch_size: list[int] = (224, 224),
deep_supervision: bool = False,
device: str = "cuda:0",
output_folder: str = "testing",
**kwargs: Any
) -> list[tuple[float, float, float, float]]:
"""
input tensor shape:
image: [1, depth, height, width]; label: [1, depth, height, width]
output list: [(dice, hd95, jaccard, asd), ...]
"""
volume = volume.squeeze(0).cpu().detach().numpy()
label = label.squeeze(0).cpu().detach().numpy()
logger.info("Predicting...")
prediction = np.zeros_like(label)
for depth in tqdm(range(volume.shape[0])):
image_slice = volume[depth, :, :]
h, w = image_slice.shape
if h != patch_size[0] or w != patch_size[1]:
image_slice = zoom(image_slice, (patch_size[0] / h, patch_size[1] / w), order=3)
if kwargs.get("norm_x_transform", None) is not None:
input = kwargs.get("norm_x_transform")(image_slice)
else:
input = torch.from_numpy(image_slice).unsqueeze(0)
input = input.unsqueeze(0).float().to(device)
model.eval()
with torch.no_grad():
outputs = model(input)
if deep_supervision: outputs = sum(outputs)
out = torch.argmax(torch.softmax(outputs, dim=1), dim=1).squeeze(0)
out = out.cpu().detach().numpy()
if h != patch_size[0] or w != patch_size[1]:
pred = zoom(out, (h / patch_size[0], w / patch_size[1]), order=0)
else:
pred = out
prediction[depth] = pred
save_x_y(
x=(volume[depth, :, :] * 255).astype(np.uint8),
y=label[depth, :, :].astype(np.uint8),
colormap=class2colormap[num_classes],
out=os.path.join(output_folder, f"{case_name}_{depth}_gt.png")
)
save_x_y_hat(
x=(volume[depth, :, :] * 255).astype(np.uint8),
y=label[depth, :, :].astype(np.uint8),
y_hat=pred.astype(np.uint8),
colormap=class2colormap[num_classes],
out=os.path.join(output_folder, f"{case_name}_{depth}_pd.png")
)
logger.info("Evaluating...")
metrics = []
for class_id in tqdm(range(1, num_classes)):
# noinspection PyTypeChecker
metrics.append(calc_metric_per_case(prediction == class_id, label == class_id))
return metrics
def inference(
model: nn.Module,
dataloader: DataLoader,
num_classes: int = 9,
patch_size: list[int] = (224, 224),
deep_supervision: bool = False,
output_folder: str = "testing",
device: str = "cuda:0",
**kwargs: Any,
) -> None:
logger.info(f"Testing iterations: {len(dataloader)}")
os.makedirs(output_folder, exist_ok=True)
metric_list = 0.0
for sample in tqdm(dataloader):
# noinspection PyTypeChecker
image, label, case_name = sample["image"], sample["label"], sample['case_name'][0]
metric_per_case = test_single_volume(
model=model,
volume=image,
label=label,
num_classes=num_classes,
case_name=case_name,
patch_size=patch_size,
deep_supervision=deep_supervision,
device=device,
output_folder=output_folder,
**kwargs
)
# per case
metric_list += np.array(metric_per_case)
# noinspection PyTypeChecker
mean_metric = np.mean(metric_per_case, axis=0)
logger.info(f"case_name: {case_name} "
f"mean_dice: {mean_metric[0]}, "
f"mean_hd95: {mean_metric[1]}, "
f"mean_jacquard: {mean_metric[2]}, "
f"mean_asd: {mean_metric[3]}")
# per class
metric_list = metric_list / len(dataloader)
for class_name, (i, _) in CLASS_COLOR_MAPS[num_classes].items():
logger.info(f"class_name: {class_name} "
f"mean_dice: {metric_list[i - 1][0]}, "
f"mean_hd95: {metric_list[i - 1][1]}, "
f"mean_jacquard: {metric_list[i - 1][2]}, "
f"mean_asd: {metric_list[i - 1][3]}")
# per metric
mean_dice = np.mean(metric_list, axis=0)[0]
mean_hd95 = np.mean(metric_list, axis=0)[1]
mean_jacquard = np.mean(metric_list, axis=0)[2]
mean_asd = np.mean(metric_list, axis=0)[3]
logger.info(f"Testing performance: "
f"mean_dice: {mean_dice}, "
f"mean_hd95: {mean_hd95}, "
f"mean_jacquard: {mean_jacquard}, "
f"mean_asd: {mean_asd}")
def get_model(ckpt: str, **kwargs: Any) -> nn.Module:
from model import build_model
state_dict = OrderedDict()
for k, v in torch.load(ckpt, map_location="cpu")["state_dict"].items():
state_dict[k.replace("_model.", "")] = v
model = build_model(**kwargs)
model.load_state_dict(state_dict)
logger.info(f"Loaded model checkpoint: {ckpt}")
return model
def test_acdc(ckpt: str) -> None:
from dataset_acdc import ACDCDataset
from torchvision.transforms import transforms
norm_x_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
output_folder = "testing_acdc"
logger.add(os.path.join(output_folder, "testing.log"))
device = "cuda:0"
model = get_model(ckpt=ckpt, in_channels=3, num_classes=4).to(device)
dataset = ACDCDataset(base_dir="dataset/acdc", split="test")
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=0)
inference(
model=model,
dataloader=dataloader,
num_classes=4,
output_folder=output_folder,
device=device,
norm_x_transform=norm_x_transform,
)
def test_synapse(ckpt: str) -> None:
from dataset_synapse import SynapseDataset
from torchvision.transforms import transforms
norm_x_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
output_folder = "testing_synapse"
logger.add(os.path.join(output_folder, "testing.log"))
device = "cuda:0"
model = get_model(ckpt=ckpt, in_channels=3, num_classes=9).to(device)
dataset = SynapseDataset(base_dir="dataset/synapse/test_vol", split="test_vol")
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=0)
inference(
model=model,
dataloader=dataloader,
num_classes=9,
output_folder=output_folder,
device=device,
norm_x_transform=norm_x_transform,
)
if __name__ == '__main__':
# test_synapse(ckpt="log/msvm-unet-synapse/checkpoints/epoch=259-val_mean_dice=0.8500.ckpt")
test_acdc(ckpt="log/msvm-unet-acdc/checkpoints/epoch.219-val_mean_dice.0.9258.ckpt")