forked from TheoEst/abdominal_registration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontiersNet.py
More file actions
155 lines (103 loc) · 5.27 KB
/
FrontiersNet.py
File metadata and controls
155 lines (103 loc) · 5.27 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 31 15:32:53 2020
@author: T_ESTIENNE
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from abdominal_registration import blocks
def integral3DImage(deformable):
img_size = deformable.shape[-3:]
x_s = (4*torch.cumsum(deformable[:, 0, :, :], dim=1) - 2)/ (img_size[0] - 1) - 1
y_s = (4*torch.cumsum(deformable[:, 1, :, :], dim=2) - 2)/ (img_size[1] - 1) - 1
z_s = (4*torch.cumsum(deformable[:, 2, :, :], dim=3) - 2)/ (img_size[2] - 1) - 1
out = torch.stack([z_s, y_s, x_s], dim=1) # Shape B*3*H*W*D
return out
def diffeomorphic3D(moving, integrated_grid=None):
integrated_grid = integrated_grid.permute(0, 2, 3, 4, 1)
# Deformed moving image
deformed_img = F.grid_sample(moving, integrated_grid)
return deformed_img
class FrontiersNet(nn.Module):
def __init__(self,
channel_multiplication=4,
pool_blocks=4,
channels=[4, 8, 16, 32, 64, 128, 256],
cross_entropy=False,
activation_type='relu',
last_activation=None,
instance_norm=False,
batch_norm=False,
nb_Convs=[1, 1, 1, 1, 1, 1, 1],
freeze_registration=False,
zeros_init=False,
symmetric_training=False,
multi_windows=False,
deep_supervision=False,
keep_all_label=False,
):
super(FrontiersNet, self).__init__()
channels = channels[:pool_blocks+1]
channels = [int(channel * channel_multiplication)
for channel in channels]
nb_Convs = nb_Convs[:pool_blocks]
input_channel = 3 if multi_windows else 1
self.deep_supervision = deep_supervision
self.symmetric_training = symmetric_training
kwargs = {'pool_blocks': pool_blocks, 'channels': channels,
'activation_type': activation_type,
'instance_norm': instance_norm, 'batch_norm' :batch_norm,
'nb_Convs':nb_Convs,
}
# Encoder
self.encoder = blocks.Encoder(input_channel=input_channel,
**kwargs)
# Decoder
registration_decoder_out_channels = 3
self.registration_decoder = blocks.Decoder(out_channels=registration_decoder_out_channels,
last_activation='sigmoid',
freeze_registration=freeze_registration,
zeros_init=zeros_init,
deep_supervision=deep_supervision,
**kwargs)
self.merge_operator = torch.sub
if deep_supervision:
self.deep_blocks = blocks.DeepSupervisionBlock(pool_blocks, channels,
registration_decoder_out_channels,
last_activation='sigmoid')
def forward(self, moving, reference):
moving_encoding = self.encoder(moving)
reference_encoding = self.encoder(reference)
mergin_moving = self.merging(moving_encoding, reference_encoding)
(deformable_grid, integrated_grid,
deformed_img) = self.apply_deformation(moving, mergin=mergin_moving)
pred_sample = [(deformable_grid, integrated_grid, deformed_img)]
if self.symmetric_training:
mergin_reference = self.merging(reference_encoding, moving_encoding)
(deformable_grid, integrated_grid,
deformed_img) = self.apply_deformation(reference, mergin=mergin_reference)
pred_sample.append( (deformable_grid, integrated_grid, deformed_img) )
return pred_sample
def apply_deformation(self, source, mergin):
deformable_grid = self.registration_decoder(mergin)
if self.deep_supervision:
deformable_grid = self.deep_blocks(deformable_grid)
integrated_grid = [integral3DImage(grid) for grid in deformable_grid]
deformed_img = [diffeomorphic3D(source, grid) for grid in integrated_grid]
deformable_grid = deformable_grid[-1]
integrated_grid = integrated_grid[-1]
else:
integrated_grid = integral3DImage(deformable_grid)
deformed_img = diffeomorphic3D(source, integrated_grid)
return deformable_grid, integrated_grid, deformed_img
def merging(self, encoding_x, encoding_y):
merged_encoding = []
for x, y in zip(encoding_x, encoding_y):
merged_encoding.append( self.merge_operator(x, y))
return merged_encoding
if __name__ == '__main__':
model = FrontiersNet(channel_multiplication=8)
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
print(count_parameters(model))