-
Notifications
You must be signed in to change notification settings - Fork 28
/
voxelmorph3d.py
286 lines (243 loc) · 11 KB
/
voxelmorph3d.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
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
use_gpu = torch.cuda.is_available()
class UNet(nn.Module):
def contracting_block(self, in_channels, out_channels, kernel_size=3):
"""
This function creates one contracting block
"""
block = torch.nn.Sequential(
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=in_channels, out_channels=out_channels, padding=1),
torch.nn.BatchNorm3d(out_channels),
torch.nn.ReLU(),
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=out_channels, out_channels=out_channels, padding=1),
torch.nn.BatchNorm3d(out_channels),
torch.nn.ReLU(),
)
return block
def expansive_block(self, in_channels, mid_channel, out_channels, kernel_size=3):
"""
This function creates one expansive block
"""
block = torch.nn.Sequential(
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=in_channels, out_channels=mid_channel, padding=1),
torch.nn.BatchNorm3d(mid_channel),
torch.nn.ReLU(),
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=mid_channel, out_channels=mid_channel, padding=1),
torch.nn.BatchNorm3d(mid_channel),
torch.nn.ReLU(),
torch.nn.ConvTranspose3d(in_channels=mid_channel, out_channels=out_channels, kernel_size=3, stride=2, padding=1, output_padding=1),
torch.nn.BatchNorm3d(out_channels),
torch.nn.ReLU(),
)
return block
def final_block(self, in_channels, mid_channel, out_channels, kernel_size=3):
"""
This returns final block
"""
block = torch.nn.Sequential(
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=in_channels, out_channels=mid_channel, padding=1),
torch.nn.BatchNorm3d(mid_channel),
torch.nn.ReLU(),
torch.nn.Conv3d(kernel_size=kernel_size, in_channels=mid_channel, out_channels=out_channels, padding=1),
torch.nn.BatchNorm3d(out_channels),
torch.nn.ReLU()
)
return block
def __init__(self, in_channel, out_channel):
super(UNet, self).__init__()
#Encode
self.conv_encode1 = self.contracting_block(in_channels=in_channel, out_channels=32)
self.conv_maxpool1 = torch.nn.MaxPool2d(kernel_size=2)
self.conv_encode2 = self.contracting_block(32, 64)
self.conv_maxpool2 = torch.nn.MaxPool2d(kernel_size=2)
self.conv_encode3 = self.contracting_block(64, 128)
self.conv_maxpool3 = torch.nn.MaxPool2d(kernel_size=2)
# Bottleneck
mid_channel = 128
self.bottleneck = torch.nn.Sequential(
torch.nn.Conv3d(kernel_size=3, in_channels=mid_channel, out_channels=mid_channel * 2, padding=1),
torch.nn.BatchNorm3d(mid_channel * 2),
torch.nn.ReLU(),
torch.nn.Conv3d(kernel_size=3, in_channels=mid_channel*2, out_channels=mid_channel, padding=1),
torch.nn.BatchNorm3d(mid_channel),
torch.nn.ReLU(),
torch.nn.ConvTranspose3d(in_channels=mid_channel, out_channels=mid_channel, kernel_size=3, stride=2, padding=1, output_padding=1),
torch.nn.BatchNorm3d(mid_channel),
torch.nn.ReLU(),
)
# Decode
self.conv_decode3 = self.expansive_block(256, 128, 64)
self.conv_decode2 = self.expansive_block(128, 64, 32)
self.final_layer = self.final_block(64, 32, out_channel)
def crop_and_concat(self, upsampled, bypass, crop=False):
"""
This layer crop the layer from contraction block and concat it with expansive block vector
"""
if crop:
c = (bypass.size()[2] - upsampled.size()[2]) // 2
bypass = F.pad(bypass, (-c, -c, -c, -c))
return torch.cat((upsampled, bypass), 1)
def forward(self, x):
# Encode
encode_block1 = self.conv_encode1(x)
encode_pool1 = self.conv_maxpool1(encode_block1)
encode_block2 = self.conv_encode2(encode_pool1)
encode_pool2 = self.conv_maxpool2(encode_block2)
encode_block3 = self.conv_encode3(encode_pool2)
encode_pool3 = self.conv_maxpool3(encode_block3)
# Bottleneck
bottleneck1 = self.bottleneck(encode_pool3)
# Decode
decode_block3 = self.crop_and_concat(bottleneck1, encode_block3)
cat_layer2 = self.conv_decode3(decode_block3)
decode_block2 = self.crop_and_concat(cat_layer2, encode_block2)
cat_layer1 = self.conv_decode2(decode_block2)
decode_block1 = self.crop_and_concat(cat_layer1, encode_block1)
final_layer = self.final_layer(decode_block1)
return final_layer
class SpatialTransformation(nn.Module):
def __init__(self, use_gpu=False):
self.use_gpu = use_gpu
super(SpatialTransformation, self).__init__()
def meshgrid(self, height, width):
x_t = torch.matmul(torch.ones([height, 1]), torch.transpose(torch.unsqueeze(torch.linspace(0.0, width -1.0, width), 1), 1, 0))
y_t = torch.matmul(torch.unsqueeze(torch.linspace(0.0, height - 1.0, height), 1), torch.ones([1, width]))
x_t = x_t.expand([height, width])
y_t = y_t.expand([height, width])
if self.use_gpu==True:
x_t = x_t.cuda()
y_t = y_t.cuda()
return x_t, y_t
def repeat(self, x, n_repeats):
rep = torch.transpose(torch.unsqueeze(torch.ones(n_repeats), 1), 1, 0)
rep = rep.long()
x = torch.matmul(torch.reshape(x, (-1, 1)), rep)
if self.use_gpu:
x = x.cuda()
return torch.squeeze(torch.reshape(x, (-1, 1)))
def interpolate(self, im, x, y):
im = F.pad(im, (0,0,1,1,1,1,0,0))
batch_size, height, width, channels = im.shape
batch_size, out_height, out_width = x.shape
x = x.reshape(1, -1)
y = y.reshape(1, -1)
x = x + 1
y = y + 1
max_x = width - 1
max_y = height - 1
x0 = torch.floor(x).long()
x1 = x0 + 1
y0 = torch.floor(y).long()
y1 = y0 + 1
x0 = torch.clamp(x0, 0, max_x)
x1 = torch.clamp(x1, 0, max_x)
y0 = torch.clamp(y0, 0, max_y)
y1 = torch.clamp(y1, 0, max_y)
dim2 = width
dim1 = width*height
base = self.repeat(torch.arange(0, batch_size)*dim1, out_height*out_width)
base_y0 = base + y0*dim2
base_y1 = base + y1*dim2
idx_a = base_y0 + x0
idx_b = base_y1 + x0
idx_c = base_y0 + x1
idx_d = base_y1 + x1
# use indices to lookup pixels in the flat image and restore
# channels dim
im_flat = torch.reshape(im, [-1, channels])
im_flat = im_flat.float()
dim, _ = idx_a.transpose(1,0).shape
Ia = torch.gather(im_flat, 0, idx_a.transpose(1,0).expand(dim, channels))
Ib = torch.gather(im_flat, 0, idx_b.transpose(1,0).expand(dim, channels))
Ic = torch.gather(im_flat, 0, idx_c.transpose(1,0).expand(dim, channels))
Id = torch.gather(im_flat, 0, idx_d.transpose(1,0).expand(dim, channels))
# and finally calculate interpolated values
x1_f = x1.float()
y1_f = y1.float()
dx = x1_f - x
dy = y1_f - y
wa = (dx * dy).transpose(1,0)
wb = (dx * (1-dy)).transpose(1,0)
wc = ((1-dx) * dy).transpose(1,0)
wd = ((1-dx) * (1-dy)).transpose(1,0)
output = torch.sum(torch.squeeze(torch.stack([wa*Ia, wb*Ib, wc*Ic, wd*Id], dim=1)), 1)
output = torch.reshape(output, [-1, out_height, out_width, channels])
return output
def forward(self, moving_image, deformation_matrix):
dx = deformation_matrix[:, :, :, 0]
dy = deformation_matrix[:, :, :, 1]
batch_size, height, width = dx.shape
x_mesh, y_mesh = self.meshgrid(height, width)
x_mesh = x_mesh.expand([batch_size, height, width])
y_mesh = y_mesh.expand([batch_size, height, width])
x_new = dx + x_mesh
y_new = dy + y_mesh
return self.interpolate(moving_image, x_new, y_new)
class VoxelMorph3d(nn.Module):
def __init__(self, in_channels=1, use_gpu=False):
super(VoxelMorph3d, self).__init__()
self.unet = UNet(in_channels, 3)
self.spatial_transform = SpatialTransformation(use_gpu)
if use_gpu:
self.unet = self.unet.cuda()
self.spatial_transform = self.spatial_transform.cuda()
def forward(self, moving_image, fixed_image):
x = torch.cat([moving_image, fixed_image], dim=3).permute(0,3,1,2)
deformation_matrix = self.unet(x).permute(0,2,3,1)
registered_image = self.spatial_transform(moving_image, deformation_matrix)
return registered_image
def cross_correlation_loss(I, J, n):
I = I.permute(0, 3, 1, 2)
J = J.permute(0, 3, 1, 2)
batch_size, channels, xdim, ydim = I.shape
I2 = torch.mul(I, I)
J2 = torch.mul(J, J)
IJ = torch.mul(I, J)
sum_filter = torch.ones((1, channels, n, n))
if use_gpu:
sum_filter = sum_filter.cuda()
I_sum = torch.conv2d(I, sum_filter, padding=1, stride=(1,1))
J_sum = torch.conv2d(J, sum_filter, padding=1 ,stride=(1,1))
I2_sum = torch.conv2d(I2, sum_filter, padding=1, stride=(1,1))
J2_sum = torch.conv2d(J2, sum_filter, padding=1, stride=(1,1))
IJ_sum = torch.conv2d(IJ, sum_filter, padding=1, stride=(1,1))
win_size = n**2
u_I = I_sum / win_size
u_J = J_sum / win_size
cross = IJ_sum - u_J*I_sum - u_I*J_sum + u_I*u_J*win_size
I_var = I2_sum - 2 * u_I * I_sum + u_I*u_I*win_size
J_var = J2_sum - 2 * u_J * J_sum + u_J*u_J*win_size
cc = cross*cross / (I_var*J_var + np.finfo(float).eps)
return torch.mean(cc)
def smooothing_loss(y_pred):
dy = torch.abs(y_pred[:, 1:, :, :] - y_pred[:, :-1, :, :])
dx = torch.abs(y_pred[:, :, 1:, :] - y_pred[:, :, :-1, :])
dx = torch.mul(dx, dx)
dy = torch.mul(dy, dy)
d = torch.mean(dx) + torch.mean(dy)
return d/2.0
def vox_morph_loss(y, ytrue, n=9, lamda=0.01):
cc = cross_correlation_loss(y, ytrue, n)
sm = smooothing_loss(y)
#print("CC Loss", cc, "Gradient Loss", sm)
loss = -1.0 * cc + lamda * sm
return loss
def dice_score(pred, target):
"""This definition generalize to real valued pred and target vector.
This should be differentiable.
pred: tensor with first dimension as batch
target: tensor with first dimension as batch
"""
top = 2 * torch.sum(pred * target, [1, 2, 3])
union = torch.sum(pred + target, [1, 2, 3])
eps = torch.ones_like(union) * 1e-5
bottom = torch.max(union, eps)
dice = torch.mean(top / bottom)
#print("Dice score", dice)
return dice