-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
151 lines (116 loc) · 4.41 KB
/
utils.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
from io import BytesIO
import torch
import torch.nn.functional as F
from torch.nn import DataParallel
import numpy as np
import os
import torchio as tio
from tqdm import tqdm
import uuid
from dataset import Datasets3D, read_param
from model import UNet
SPATIAL_DIMENSIONS = 2, 3, 4
def prediction(state_dict, num_classes, img_file, device, landmarks, batch_size):
subject_embedding = tio.Subject(
mri=tio.ScalarImage(img_file)
)
validation_transform = tio.Compose([
tio.ToCanonical(),
tio.Resample(4),
tio.CropOrPad((48, 60, 48)),
tio.HistogramStandardization({'mri': landmarks}),
tio.ZNormalization(masking_method=tio.ZNormalization.mean),
tio.OneHot(),
])
test_data = validation_transform(subject_embedding)
patch_size = 48, 48, 48 # we can user larger patches for inference
patch_overlap = 4, 4, 4
grid_sampler = tio.inference.GridSampler(
test_data,
patch_size,
patch_overlap,
)
patch_loader = torch.utils.data.DataLoader(
grid_sampler, batch_size=2*batch_size)
aggregator = tio.inference.GridAggregator(grid_sampler)
model = UNet(num_classes)
model.load_state_dict(state_dict)
if torch.cuda.is_available():
model = DataParallel(model)
model.to(device)
model.eval()
with torch.no_grad():
for patches_batch in patch_loader:
inputs = patches_batch['mri'][tio.DATA].to(device)
locations = patches_batch[tio.LOCATION]
probabilities = model(inputs).softmax(dim=1)
aggregator.add_batch(probabilities, locations)
output_tensor = aggregator.get_output_tensor()
output_tensor = torch.argmax(output_tensor, dim=0).unsqueeze(0)
affine = test_data.mri.affine
prediction = tio.LabelMap(tensor=output_tensor.short(), affine=affine)
spatial_transform = tio.Compose([
tio.ToCanonical(),
tio.Resample(4)
])
resample_shape = spatial_transform(tio.ScalarImage(img_file)).shape[-3:]
print(f"raw: {prediction}")
prediction = tio.CropOrPad(resample_shape)(prediction)
print(f"crop/pad: {prediction}")
prediction = tio.Resample(img_file, image_interpolation='nearest')(prediction)
print(prediction)
save_name = f'res_{uuid.uuid4().hex}.nii.gz'
prediction.save(save_name)
with open(save_name, "rb") as f:
buffered = BytesIO(f.read())
return buffered
def get_dice_score(output, target, epsilon=1e-9):
p0 = output
g0 = target
p1 = 1 - p0
g1 = 1 - g0
tp = (p0 * g0).sum(dim=SPATIAL_DIMENSIONS)
fp = (p0 * g1).sum(dim=SPATIAL_DIMENSIONS)
fn = (p1 * g0).sum(dim=SPATIAL_DIMENSIONS)
num = 2 * tp
denom = 2 * tp + fp + fn + epsilon
dice_score = num / denom
return dice_score
def get_dice_loss(output, target):
return 1 - get_dice_score(output, target)
def train_one_epoch(train_loader, model, device, optimizer):
epoch_losses = []
model.train()
for batch_idx, batch in enumerate(tqdm(train_loader)):
inputs = batch['mri'][tio.DATA].to(device)
targets = batch['brain'][tio.DATA].to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(True):
logits = model(inputs)
probabilities = F.softmax(logits, dim=1)
batch_losses = get_dice_loss(probabilities, targets)
batch_loss = batch_losses.mean()
batch_loss.backward()
optimizer.step()
epoch_losses.append(batch_loss.item())
epoch_losses = np.array(epoch_losses)
return epoch_losses.mean()
def valid(test_loader, model, device):
epoch_losses = []
val_acc = []
model.eval()
for batch_idx, batch in enumerate(tqdm(test_loader)):
inputs = batch['mri'][tio.DATA].to(device)
targets = batch['brain'][tio.DATA].to(device)
with torch.set_grad_enabled(False):
logits = model(inputs)
probabilities = F.softmax(logits, dim=1)
batch_losses = get_dice_loss(probabilities, targets)
dice_score = get_dice_score(probabilities, targets)
batch_loss = batch_losses.mean()
acc = dice_score.mean()
epoch_losses.append(batch_loss.item())
val_acc.append(acc.item())
epoch_losses = np.array(epoch_losses)
val_acc = np.array(val_acc)
return epoch_losses.mean(), val_acc.mean()