-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance_optimization.py
185 lines (174 loc) · 8.68 KB
/
instance_optimization.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
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "."))
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import torch as tc
import torch.nn.functional as F
import torch.optim as optim
import utils_tc as utc
from typing import Callable
def affine_registration(source: tc.Tensor, target: tc.Tensor, num_levels: int, used_levels: int, num_iters: list,
learning_rate: float, cost_function: Callable[[tc.Tensor, tc.Tensor, dict], float], cost_function_params: dict={},
device: str="cpu", initial_transform=None, echo: bool=False, return_best: bool=False):
"""
Performs the affine registration using the instance optimization technique (a prototype).
For the real-time DirectX implementation the autograd must be replaced by an analytical gradient calculation
and the optimization should be implemented using matrix-free operations and a quasi-Newton algorithm.
Parameters
----------
source : tc.Tensor
The source tensor (1x1 x size)
target : tc.Tensor
The target tensor (1x1 x size)
num_levels : int
The number of resolution levels
used_levels : int
The number of actually used resolution levels (must be lower (or equal) than the num_levels)
num_iters : int
The nubmer of iterations per resolution
learning_rate : float
The learning rate for the optimizer
cost_function : Callable[tc.Tensor, tc.Tensor, dict] -> float
The cost function being optimized
cost_function_params : dict (default: {})
The optional cost function parameters
device : str
The device used for warping (e.g. "cpu" or "cuda:0")
Returns
----------
transformation : tc.Tensor
The affine transformation matrix (1 x transformation_size (2x3 or 3x4))
"""
ndim = len(source.size()) - 2
if initial_transform is None:
if ndim == 2:
transformation = tc.zeros((1, 2, 3), dtype=source.dtype, device=device)
transformation[0, 0, 0] = 1.0
transformation[0, 1, 1] = 1.0
transformation = transformation.detach().clone()
transformation.requires_grad = True
elif ndim == 3:
transformation = tc.zeros((1, 3, 4), dtype=source.dtype, device=device)
transformation[0, 0, 0] = 1.0
transformation[0, 1, 1] = 1.0
transformation[0, 2, 2] = 1.0
transformation = transformation.detach().clone()
transformation.requires_grad = True
else:
raise ValueError("Unsupported number of dimensions.")
else:
transformation = initial_transform.detach().clone()
transformation.requires_grad = True
optimizer = optim.Adam([transformation], learning_rate)
source_pyramid = utc.create_pyramid(source, num_levels=num_levels)
target_pyramid = utc.create_pyramid(target, num_levels=num_levels)
if return_best:
best_transformation = transformation.clone()
best_cost = 1000.0
for j in range(used_levels):
current_source = source_pyramid[j]
current_target = target_pyramid[j]
for i in range(num_iters[j]):
with tc.set_grad_enabled(True):
sampling_grid = F.affine_grid(transformation, size=current_source.size(), align_corners=False)
warped_source = utc.transform_tensor(current_source, sampling_grid, device=device)
cost = cost_function(warped_source, current_target, device=device, **cost_function_params)
cost.backward()
optimizer.step()
current_cost = cost.item()
optimizer.zero_grad()
if echo:
print(f"Iter: {i}, Current cost: {current_cost}")
if return_best:
if current_cost < best_cost:
best_cost = current_cost
best_transformation = transformation.clone()
if return_best:
return best_transformation
else:
return transformation
def nonrigid_registration(source: tc.Tensor, target: tc.Tensor, num_levels: int, used_levels: int, num_iters: list,
learning_rates: list, alphas: list,
cost_function: Callable[[tc.Tensor, tc.Tensor, dict], float], regularization_function: Callable[[tc.Tensor, dict], float],
cost_function_params: dict={}, regularization_function_params: dict={},
penalty_function: Callable=None, penalty_function_params: dict={},
initial_displacement_field: tc.Tensor=None,
device: str="cpu", echo: bool=False):
"""
Performs the nonrigid registration using the instance optimization technique (a prototype).
For the real-time DirectX implementation the autograd must be replaced by an analytical gradient calculation
and the optimization should be implemented using matrix-free operations and a quasi-Newton algorithm.
Parameters
----------
source : tc.Tensor
The source tensor (1x1 x size)
target : tc.Tensor
The target tensor (1x1 x size)
num_levels : int
The number of resolution levels
used_levels : int
The number of actually used resolution levels (must be lower (or equal) than the num_levels)
num_iters : int
The nubmer of iterations per resolution
learning_rate : float
The learning rate for the optimizer
alpha : float
The regularization weight
cost_function : Callable[tc.Tensor, tc.Tensor, dict] -> float
The cost function being optimized
regularization_function : Callable[tc.Tensor, dict] -> float
The regularization function
cost_function_params : dict (default: {})
The optional cost function parameters
regularization_function_params : dict (default: {})
The optional regularization function parameters
penalty_function : Callable
The optional penalty function (must be differntiable)
penalty_function_params : dict(default: {})
The optional penalty function parameters
initial_displacement_field : tc.Tensor (default None)
The initial displacement field (e.g. resulting from the initial, affine registration)
device : str
The device used for warping (e.g. "cpu" or "cuda:0")
Returns
----------
displacement_field : tc.Tensor
The calculated displacement_field (to be applied using warp_tensor from utils_tc)
"""
source_pyramid = utc.create_pyramid(source, num_levels=num_levels)
target_pyramid = utc.create_pyramid(target, num_levels=num_levels)
for j in range(used_levels):
current_source = source_pyramid[j]
current_target = target_pyramid[j]
if j == 0:
if initial_displacement_field is None:
displacement_field = utc.create_identity_displacement_field(current_source).detach().clone()
displacement_field.requires_grad = True
else:
displacement_field = utc.resample_displacement_field_to_size(initial_displacement_field, (current_source.size(2), current_source.size(3))).detach().clone()
displacement_field.requires_grad = True
optimizer = optim.Adam([displacement_field], learning_rates[j])
else:
displacement_field = utc.resample_displacement_field_to_size(displacement_field, (current_source.size(2), current_source.size(3))).detach().clone()
displacement_field.requires_grad = True
optimizer = optim.Adam([displacement_field], learning_rates[j])
for i in range(num_iters[j]):
with tc.set_grad_enabled(True):
warped_source = utc.warp_tensor(current_source, displacement_field, device=device)
if i == 0:
if echo:
print(f"Initial cost: {cost_function(current_source, current_target, device=device, **cost_function_params)}")
print(f"First warp cost: {cost_function(warped_source, current_target, device=device, **cost_function_params)}")
cost = cost_function(warped_source, current_target, device=device, **cost_function_params)
reg = regularization_function(displacement_field, device=device, **regularization_function_params)
loss = cost + alphas[j]*reg
if penalty_function is not None:
loss = loss + penalty_function(penalty_function_params)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if echo:
print("Iter: ", i, "Current cost: ", cost.item(), "Current reg: ", reg.item(), "Current loss: ", loss.item())
if used_levels != num_levels:
displacement_field = utc.resample_displacement_field_to_size(displacement_field, (source.size(2), source.size(3)))
return displacement_field