|
a |
|
b/network_reconstruction.py |
|
|
1 |
import torch |
|
|
2 |
from torch import nn |
|
|
3 |
import torch.nn.functional as F |
|
|
4 |
|
|
|
5 |
def relu(): |
|
|
6 |
return nn.ReLU(inplace=True) |
|
|
7 |
|
|
|
8 |
|
|
|
9 |
def conv(in_channels, out_channels, kernel_size=(3,3,3), stride=(1,1,1), padding = 1, nonlinearity = relu): |
|
|
10 |
conv_layer = nn.Conv3d(in_channels = in_channels, out_channels= out_channels, kernel_size = kernel_size, stride = stride, padding = padding, bias = False) |
|
|
11 |
|
|
|
12 |
nll_layer = nonlinearity() |
|
|
13 |
bn_layer = nn.BatchNorm3d(out_channels) |
|
|
14 |
|
|
|
15 |
layers = [conv_layer, bn_layer, nll_layer] |
|
|
16 |
return nn.Sequential(*layers) |
|
|
17 |
|
|
|
18 |
def deconv(in_channels, out_channels, kernel_size=(3,3,3), stride=(1,1,1), padding = 1, nonlinearity = relu): |
|
|
19 |
conv_layer = nn.ConvTranspose3d(in_channels = in_channels, out_channels= out_channels, kernel_size = kernel_size, stride = stride, padding = padding, output_padding = 1, bias = False) |
|
|
20 |
|
|
|
21 |
nll_layer = nonlinearity() |
|
|
22 |
bn_layer = nn.BatchNorm3d(out_channels) |
|
|
23 |
|
|
|
24 |
layers = [conv_layer, bn_layer, nll_layer] |
|
|
25 |
return nn.Sequential(*layers) |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
def conv_blocks_2(in_channels, out_channels, strides=(1,1,1)): |
|
|
29 |
conv1 = conv(in_channels, out_channels, stride = strides) |
|
|
30 |
conv2 = conv(out_channels, out_channels, stride=(1,1,1)) |
|
|
31 |
layers = [conv1, conv2] |
|
|
32 |
return nn.Sequential(*layers) |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
def conv_blocks_3(in_channels, out_channels, strides=(1,1,1)): |
|
|
36 |
conv1 = conv(in_channels, out_channels, stride = strides) |
|
|
37 |
conv2 = conv(out_channels, out_channels, stride=(1,1,1)) |
|
|
38 |
conv3 = conv(out_channels, out_channels, stride=(1,1,1)) |
|
|
39 |
layers = [conv1, conv2, conv3] |
|
|
40 |
return nn.Sequential(*layers) |
|
|
41 |
|
|
|
42 |
def fullyconnect(in_features, out_features, out_channels, nonlinearity = relu): |
|
|
43 |
fc_layer = nn.Linear(in_features = in_features, out_features= out_features, bias = False) |
|
|
44 |
|
|
|
45 |
nll_layer = nonlinearity() |
|
|
46 |
bn_layer = nn.BatchNorm1d(out_channels) |
|
|
47 |
|
|
|
48 |
layers = [fc_layer, bn_layer, nll_layer] |
|
|
49 |
return nn.Sequential(*layers) |
|
|
50 |
|
|
|
51 |
def conv_2D(in_channels, out_channels, kernel_size=3, stride=1, padding = 1, nonlinearity = relu): |
|
|
52 |
conv_layer = nn.Conv2d(in_channels = in_channels, out_channels= out_channels, kernel_size = kernel_size, stride = stride, padding = padding, bias = False) |
|
|
53 |
|
|
|
54 |
nll_layer = nonlinearity() |
|
|
55 |
bn_layer = nn.BatchNorm2d(out_channels) |
|
|
56 |
|
|
|
57 |
layers = [conv_layer, bn_layer, nll_layer] |
|
|
58 |
return nn.Sequential(*layers) |
|
|
59 |
|
|
|
60 |
def conv_1D(in_channels, out_channels, kernel_size=3, stride=1, padding = 1, nonlinearity = relu): |
|
|
61 |
conv_layer = nn.Conv1d(in_channels = in_channels, out_channels= out_channels, kernel_size = kernel_size, stride = stride, padding = padding, bias = False) |
|
|
62 |
|
|
|
63 |
nll_layer = nonlinearity() |
|
|
64 |
bn_layer = nn.BatchNorm2d(out_channels) |
|
|
65 |
|
|
|
66 |
layers = [conv_layer, bn_layer, nll_layer] |
|
|
67 |
return nn.Sequential(*layers) |
|
|
68 |
|
|
|
69 |
def deconv_2D(in_channels, out_channels, kernel_size=3, stride=1, padding = 1, nonlinearity = relu): |
|
|
70 |
conv_layer = nn.ConvTranspose2d(in_channels = in_channels, out_channels= out_channels, kernel_size = kernel_size, stride = stride, padding = padding, output_padding = 1, bias = False) |
|
|
71 |
|
|
|
72 |
nll_layer = nonlinearity() |
|
|
73 |
bn_layer = nn.BatchNorm2d(out_channels) |
|
|
74 |
|
|
|
75 |
layers = [conv_layer, bn_layer, nll_layer] |
|
|
76 |
return nn.Sequential(*layers) |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
def conv_blocks_2_2D(in_channels, out_channels, strides=1): |
|
|
80 |
conv1 = conv_2D(in_channels, out_channels, stride = strides) |
|
|
81 |
conv2 = conv_2D(out_channels, out_channels, stride=1) |
|
|
82 |
layers = [conv1, conv2] |
|
|
83 |
return nn.Sequential(*layers) |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
def conv_blocks_3_2D(in_channels, out_channels, strides=1): |
|
|
87 |
conv1 = conv_2D(in_channels, out_channels, stride = strides) |
|
|
88 |
conv2 = conv_2D(out_channels, out_channels, stride=1) |
|
|
89 |
conv3 = conv_2D(out_channels, out_channels, stride=1) |
|
|
90 |
layers = [conv1, conv2, conv3] |
|
|
91 |
return nn.Sequential(*layers) |
|
|
92 |
|
|
|
93 |
# Flatten layer |
|
|
94 |
class Flatten(nn.Module): |
|
|
95 |
def forward(self, input): |
|
|
96 |
return input.view(input.size(0), -1) |
|
|
97 |
|
|
|
98 |
def generate_grid(x, offset): |
|
|
99 |
x_shape = x.size() |
|
|
100 |
grid_d, grid_w, grid_h = torch.meshgrid([torch.linspace(-1, 1, x_shape[2]), torch.linspace(-1, 1, x_shape[3]), torch.linspace(-1, 1, x_shape[4])]) # (h, w, h) |
|
|
101 |
grid_d = grid_d.cuda().float() |
|
|
102 |
grid_w = grid_w.cuda().float() |
|
|
103 |
grid_h = grid_h.cuda().float() |
|
|
104 |
|
|
|
105 |
grid_d = nn.Parameter(grid_d, requires_grad=False) |
|
|
106 |
grid_w = nn.Parameter(grid_w, requires_grad=False) |
|
|
107 |
grid_h = nn.Parameter(grid_h, requires_grad=False) |
|
|
108 |
|
|
|
109 |
offset_h, offset_w, offset_d = torch.split(offset, 1, 1) |
|
|
110 |
offset_d = offset_d.contiguous().view(-1, int(x_shape[2]), int(x_shape[3]), int(x_shape[4])) # (b*c, d, w, h) |
|
|
111 |
offset_w = offset_w.contiguous().view(-1, int(x_shape[2]), int(x_shape[3]), int(x_shape[4])) # (b*c, d, w, h) |
|
|
112 |
offset_h = offset_h.contiguous().view(-1, int(x_shape[2]), int(x_shape[3]), int(x_shape[4])) # (b*c, d, w, h) |
|
|
113 |
|
|
|
114 |
offset_d = grid_d + offset_d |
|
|
115 |
offset_w = grid_w + offset_w |
|
|
116 |
offset_h = grid_h + offset_h |
|
|
117 |
|
|
|
118 |
offsets = torch.stack((offset_h, offset_w, offset_d), 4) # should have the same order as offset |
|
|
119 |
return offsets |
|
|
120 |
|
|
|
121 |
def transform(seg_source, loc, mode='bilinear'): |
|
|
122 |
grid = generate_grid(seg_source, loc) |
|
|
123 |
# seg_source: NCDHW |
|
|
124 |
# grid: NDHW3 |
|
|
125 |
out = F.grid_sample(seg_source, grid, mode=mode, align_corners=True) |
|
|
126 |
return out |
|
|
127 |
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
class Mesh_2d(nn.Module): |
|
|
131 |
"""Deformable registration network with input from image space """ |
|
|
132 |
def __init__(self, n_ch=1): |
|
|
133 |
super(Mesh_2d, self).__init__() |
|
|
134 |
|
|
|
135 |
self.conv1 = conv_2D(n_ch, 32) |
|
|
136 |
self.conv2 = conv_2D(32, 64) |
|
|
137 |
|
|
|
138 |
def forward(self, x_2ch, x_2ched, x_4ch, x_4ched): |
|
|
139 |
# x: source image; x_pred: target image; |
|
|
140 |
net = {} |
|
|
141 |
|
|
|
142 |
net['conv1_2ch'] = self.conv1(x_2ch) |
|
|
143 |
net['conv1_4ch'] = self.conv1(x_4ch) |
|
|
144 |
net['conv1s_2ch'] = self.conv1(x_2ched) |
|
|
145 |
net['conv1s_4ch'] = self.conv1(x_4ched) |
|
|
146 |
|
|
|
147 |
net['conv2_2ch'] = self.conv2(net['conv1_2ch']) |
|
|
148 |
net['conv2_4ch'] = self.conv2(net['conv1_4ch']) |
|
|
149 |
net['conv2s_2ch'] = self.conv2(net['conv1s_2ch']) |
|
|
150 |
net['conv2s_4ch'] = self.conv2(net['conv1s_4ch']) |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
return net |
|
|
154 |
|
|
|
155 |
class deformnet(nn.Module): |
|
|
156 |
"""Deformable registration network with input from image space """ |
|
|
157 |
def __init__(self, n_ch=64, mesh_dim=22043): |
|
|
158 |
super(deformnet, self).__init__() |
|
|
159 |
|
|
|
160 |
self.conv_blocks_2D = [conv_blocks_2_2D(n_ch, 64), conv_blocks_2_2D(64, 128, 2), conv_blocks_3_2D(128, 256, 2), |
|
|
161 |
conv_blocks_3_2D(256, 512, 2), conv_blocks_3_2D(512, 512, 2)] |
|
|
162 |
|
|
|
163 |
self.conv_blocks_2D = nn.Sequential(*self.conv_blocks_2D) |
|
|
164 |
|
|
|
165 |
self.conv2d9 = conv_2D(512 * 3, 512 * 2, kernel_size=3, stride=1) |
|
|
166 |
self.conv2d10 = deconv_2D(512 * 2, 512, kernel_size=3, stride=2) |
|
|
167 |
self.conv2d10_1 = conv_2D(512, 512, kernel_size=3, stride=1) |
|
|
168 |
self.conv2d11 = deconv_2D(512, 256, kernel_size=3, stride=2) |
|
|
169 |
self.conv2d11_1 = conv_2D(256, 256, kernel_size=3, stride=1) |
|
|
170 |
self.conv2d12 = deconv_2D(256, 128, kernel_size=3, stride=2) |
|
|
171 |
self.conv2d12_1 = conv_2D(128, 128, kernel_size=3, stride=1) |
|
|
172 |
self.conv2d13 = deconv_2D(128, 64, kernel_size=3, stride=2) |
|
|
173 |
self.conv2d13_1 = conv_2D(64, 64, kernel_size=3, stride=1) |
|
|
174 |
|
|
|
175 |
self.conv3d17 = conv(1, 3, 3, stride=(1, 1, 1)) |
|
|
176 |
self.conv3d18 = nn.Conv3d(3, 3, 1, stride=(1, 1, 1)) |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
def forward(self, x_saed, x_2ched, x_4ched): |
|
|
180 |
# x: source image; x_pred: target image; |
|
|
181 |
net = {} |
|
|
182 |
|
|
|
183 |
net['conv0_sa_ed'] = x_saed |
|
|
184 |
net['conv0_2ch_ed'] = x_2ched |
|
|
185 |
net['conv0_4ch_ed'] = x_4ched |
|
|
186 |
# 5 refers to 5 output or 5 blocks |
|
|
187 |
for i in range(5): |
|
|
188 |
net['conv%d_sa_ed' % (i + 1)] = self.conv_blocks_2D[i](net['conv%d_sa_ed' % i]) |
|
|
189 |
net['conv%d_2ch_ed' % (i + 1)] = self.conv_blocks_2D[i](net['conv%d_2ch_ed' % i]) |
|
|
190 |
net['conv%d_4ch_ed' % (i + 1)] = self.conv_blocks_2D[i](net['conv%d_4ch_ed' % i]) |
|
|
191 |
|
|
|
192 |
|
|
|
193 |
net['concat'] = torch.cat((net['conv5_sa_ed'], net['conv5_2ch_ed'], net['conv5_4ch_ed']), 1) |
|
|
194 |
|
|
|
195 |
net['conv2d_0_ed'] = self.conv2d9(net['concat']) |
|
|
196 |
net['conv2d_1_ed'] = self.conv2d10_1(self.conv2d10(net['conv2d_0_ed'])) |
|
|
197 |
net['conv2d_2_ed'] = self.conv2d11_1(self.conv2d11(net['conv2d_1_ed'])) |
|
|
198 |
net['conv2d_3_ed'] = self.conv2d12_1(self.conv2d12(net['conv2d_2_ed'])) |
|
|
199 |
net['conv2d_4_ed'] = self.conv2d13_1(self.conv2d13(net['conv2d_3_ed'])) |
|
|
200 |
net['conv3d_def_ed'] = net['conv2d_4_ed'].unsqueeze(1) |
|
|
201 |
net['out_def_ed'] = torch.tanh(self.conv3d18(self.conv3d17(net['conv3d_def_ed']))) |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
return net |