-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.py
191 lines (130 loc) · 5.63 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
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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision
import os
from progressBar import printProgressBar
import scipy.io as sio
from scipy import ndimage
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
class computeDiceOneHotBinary(nn.Module):
def __init__(self):
super(computeDiceOneHotBinary, self).__init__()
def dice(self, input, target):
inter = (input * target).float().sum()
sum = input.sum() + target.sum()
if (sum == 0).all():
return (2 * inter + 1e-8) / (sum + 1e-8)
return 2 * (input * target).float().sum() / (input.sum() + target.sum())
def inter(self, input, target):
return (input * target).float().sum()
def sum(self, input, target):
return input.sum() + target.sum()
def forward(self, pred, GT):
# pred is converted to 0 and 1
batchsize = GT.size(0)
DiceB = to_var(torch.zeros(batchsize, 2))
DiceF = to_var(torch.zeros(batchsize, 2))
for i in range(batchsize):
DiceB[i, 0] = self.inter(pred[i, 0], GT[i, 0])
DiceF[i, 0] = self.inter(pred[i, 1], GT[i, 1])
DiceB[i, 1] = self.sum(pred[i, 0], GT[i, 0])
DiceF[i, 1] = self.sum(pred[i, 1], GT[i, 1])
return DiceB, DiceF
def DicesToDice(Dices):
sums = Dices.sum(dim=0)
return (2 * sums[0] + 1e-8) / (sums[1] + 1e-8)
def getSingleImageBin(pred):
# input is a 2-channels image corresponding to the predictions of the net
# output is a gray level image (1 channel) of the segmentation with "discrete" values
n_channels = 2
Val = to_var(torch.zeros(2))
Val[1] = 1.0
x = predToSegmentation(pred)
out = x * Val.view(1, n_channels, 1, 1)
return out.sum(dim=1, keepdim=True)
def predToSegmentation(pred):
Max = pred.max(dim=1, keepdim=True)[0]
x = pred / Max
return (x == 1).float()
def getOneHotSegmentation(batch):
backgroundVal = 0
# IVD
label1 = 1.0
oneHotLabels = torch.cat((batch == backgroundVal, batch == label1),
dim=1)
return oneHotLabels.float()
def getTargetSegmentation(batch):
# input is 1-channel of values between 0 and 1
spineLabel = 1.0
return (batch / spineLabel).round().long().squeeze()
def saveImages(net, img_batch, batch_size, epoch, modelName):
path = '../Results/Images_PNG/' + modelName + '_'+ str(epoch)
if not os.path.exists(path):
os.makedirs(path)
total = len(img_batch)
net.eval()
softMax = nn.Softmax()
for i, data in enumerate(img_batch):
printProgressBar(i, total, prefix="Saving images.....", length=30)
image_f,image_i,image_o,image_w, labels, img_names = data
# Be sure here your image is betwen [0,1]
image_f=image_f.type(torch.FloatTensor)
image_i=image_i.type(torch.FloatTensor)
image_o=image_o.type(torch.FloatTensor)
image_w=image_w.type(torch.FloatTensor)
images = torch.cat((image_f,image_i,image_o,image_w),dim=1)
MRI = to_var(images)
image_f_var = to_var(image_f)
Segmentation = to_var(labels)
segmentation_prediction = net(MRI)
pred_y = softMax(segmentation_prediction)
segmentation = getSingleImageBin(pred_y)
imgname = img_names[0].split('/Fat/')
imgname = imgname[1].split('_fat.png')
out = torch.cat((image_f_var, segmentation, Segmentation*255))
torchvision.utils.save_image(out.data, os.path.join(path,imgname[0] + '.png'),
nrow=batch_size,
padding=2,
normalize=False,
range=None,
scale_each=False)
printProgressBar(total, total, done="Images saved !")
def inference(net, img_batch, batch_size, epoch):
total = len(img_batch)
Dice1 = torch.zeros(total, 2)
net.eval()
dice = computeDiceOneHotBinary().cuda()
softMax = nn.Softmax().cuda()
img_names_ALL = []
for i, data in enumerate(img_batch):
printProgressBar(i, total, prefix="[Inference] Getting segmentations...", length=30)
image_f,image_i,image_o,image_w, labels, img_names = data
# Be sure here your image is betwen [0,1]
image_f=image_f.type(torch.FloatTensor)/65535
image_i=image_i.type(torch.FloatTensor)/65535
image_o=image_o.type(torch.FloatTensor)/65535
image_w=image_w.type(torch.FloatTensor)/65535
images = torch.cat((image_f,image_i,image_o,image_w),dim=1)
img_names_ALL.append(img_names[0].split('/')[-1].split('.')[0])
MRI = to_var(images)
labels = labels.numpy()
idx=np.where(labels>0.0)
labels[idx]=1.0
labels = torch.from_numpy(labels)
labels = labels.type(torch.FloatTensor)
Segmentation = to_var(labels)
segmentation_prediction = net(MRI)
pred_y = softMax(segmentation_prediction)
Segmentation_planes = getOneHotSegmentation(Segmentation)
segmentation_prediction_ones = predToSegmentation(pred_y)
DicesN, Dices1 = dice(segmentation_prediction_ones, Segmentation_planes)
Dice1[i] = Dices1.data
printProgressBar(total, total, done="[Inference] Segmentation Done !")
ValDice1 = DicesToDice(Dice1)
return [ValDice1]