-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
293 lines (254 loc) · 10.3 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable, Function
import torch.nn.modules.normalization as norms
import scipy.spatial.distance as distances
import numpy as np
import pdb
import SimpleITK as sitk
## ------------------ Network components -------------------------
class unetConv3(nn.Module):
def __init__(self, in_size, out_size, is_batchnorm):
super(unetConv3, self).__init__()
if is_batchnorm:
if in_size<64:
nGrps = 4
else:
nGrps = 10#16
self.conv1 = nn.Sequential(nn.Conv3d(in_size, out_size, 3, 1, 1),
norms.GroupNorm(nGrps,out_size),
nn.ReLU(),)
self.conv2 = nn.Sequential(nn.Conv3d(out_size, out_size, 3, 1, 1),
norms.GroupNorm(nGrps,out_size),
nn.ReLU(),)
# self.conv3 = nn.Sequential(nn.Conv3d(out_size, out_size, 3, 1, 1),
# norms.GroupNorm(nGrps,out_size),
# nn.ReLU(),)
else:
self.conv1 = nn.Sequential(nn.Conv3d(in_size, out_size, 3, 1, 2),
nn.ReLU(),)
self.conv2 = nn.Sequential(nn.Conv3d(out_size, out_size, 3, 1, 2),
nn.ReLU(),)
def forward(self, inputs):
outputs = self.conv1(inputs)
outputs = self.conv2(outputs)
# outputs = self.conv3(outputs)
return outputs
class Combiner(nn.Module):
'''
Combines outputs of two layers by padding (if needed) and concatenation.
'''
def __init__(self):
super(Combiner,self).__init__()
def getPadding(self,offset):
if offset%2==0:
padding = 2*[np.sign(offset)*(np.abs(offset) // 2)]
else:
padding = [np.sign(offset)*(np.abs(offset) // 2),np.sign(offset)*( (np.abs(offset) // 2) + 1)]
return padding
def forward(self,input1,input2):
'''
input1 - from decoder ; input2 - from encoder.
'''
offset1 = input2.size()[2] - input1.size()[2]
padding1 = self.getPadding(offset1)
offset2 = input2.size()[3] - input1.size()[3]
padding2 = self.getPadding(offset2)
padding = padding2+padding1
output1 = F.pad(input1, padding)
return torch.cat([output1, input2], 1)
class Transition(nn.Module):
def __init__(self, nChannels, nOutChannels):
super(Transition, self).__init__()
if nChannels<64:
nGrps = 4
else:
nGrps = 10#16
self.bn1 = norms.GroupNorm(nGrps,nChannels)
# self.bn1 = nn.BatchNorm2d(nChannels)
self.conv1 = nn.Conv3d(nChannels, nOutChannels, kernel_size=1,
bias=False)
self.dnConv1 = nn.Conv3d(nOutChannels,nOutChannels,2,stride=2)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = self.dnConv1(out)
# out = F.avg_pool2d(out, 2)
return out
class SingleLayer(nn.Module):
def __init__(self, nChannels, growthRate):
super(SingleLayer, self).__init__()
# self.bn1 = nn.BatchNorm2d(nChannels)
if nChannels<64:
nGrps = 4
else:
nGrps = 8#16
self.bn1 = norms.GroupNorm(nGrps,nChannels)
self.conv1 = nn.Conv3d(nChannels, growthRate, kernel_size=3,
padding=1, bias=False)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = torch.cat((x, out), 1)
return out
class Bottleneck(nn.Module):
def __init__(self, nChannels, growthRate):
super(Bottleneck, self).__init__()
interChannels = 4*growthRate
self.bn1 = nn.BatchNorm2d(nChannels)
self.conv1 = nn.Conv2d(nChannels, interChannels, kernel_size=1,
bias=True)
self.bn2 = nn.BatchNorm2d(interChannels)
self.conv2 = nn.Conv2d(interChannels, growthRate, kernel_size=3,
padding=0, bias=True)
def forward(self, x):
out = self.conv1(F.relu(self.bn1(x)))
out = self.conv2(F.relu(self.bn2(out)))
out = F.pad(out,(1,1,1,1),mode='replicate')
out = torch.cat((x, out), 1)
return out
def _make_dense(nChannels, growthRate, nDenseBlocks, bottleneck):
layers = []
for i in range(int(nDenseBlocks)):
if bottleneck:
layers.append(Bottleneck(nChannels, growthRate))
else:
layers.append(SingleLayer(nChannels, growthRate))
nChannels += growthRate
return nn.Sequential(*layers)
def toCategorical(batch_size,yArr,nClasses,dims):
if dims==3:
y_OH = torch.FloatTensor(batch_size,nClasses,yArr.shape[2],yArr.shape[3],yArr.shape[4])
elif dims==1:
y_OH = torch.FloatTensor(batch_size,nClasses)
y_OH.zero_()
y_OH.scatter_(1,yArr,1)
return y_OH
## ------------------ Losses -------------------------
def contrastiveLoss(a,b,y,m,reduction='sum',gpuID=0):
'''
Contrastive loss LC=∑N,n=1 (y)d^2+ (1−y) max(0,m−d)^2 where d is L2 distance b/w a and b
a, b = final embeddings of the pair
y = label
m = margin ; If distance is greater than margin, loss becomes 0 for negative pair
'''
d = torch.norm(a-b)
loss = y*(d**2) + (1-y)*torch.max(torch.Tensor([0]).cuda(gpuID), (m-d) )**2
if reduction=='sum':
loss = torch.sum(loss)
return loss
class myBCELoss(nn.Module):
'''
Weighted Binary cross entropy loss. Tested.
'''
def __init__(self,weight):
super(myBCELoss,self).__init__()
self.weight = weight
def forward(self,inputs,target):
normVal = 1e-24
# target = target[:,1,:,:,:]
# inputs = inputs[:,1,:,:,:]
weights = 1 + (self.weight-1)*target # to make pos wt as self.weight and others as 1
loss = -((weights*target)*inputs.clamp(min=normVal).log()+(1-target)*(1-inputs).clamp(min=normVal).log()).mean()
return loss
class DiceCoeff(Function):
"""Dice coeff for individual examples"""
def forward(self, input, target):
self.save_for_backward(input, target)
eps = 0.0001
self.inter = torch.dot(input.view(-1), target.view(-1))
self.union = torch.sum(input) + torch.sum(target) + eps
t = (2 * self.inter.float() + eps) / self.union.float()
return t
# This function has only a single output, so it gets only one gradient
def backward(self, grad_output):
input, target = self.saved_variables
grad_input = grad_target = None
if self.needs_input_grad[0]:
grad_input = grad_output * 2 * (target * self.union - self.inter) \
/ (self.union * self.union)
if self.needs_input_grad[1]:
grad_target = None
return grad_input, grad_target
def dice_coeff(input, target):
"""Dice coeff for batches"""
if input.is_cuda:
s = torch.FloatTensor(1).cuda().zero_()
else:
s = torch.FloatTensor(1).zero_()
for i, c in enumerate(zip(input, target)):
s = s + DiceCoeff().forward(c[0], c[1])
return s / (i + 1)
def tversky_loss(beta, y_true, y_pred):
numerator = torch.sum(y_true * y_pred)
denominator = y_true * y_pred + beta * (1 - y_true) * y_pred + (1 - beta) * y_true * (1 - y_pred)
return 1 - (numerator + 1) / (torch.sum(denominator) + 1)
## ------------------ Metrics -------------------------
def integralDice(pred,gt,k):
'''
Dice coefficient for multiclass hard thresholded prediction consisting of integers instead of binary
values. k = integer for class for which Dice is being calculated.
'''
return torch.sum(pred[gt==k]==k)*2.0 / (torch.sum(pred[pred==k]==k) + torch.sum(gt[gt==k]==k)).float()
def globalAcc(predList,labelList):
'''
Compute accuracy over all samples using list of predictions and labels.
'''
predList = torch.cat(predList)
labelList = torch.cat(labelList)
acc = torch.sum(predList==labelList).float()/( predList.shape[0] )
return acc
def getHausdorff(pred,label):
u = np.where(pred.detach().cpu().numpy())
v = np.where(label.detach().cpu().numpy())
hd_u_v = distances.directed_hausdorff(u, v)[0]
hd_v_u = distances.directed_hausdorff(v, u)[0]
hd = max(hd_u_v,hd_v_u) / u[0].shape[0]
return hd
## ------------------ Aux -------------------------
def saveVolume(vol,fileName):
writer = sitk.ImageFileWriter()
writer.SetFileName(fileName)
if isinstance(vol,torch.Tensor):
if vol.requires_grad:
vol = vol.detach()
if vol.is_cuda:
vol = vol.cpu()
vol = vol.numpy()
if vol.dtype=='int64':
vol = vol.astype('uint8')
writer.Execute(sitk.GetImageFromArray(vol.swapaxes(0,2)))
def compare_models(model_1, model_2):
'''
Copied from https://discuss.pytorch.org/t/check-if-models-have-same-weights/4351/6
'''
models_differ = 0
for key_item_1, key_item_2 in zip(model_1.state_dict().items(), model_2.state_dict().items()):
if torch.equal(key_item_1[1], key_item_2[1]):
pass
else:
models_differ += 1
if (key_item_1[0] == key_item_2[0]): # if names are same but weights are different
print('Mismatch found at', key_item_1[0])
else:
print(key_item_1[0]+' layer found in model 1 but layer '+key_item_2[0]+' found in model 2 at this place.')
# raise Exception
if models_differ == 0:
print('Models match perfectly! :)')
def getClassWts(nBatches,trainGenObj):
wtList = []
ct = 0
for i in range(1,2):
weight = 0
for j in range(nBatches):
vol,labels,case,_ = trainGenObj.__next__()
# if 2 in torch.unique(labels):
wt = torch.sum(labels[0,0,:,:,:]==i).float() / (labels.shape[2]*labels.shape[3]*labels.shape[4])
weight+=(1/wt)
ct+=1
# print(case+': '+str(1/wt)+' Average so far: '+str(weight.float()/j))
pdb.set_trace()
avWt = weight/float(ct)#float(j-1)#285
wtList.append(avWt)
# timeTaken = time.time() - initTime
print(wtList)
return wtList