-
Notifications
You must be signed in to change notification settings - Fork 1
/
probabilistic_UNet.py
445 lines (366 loc) · 18.2 KB
/
probabilistic_UNet.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torch.distributions import Normal, Independent, kl
from net_utils import warp_image_diffeomorphic
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def truncated_normal_(tensor, mean=0, std=1):
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
def init_weights(m):
if type(m) == nn.Conv3d or type(m) == nn.ConvTranspose3d:
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
truncated_normal_(m.bias, mean=0, std=0.0001)
def init_weights_orthogonal_normal(m):
if type(m) == nn.Conv3d or type(m) == nn.ConvTranspose3d:
nn.init.orthogonal_(m.weight)
truncated_normal_(m.bias, mean=0, std=0.0001)
def l2_regularisation(m):
l2_reg = None
for W in m.parameters():
if l2_reg is None:
l2_reg = W.norm(2)
else:
l2_reg = l2_reg + W.norm(2)
return l2_reg
class DownConvBlock(nn.Module):
"""
A block of three convolutional layers where each layer is followed by a non-linear activation function
Between each block we add a pooling operation.
"""
def __init__(self, input_dim, output_dim, initializers, padding, pool=True):
super(DownConvBlock, self).__init__()
layers = []
if pool:
layers.append(nn.AvgPool3d(kernel_size=2, stride=2, padding=0, ceil_mode=True))
layers.append(nn.Conv3d(input_dim, output_dim, kernel_size=3, stride=1, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.Conv3d(output_dim, output_dim, kernel_size=3, stride=1, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
self.layers = nn.Sequential(*layers)
self.layers.apply(init_weights)
def forward(self, patch):
return self.layers(patch)
class UpConvBlock(nn.Module):
"""
A block consists of an upsampling layer followed by a convolutional layer to reduce the amount of channels and then a DownConvBlock
If bilinear is set to false, we do a transposed convolution instead of upsampling
"""
def __init__(self, input_dim, output_dim, initializers, padding, bilinear=True):
super(UpConvBlock, self).__init__()
self.bilinear = bilinear
if not self.bilinear:
self.upconv_layer = nn.ConvTranspose3d(input_dim, output_dim, kernel_size=3, stride=2)
self.upconv_layer.apply(init_weights)
self.conv_block = DownConvBlock(input_dim, output_dim, initializers, padding, pool=False)
def forward(self, x, bridge):
if self.bilinear:
up = nn.functional.interpolate(x, mode='trilinear', scale_factor=2, align_corners=True)
else:
up = self.upconv_layer(x)
assert up.shape[3] == bridge.shape[3]
out = torch.cat([up, bridge], 1)
out = self.conv_block(out)
return out
class Unet(nn.Module):
"""
A UNet (https://arxiv.org/abs/1505.04597) implementation.
input_channels: the number of channels in the image (1 for greyscale and 3 for RGB)
dim: the number of classes to predict
num_filters: list with the amount of filters per layer
apply_last_layer: boolean to apply last layer or not (not used in Probabilistic UNet)
padidng: Boolean, if true we pad the images with 1 so that we keep the same dimensions
"""
def __init__(self, input_channels, dim, num_filters, initializers, apply_last_layer=True, padding=True):
super(Unet, self).__init__()
self.input_channels = input_channels
self.dim = dim
self.num_filters = num_filters
self.padding = padding
self.activation_maps = []
self.apply_last_layer = apply_last_layer
self.contracting_path = nn.ModuleList()
for i in range(len(self.num_filters)):
input = self.input_channels if i == 0 else output
output = self.num_filters[i]
if i == 0:
pool = False
else:
pool = True
self.contracting_path.append(DownConvBlock(input, output, initializers, padding, pool=pool))
self.upsampling_path = nn.ModuleList()
n = len(self.num_filters) - 2
for i in range(n, -1, -1):
input = output + self.num_filters[i]
output = self.num_filters[i]
self.upsampling_path.append(UpConvBlock(input, output, initializers, padding))
if self.apply_last_layer:
self.last_layer = nn.Conv3d(output, dim, kernel_size=1)
def forward(self, input, val):
blocks = []
x=input
for i, down in enumerate(self.contracting_path):
x = down(x)
if i != len(self.contracting_path) - 1:
blocks.append(x)
for i, up in enumerate(self.upsampling_path):
x = up(x, blocks[-i - 1])
del blocks
# Used for saving the activations and plotting
if val:
self.activation_maps.append(x)
if self.apply_last_layer:
x = self.last_layer(x)
return x
class Encoder(nn.Module):
"""
A convolutional neural network, consisting of len(num_filters) times a block of no_convs_per_block convolutional layers,
after each block a pooling operation is performed. And after each convolutional layer a non-linear (ReLU) activation function is applied.
"""
def __init__(self, input_channels, num_filters, no_convs_per_block, initializers, padding=True, posterior=False):
super(Encoder, self).__init__()
self.contracting_path = nn.ModuleList()
self.input_channels = input_channels
self.num_filters = num_filters
if posterior:
# To accomodate for the mask that is concatenated at the channel axis, we increase the input_channels.
self.input_channels*=2
layers = []
for i in range(len(self.num_filters)):
"""
Determine input_dim and output_dim of conv layers in this block. The first layer is input x output,
All the subsequent layers are output x output.
"""
input_dim = self.input_channels if i == 0 else output_dim
output_dim = num_filters[i]
if i != 0:
layers.append(nn.AvgPool3d(kernel_size=2, stride=2, padding=0, ceil_mode=True))
layers.append(nn.Conv3d(input_dim, output_dim, kernel_size=3, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
for _ in range(no_convs_per_block - 1):
layers.append(nn.Conv3d(output_dim, output_dim, kernel_size=3, padding=int(padding)))
layers.append(nn.ReLU(inplace=True))
self.layers = nn.Sequential(*layers)
self.layers.apply(init_weights)
def forward(self, input):
output = self.layers(input)
return output
class AxisAlignedConvGaussian(nn.Module):
"""
A convolutional net that parametrizes a Gaussian distribution with axis aligned covariance matrix.
"""
def __init__(self, input_channels, num_filters, no_convs_per_block, latent_dim, initializers, posterior=False):
super(AxisAlignedConvGaussian, self).__init__()
self.input_channels = input_channels
self.channel_axis = 1
self.num_filters = num_filters
self.no_convs_per_block = no_convs_per_block
self.latent_dim = latent_dim
self.posterior = posterior
if self.posterior:
self.name = 'Posterior'
else:
self.name = 'Prior'
self.encoder = Encoder(self.input_channels, self.num_filters, self.no_convs_per_block, initializers,
posterior=self.posterior)
self.conv_layer = nn.Conv3d(num_filters[-1], 2 * self.latent_dim, (1, 1, 1), stride=1)
self.show_img = 0
self.show_seg = 0
self.show_concat = 0
self.show_enc = 0
self.sum_input = 0
nn.init.kaiming_normal_(self.conv_layer.weight, mode='fan_in', nonlinearity='relu')
nn.init.normal_(self.conv_layer.bias)
def forward(self, input, segm=None):
# If segmentation is not none, concatenate the mask to the channel axis of the input
if segm is not None:
self.show_img = input
self.show_seg = segm
input = torch.cat((input, segm), dim=1)
self.show_concat = input
self.sum_input = torch.sum(input)
encoding = self.encoder(input)
self.show_enc = encoding
# We only want the mean of the resulting hxwxd image
encoding = torch.mean(encoding, dim=2, keepdim=True)
encoding = torch.mean(encoding, dim=3, keepdim=True)
encoding = torch.mean(encoding, dim=4, keepdim=True)
# Convert encoding to 2 x latent dim and split up for mu and log_sigma
mu_log_sigma = self.conv_layer(encoding)
# We squeeze the second dimension twice, since otherwise it won't work when batch size is equal to 1
mu_log_sigma = torch.squeeze(mu_log_sigma, dim=2)
mu_log_sigma = torch.squeeze(mu_log_sigma, dim=2)
mu_log_sigma = torch.squeeze(mu_log_sigma, dim=2)
mu = mu_log_sigma[:, :self.latent_dim]
log_sigma = mu_log_sigma[:, self.latent_dim:]
# This is a multivariate normal with diagonal covariance matrix sigma
# https://github.com/pytorch/pytorch/pull/11178
dist = Independent(Normal(loc=mu, scale=torch.exp(log_sigma)), 1)
return dist
class Fcomb(nn.Module):
"""
A function composed of no_convs_fcomb times a 1x1 convolution that combines the sample taken from the latent space,
and output of the UNet (the feature map) by concatenating them along their channel axis.
"""
def __init__(self, num_filters, latent_dim, num_output_channels, dim, no_convs_fcomb, initializers,
use_tile=True):
super(Fcomb, self).__init__()
self.num_channels = num_output_channels # output channels
self.dim = dim
self.channel_axis = 1
self.spatial_axes = [2, 3,4]
self.num_filters = num_filters
self.latent_dim = latent_dim
self.use_tile = use_tile
self.no_convs_fcomb = no_convs_fcomb
self.name = 'Fcomb'
if self.use_tile:
layers = []
# Decoder of N x a 1x1 convolution followed by a ReLU activation function except for the last layer
layers.append(nn.Conv3d(self.num_filters[0] + self.latent_dim, self.num_filters[0], kernel_size=1))
layers.append(nn.ReLU(inplace=True))
for _ in range(no_convs_fcomb - 2):
layers.append(nn.Conv3d(self.num_filters[0], self.num_filters[0], kernel_size=1))
layers.append(nn.ReLU(inplace=True))
self.layers = nn.Sequential(*layers)
self.last_layer =nn.Conv3d(self.num_filters[0], self.dim, kernel_size=1)
if initializers['w'] == 'orthogonal':
self.layers.apply(init_weights_orthogonal_normal)
self.last_layer.apply(init_weights_orthogonal_normal)
else:
self.layers.apply(init_weights)
self.last_layer.apply(init_weights)
def tile(self, a, dim, n_tile):
"""
This function is taken form PyTorch forum and mimics the behavior of tf.tile.
Source: https://discuss.pytorch.org/t/how-to-tile-a-tensor/13853/3
"""
init_dim = a.size(dim)
repeat_idx = [1] * a.dim()
repeat_idx[dim] = n_tile
a = a.repeat(*(repeat_idx))
order_index = torch.LongTensor(np.concatenate([init_dim * np.arange(n_tile) + i for i in range(init_dim)])).to(
device)
return torch.index_select(a, dim, order_index)
def forward(self, feature_map, z):
"""
Z is batch_sizexlatent_dim and feature_map is batch_sizexno_channelsxHxW.
So broadcast Z to batch_sizexlatent_dimxHxW. Behavior is exactly the same as tf.tile (verified)
"""
if self.use_tile:
z = torch.unsqueeze(z, 2)
z = self.tile(z, 2, feature_map.shape[self.spatial_axes[0]])
z = torch.unsqueeze(z, 3)
z = self.tile(z, 3, feature_map.shape[self.spatial_axes[1]])
z = torch.unsqueeze(z, 4)
z = self.tile(z, 4, feature_map.shape[self.spatial_axes[2]])
# Concatenate the feature map (output of the UNet) and the sample taken from the latent space
feature_map = torch.cat((feature_map, z), dim=self.channel_axis)
output = self.layers(feature_map)
return self.last_layer(output)
class ProbabilisticUnet(nn.Module):
"""
A probabilistic UNet (https://arxiv.org/abs/1806.05034) implementation.
input_channels: the number of channels in the image (1 for greyscale and 3 for RGB)
dim: the number of classes to predict
num_filters: is a list consisint of the amount of filters layer
latent_dim: dimension of the latent space
no_cons_per_block: no convs per block in the (convolutional) encoder of prior and posterior
"""
def __init__(self, input_channels=1,dim=1, num_filters=[32, 64, 128, 192], latent_dim=6, no_convs_fcomb=4,
beta=10.0, device=device):
super(ProbabilisticUnet, self).__init__()
self.input_channels = input_channels
self.dim = dim
self.num_filters = num_filters
self.latent_dim = latent_dim
self.no_convs_per_block = 3
self.no_convs_fcomb = no_convs_fcomb
self.initializers = {'w': 'he_normal', 'b': 'normal'}
self.beta = beta
self.z_prior_sample = 0
self.unet = Unet(self.input_channels, self.dim, self.num_filters, self.initializers,
apply_last_layer=False, padding=True).to(device)
self.prior = AxisAlignedConvGaussian(self.input_channels, self.num_filters, self.no_convs_per_block,
self.latent_dim, self.initializers, ).to(device)
self.posterior = AxisAlignedConvGaussian(self.input_channels, self.num_filters, self.no_convs_per_block,
self.latent_dim, self.initializers, posterior=True).to(device)
self.fcomb = Fcomb(self.num_filters, self.latent_dim, self.input_channels, self.dim,
self.no_convs_fcomb, {'w': 'orthogonal', 'b': 'normal'}, use_tile=True).to(device)
def forward(self, patch, segm, training=True):
"""
Construct prior latent space for patch and run patch through UNet,
in case training is True also construct posterior latent space
"""
if training:
self.posterior_latent_space = self.posterior.forward(patch, segm)
self.prior_latent_space = self.prior.forward(patch)
self.unet_features = self.unet.forward(patch, False)
def sample(self, testing=False, given_sample=None):
"""
Sample a segmentation by reconstructing from a prior sample
and combining this with UNet features
"""
if given_sample is None:
if testing == False:
z_prior = self.prior_latent_space.rsample()
self.z_prior_sample = z_prior
else:
z_prior = self.prior_latent_space.rsample()
self.z_prior_sample = z_prior
else:
z_prior=given_sample
return self.fcomb.forward(self.unet_features, z_prior)
def reconstruct(self, use_posterior_mean=False, calculate_posterior=False, z_posterior=None):
"""
Reconstruct a segmentation from a posterior sample (decoding a posterior sample) and UNet feature map
use_posterior_mean: use posterior_mean instead of sampling z_q
calculate_posterior: use a provided sample or sample from posterior latent space
"""
if use_posterior_mean:
z_posterior = self.posterior_latent_space.loc
else:
if calculate_posterior:
z_posterior = self.posterior_latent_space.rsample()
return self.fcomb.forward(self.unet_features, z_posterior)
def kl_divergence(self, analytic=True, calculate_posterior=False, z_posterior=None):
"""
Calculate the KL divergence between the posterior and prior KL(Q||P)
analytic: calculate KL analytically or via sampling from the posterior
calculate_posterior: if we use samapling to approximate KL we can sample here or supply a sample
"""
if analytic:
# Need to add this to torch source code, see: https://github.com/pytorch/pytorch/issues/13545
kl_div = kl.kl_divergence(self.posterior_latent_space, self.prior_latent_space)
else:
if calculate_posterior:
z_posterior = self.posterior_latent_space.rsample()
log_posterior_prob = self.posterior_latent_space.log_prob(z_posterior)
log_prior_prob = self.prior_latent_space.log_prob(z_posterior)
kl_div = log_posterior_prob - log_prior_prob
return kl_div
def elbo(self, input,segm, analytic_kl=True, reconstruct_posterior_mean=False):
"""
Calculate the evidence lower bound of the log-likelihood of P(Y|X)
"""
criterion = nn.L1Loss()
z_posterior = self.posterior_latent_space.rsample()
self.kl = torch.mean(
self.kl_divergence(analytic=analytic_kl, calculate_posterior=False, z_posterior=z_posterior))
# Here we use the posterior sample sampled above
self.displ = self.reconstruct(use_posterior_mean=reconstruct_posterior_mean, calculate_posterior=False,
z_posterior=z_posterior)
if self.dim == 2:
self.displ = self.displ.permute(0, 2, 3, 1)
else:
self.displ =self.displ.permute(0, 2, 3, 4, 1)
# here comes the warping part!!!
self.reconstruction_warped, self.computed_displ = warp_image_diffeomorphic(input,self.displ, mode='bilinear',ret_displ=True)
reconstruction_loss = criterion(input=self.reconstruction_warped, target=segm)
self.reconstruction_loss = reconstruction_loss
return -(20*self.reconstruction_loss+ self.beta * self.kl)