-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayerwise_regularizer.py
185 lines (168 loc) · 7.54 KB
/
layerwise_regularizer.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
from icon_registration.losses import ICONLoss, flips
import icon_registration.network_wrappers as network_wrappers
import torch
from icon_registration.config import device
class GradientICONLayerwiseRegularizer(network_wrappers.RegistrationModule):
def __init__(self, network, lmbda):
super().__init__()
self.regis_net = network
self.lmbda = lmbda
def compute_gradient_icon_loss(self, phi_AB, phi_BA):
Iepsilon = (
self.identity_map
+ torch.randn(*self.identity_map.shape).to(self.identity_map.device)
* 1
/ self.identity_map.shape[-1]
)
direction_losses = []
approximate_Iepsilon = phi_AB(phi_BA(Iepsilon))
inverse_consistency_error = Iepsilon - approximate_Iepsilon
delta = 0.001
if len(self.identity_map.shape) == 4:
dx = torch.Tensor([[[[delta]], [[0.0]]]]).to(self.identity_map.device)
dy = torch.Tensor([[[[0.0]], [[delta]]]]).to(self.identity_map.device)
direction_vectors = (dx, dy)
elif len(self.identity_map.shape) == 5:
dx = torch.Tensor([[[[[delta]]], [[[0.0]]], [[[0.0]]]]]).to(
self.identity_map.device
)
dy = torch.Tensor([[[[[0.0]]], [[[delta]]], [[[0.0]]]]]).to(
self.identity_map.device
)
dz = torch.Tensor([[[[0.0]]], [[[0.0]]], [[[delta]]]]).to(
self.identity_map.device
)
direction_vectors = (dx, dy, dz)
elif len(self.identity_map.shape) == 3:
dx = torch.Tensor([[[delta]]]).to(self.identity_map.device)
direction_vectors = (dx,)
for d in direction_vectors:
approximate_Iepsilon_d = phi_AB(phi_BA(Iepsilon + d))
inverse_consistency_error_d = Iepsilon + d - approximate_Iepsilon_d
grad_d_icon_error = (
inverse_consistency_error - inverse_consistency_error_d
) / delta
direction_losses.append(torch.mean(grad_d_icon_error**2))
inverse_consistency_loss = sum(direction_losses)
return inverse_consistency_loss
def forward(self, image_A, image_B):
assert self.identity_map.shape[2:] == image_A.shape[2:]
assert self.identity_map.shape[2:] == image_B.shape[2:]
self.identity_map.isIdentity = True
self.phi_AB = self.regis_net(image_A, image_B)
self.phi_BA = self.regis_net(image_B, image_A)
inverse_consistency_loss = self.compute_gradient_icon_loss(
self.phi_AB, self.phi_BA
)
return self.phi_AB, self.lmbda * inverse_consistency_loss + torch.mean(
10000 * (self.phi_AB(self.identity_map) - self.identity_map) ** 2
)
class DiffusionLayerwiseRegularizer(network_wrappers.RegistrationModule):
def __init__(self, network, lmbda):
super().__init__()
self.regis_net = network
self.lmbda = lmbda
def compute_diffusion_loss(self, phi_AB_vectorfield):
phi_AB_vectorfield = self.identity_map - phi_AB_vectorfield
if len(self.identity_map.shape) == 3:
bending_energy = torch.mean(
(-phi_AB_vectorfield[:, :, 1:] + phi_AB_vectorfield[:, :, 1:-1]) ** 2
)
elif len(self.identity_map.shape) == 4:
bending_energy = torch.mean(
(-phi_AB_vectorfield[:, :, 1:] + phi_AB_vectorfield[:, :, :-1]) ** 2
) + torch.mean(
(-phi_AB_vectorfield[:, :, :, 1:] + phi_AB_vectorfield[:, :, :, :-1])
** 2
)
elif len(self.identity_map.shape) == 5:
bending_energy = (
torch.mean(
(-phi_AB_vectorfield[:, :, 1:] + phi_AB_vectorfield[:, :, :-1]) ** 2
)
+ torch.mean(
(
-phi_AB_vectorfield[:, :, :, 1:]
+ phi_AB_vectorfield[:, :, :, :-1]
)
** 2
)
+ torch.mean(
(
-phi_AB_vectorfield[:, :, :, :, 1:]
+ phi_AB_vectorfield[:, :, :, :, :-1]
)
** 2
)
)
return bending_energy * self.identity_map.shape[2] ** 2
def forward(self, image_A, image_B):
assert self.identity_map.shape[2:] == image_A.shape[2:]
assert self.identity_map.shape[2:] == image_B.shape[2:]
self.identity_map.isIdentity = True
self.phi_AB = self.regis_net(image_A, image_B)
diffusion_loss = self.compute_diffusion_loss(self.phi_AB(self.identity_map))
return self.phi_AB, self.lmbda * diffusion_loss
class TwoStepLayerwiseRegularizer(network_wrappers.RegistrationModule):
def __init__(self, phi, psi):
super().__init__()
self.phi = phi
self.psi = psi
def forward(self, image_A, image_B):
phi_AB, loss1 = self.phi(image_A, image_B)
a_circ_phi_AB = self.as_function(image_A)(phi_AB(self.identity_map))
psi_AB, loss2 = self.psi(a_circ_phi_AB, image_B)
return (lambda coords: phi_AB(psi_AB(coords))), loss1 + loss2
class CollectLayerwiseRegularizer(network_wrappers.RegistrationModule):
def __init__(self, network, similarity):
super().__init__()
self.regis_net = network
self.similarity = similarity
def compute_similarity_measure(self, phi_AB_vectorfield, image_A, image_B):
if getattr(self.similarity, "isInterpolated", False):
inbounds_tag = torch.zeros(
[image_A.shape[0]] + [1] + list(image_A.shape[2:]),
device=image_A.device,
)
if len(self.input_shape) - 2 == 3:
inbounds_tag[:, :, 1:-1, 1:-1, 1:-1] = 1.0
elif len(self.input_shape) - 2 == 2:
inbounds_tag[:, :, 1:-1, 1:-1] = 1.0
else:
inbounds_tag[:, :, 1:-1] = 1.0
else:
inbounds_tag = None
self.warped_image_A = self.as_function(
torch.cat([image_A, inbounds_tag], axis=1)
if inbounds_tag is not None
else image_A
)(phi_AB_vectorfield)
similarity_loss = self.similarity(self.warped_image_A, image_B)
return similarity_loss
def forward(self, image_A, image_B) -> ICONLoss:
assert self.identity_map.shape[2:] == image_A.shape[2:]
assert self.identity_map.shape[2:] == image_B.shape[2:]
self.identity_map.isIdentity = True
self.phi_AB, regularity_loss = self.regis_net(image_A, image_B)
self.phi_AB_vectorfield = self.phi_AB(self.identity_map)
similarity_loss = 2 * self.compute_similarity_measure(
self.phi_AB_vectorfield, image_A, image_B
)
all_loss = regularity_loss + similarity_loss
transform_magnitude = torch.mean(
(self.identity_map - self.phi_AB_vectorfield) ** 2
)
return ICONLoss(
all_loss,
regularity_loss,
similarity_loss,
transform_magnitude,
flips(self.phi_AB_vectorfield),
)
def prepare_for_viz(self, image_A, image_B):
self.phi_AB = self.regis_net(image_A, image_B)[0]
self.phi_AB_vectorfield = self.phi_AB(self.identity_map)
self.phi_BA = self.regis_net(image_B, image_A)[0]
self.phi_BA_vectorfield = self.phi_BA(self.identity_map)
self.warped_image_A = self.as_function(image_A)(self.phi_AB_vectorfield)
self.warped_image_B = self.as_function(image_B)(self.phi_BA_vectorfield)