forked from omarocegueda/registration
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SSDMetric.py
716 lines (695 loc) · 35.6 KB
/
SSDMetric.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
r'''
Similarity metric defined by the sum of squared differences
'''
import numpy as np
from scipy import gradient, ndimage
import tensorFieldUtils as tf
from SimilarityMetric import SimilarityMetric
import registrationCommon as rcommon
import matplotlib.pyplot as plt
class SSDMetric(SimilarityMetric):
r'''
Similarity metric for (monomodal) nonlinear image registration defined by
the sum of squared differences (SSD).
'''
GAUSS_SEIDEL_STEP = 0
DEMONS_STEP = 1
def get_default_parameters(self):
return {'lambda':1.0, 'max_inner_iter':5, 'scale':1,
'max_step_length':0.25, 'sigma_diff':3.0, 'step_type':0}
def __init__(self, dim, parameters):
super(SSDMetric, self).__init__(dim, parameters)
self.step_type = self.parameters['step_type']
self.levels_below = 0
def initialize_iteration(self):
r'''
Precomputes the gradient of the input images to be used in the
computation of the forward and backward steps.
'''
self.gradient_moving = np.empty(
shape = (self.moving_image.shape)+(self.dim,), dtype = np.float64)
i = 0
for grad in gradient(self.moving_image):
self.gradient_moving[..., i] = grad
i += 1
i = 0
self.gradient_fixed = np.empty(
shape = (self.fixed_image.shape)+(self.dim,), dtype = np.float64)
for grad in gradient(self.fixed_image):
self.gradient_fixed[..., i] = grad
i += 1
def compute_forward(self):
r'''
Computes the update displacement field to be used for registration of
the moving image towards the fixed image
'''
if self.step_type == SSDMetric.GAUSS_SEIDEL_STEP:
return self.compute_gauss_seidel_step(True)
elif self.step_type == SSDMetric.DEMONS_STEP:
return self.compute_demons_step(True)
return None
def compute_backward(self):
r'''
Computes the update displacement field to be used for registration of
the fixed image towards the moving image
'''
if self.step_type == SSDMetric.GAUSS_SEIDEL_STEP:
return self.compute_gauss_seidel_step(False)
elif self.step_type == SSDMetric.DEMONS_STEP:
return self.compute_demons_step(False)
return None
def compute_gauss_seidel_step(self, forward_step = True):
r'''
Minimizes the linearized energy function defined by the sum of squared
differences of corresponding pixels of the input images with respect
to the displacement field.
'''
max_inner_iter = self.parameters['max_inner_iter']
lambda_param = self.parameters['lambda']
max_step_length = self.parameters['max_step_length']
if forward_step:
shape = self.fixed_image.shape
else:
shape = self.moving_image.shape
if forward_step:
delta_field = self.fixed_image-self.moving_image
else:
delta_field = self.moving_image - self.fixed_image
#gradient = self.gradient_moving+self.gradient_fixed
gradient = self.gradient_moving
displacement = np.zeros(shape = (shape)+(self.dim,), dtype = np.float64)
if self.dim == 2:
self.energy = v_cycle_2d(self.levels_below, max_inner_iter,
delta_field, None, gradient, None,
lambda_param, displacement)
else:
self.energy = v_cycle_3d(self.levels_below, max_inner_iter,
delta_field, None, gradient, None,
lambda_param, displacement)
max_norm = np.sqrt(np.sum(displacement**2, -1)).max()
if max_norm > max_step_length:
displacement *= max_step_length/max_norm
return displacement
def compute_demons_step(self, forward_step = True):
r'''
Computes the demons step proposed by Vercauteren et al.[1] for the SSD
metric.
[1] Tom Vercauteren, Xavier Pennec, Aymeric Perchant, Nicholas Ayache,
"Diffeomorphic Demons: Efficient Non-parametric Image Registration",
Neuroimage 2009
'''
sigma_diff = self.parameters['sigma_diff']
max_step_length = self.parameters['max_step_length']
scale = self.parameters['scale']
if forward_step:
delta_field = self.fixed_image-self.moving_image
else:
delta_field = self.moving_image - self.fixed_image
gradient = self.gradient_moving+self.gradient_fixed
if self.dim == 2:
forward = tf.compute_demons_step2D(delta_field, gradient,
max_step_length, scale)
forward[..., 0] = ndimage.filters.gaussian_filter(forward[..., 0],
sigma_diff)
forward[..., 1] = ndimage.filters.gaussian_filter(forward[..., 1],
sigma_diff)
else:
forward = tf.compute_demons_step2D(delta_field, gradient,
max_step_length, scale)
forward[..., 0] = ndimage.filters.gaussian_filter(forward[..., 0],
sigma_diff)
forward[..., 1] = ndimage.filters.gaussian_filter(forward[..., 1],
sigma_diff)
forward[..., 2] = ndimage.filters.gaussian_filter(forward[..., 2],
sigma_diff)
return forward
def get_energy(self):
return NotImplemented
def use_original_fixed_image(self, originalfixed_image):
r'''
SSDMetric does not take advantage of the original fixed image, just pass
'''
pass
def use_original_moving_image(self, original_moving_image):
r'''
SSDMetric does not take advantage of the original moving image just pass
'''
pass
def use_fixed_image_dynamics(self, originalfixed_image, transformation):
r'''
SSDMetric does not take advantage of the image dynamics, just pass
'''
pass
def use_moving_image_dynamics(self, original_moving_image, transformation):
r'''
SSDMetric does not take advantage of the image dynamics, just pass
'''
pass
def report_status(self):
plt.figure()
rcommon.overlayImages(self.moving_image, self.fixed_image, False)
def get_metric_name(self):
return "SSDMetric"
def free_iteration(self):
pass
#######################Multigrid algorithms for SSD-like metrics#############
printEnergy = False
def single_cycle_2d(n, k, delta_field, sigma_field, gradient_field,
lambda_param, displacement, depth = 0):
r'''
One-pass multi-resolution Gauss-Seidel solver: solves the SSD-like linear
system starting at the coarcer resolution and then refining at the finer
resolution.
'''
if n == 0:
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
None,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
return error
#solve at coarcer grid
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field(sigma_field)
subdelta_field = tf.downsample_scalar_field(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
sub_displacement = np.zeros(
shape = ((shape[0]+1)//2, (shape[1]+1)//2, 2 ), dtype = np.float64)
sublambda_param = lambda_param*0.25
single_cycle_2d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration:', energy
#post-smoothing
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
None,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
def v_cycle_2d(n, k, delta_field, sigma_field, gradient_field, target,
lambda_param, displacement, depth = 0):
r'''
Multi-resolution Gauss-Seidel solver: solves the linear system by first
filtering (GS-iterate) the current level, then solves for the residual
at a coarcer resolution andfinally refines the solution at the current
resolution. This scheme corresponds to the V-cycle proposed by Bruhn and
Weickert[1].
[1] Andres Bruhn and Joachim Weickert, "Towards ultimate motion estimation:
combining highest accuracy with real-time performance",
10th IEEE International Conference on Computer Vision, 2005.
ICCV 2005.
'''
#presmoothing
for i in range(k):
tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
if n == 0:
try:
energy
except NameError:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
#solve at coarcer grid
residual = None
residual = tf.compute_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
residual)
sub_residual = np.array(tf.downsample_displacement_field(residual))
del residual
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field(sigma_field)
subdelta_field = tf.downsample_scalar_field(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
#sub_displacement = np.array(tf.downsample_displacement_field(displacement))
sub_displacement = np.zeros(shape = ((shape[0]+1)//2, (shape[1]+1)//2, 2 ),
dtype = np.float64)
sublambda_param = lambda_param*0.25
v_cycle_2d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration:', energy
#post-smoothing
for i in range(k):
tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
def w_cycle_2d(n, k, delta_field, sigma_field, gradient_field, target,
lambda_param, displacement, depth = 0):
r'''
Multi-resolution Gauss-Seidel solver: solves the linear system by performing
two v-cycles at each resolution, which corresponds to the w-cycle scheme
proposed by Bruhn and Weickert[1].
[1] Andres Bruhn and Joachim Weickert, "Towards ultimate motion estimation:
combining highest accuracy with real-time performance",
10th IEEE International Conference on Computer Vision, 2005.
ICCV 2005.
'''
#presmoothing
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [first]:', energy
if n == 0:
return error
residual = tf.compute_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
None)
sub_residual = np.array(tf.downsample_displacement_field(residual))
del residual
#solve at coarcer grid
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field(sigma_field)
subdelta_field = tf.downsample_scalar_field(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
#sub_displacement = np.array(tf.downsample_displacement_field(displacement))
sub_displacement = np.zeros(shape = ((shape[0]+1)//2, (shape[1]+1)//2, 2 ),
dtype = np.float64)
sublambda_param = lambda_param*0.25
w_cycle_2d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration[first]:', energy
#post-smoothing (second smoothing)
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [second]:', energy
residual = tf.compute_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
None)
sub_residual = np.array(tf.downsample_displacement_field(residual))
del residual
#sub_displacement = np.array(tf.downsample_displacement_field(displacement))
sub_displacement = np.zeros(shape = ((shape[0]+1)//2, (shape[1]+1)//2, 2 ),
dtype = np.float64)
w_cycle_2d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration[second]:', energy
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD2D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD2D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [third]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD2D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
def single_cycle_3d(n, k, delta_field, sigma_field, gradient_field,
lambda_param, displacement, depth = 0):
r'''
One-pass multi-resolution Gauss-Seidel solver: solves the SSD-like linear
system starting at the coarcer resolution and then refining at the finer
resolution.
'''
if n == 0:
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
None,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
return error
#solve at coarcer grid
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field3D(sigma_field)
subdelta_field = tf.downsample_scalar_field3D(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field3D(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
sub_displacement = np.zeros(
shape = ((shape[0]+1)//2, (shape[1]+1)//2, (shape[2]+1)//2, 3 ),
dtype = np.float64)
sublambda_param = lambda_param*0.25
single_cycle_3d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field3D(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration:', energy
#post-smoothing
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
None,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
def v_cycle_3d(n, k, delta_field, sigma_field, gradient_field, target,
lambda_param, displacement, depth = 0):
r'''
Multi-resolution Gauss-Seidel solver: solves the linear system by first
filtering (GS-iterate) the current level, then solves for the residual
at a coarcer resolution andfinally refines the solution at the current
resolution. This scheme corresponds to the V-cycle proposed by Bruhn and
Weickert[1].
[1] Andres Bruhn and Joachim Weickert, "Towards ultimate motion estimation:
combining highest accuracy with real-time performance",
10th IEEE International Conference on Computer Vision, 2005.
ICCV 2005.
'''
#presmoothing
for i in range(k):
tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
if n == 0:
try:
energy
except NameError:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
#solve at coarcer grid
residual = tf.compute_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
None)
sub_residual = np.array(tf.downsample_displacement_field3D(residual))
del residual
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field3D(sigma_field)
subdelta_field = tf.downsample_scalar_field3D(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field3D(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
sub_displacement = np.zeros(
shape = ((shape[0]+1)//2, (shape[1]+1)//2, (shape[2]+1)//2, 3 ),
dtype = np.float64)
sublambda_param = lambda_param*0.25
v_cycle_3d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
del subdelta_field
del subsigma_field
del subgradient_field
del sub_residual
tf.accumulate_upsample_displacement_field3D(sub_displacement, displacement)
del sub_displacement
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration:', energy
#post-smoothing
for i in range(k):
tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [unique]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy
def w_cycle_3d(n, k, delta_field, sigma_field, gradient_field, target,
lambda_param, displacement, depth = 0):
r'''
Multi-resolution Gauss-Seidel solver: solves the linear system by performing
two v-cycles at each resolution, which corresponds to the w-cycle scheme
proposed by Bruhn and Weickert[1].
[1] Andres Bruhn and Joachim Weickert, "Towards ultimate motion estimation:
combining highest accuracy with real-time performance",
10th IEEE International Conference on Computer Vision, 2005. ICCV 2005.
'''
#presmoothing
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [first]:', energy
if n == 0:
return error
residual = tf.compute_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
None)
sub_residual = np.array(tf.downsample_displacement_field3D(residual))
del residual
#solve at coarcer grid
subsigma_field = None
if sigma_field != None:
subsigma_field = tf.downsample_scalar_field3D(sigma_field)
subdelta_field = tf.downsample_scalar_field3D(delta_field)
subgradient_field = np.array(
tf.downsample_displacement_field3D(gradient_field))
shape = np.array(displacement.shape).astype(np.int32)
sub_displacement = np.zeros(
shape = ((shape[0]+1)//2, (shape[1]+1)//2, (shape[2]+1)//2, 3 ),
dtype = np.float64)
sublambda_param = lambda_param*0.25
w_cycle_3d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field3D(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration[first]:', energy
#post-smoothing (second smoothing)
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [second]:', energy
residual = tf.compute_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement,
None)
sub_residual = np.array(tf.downsample_displacement_field3D(residual))
del residual
sub_displacement[...] = 0
w_cycle_3d(n-1, k, subdelta_field, subsigma_field, subgradient_field,
sub_residual, sublambda_param, sub_displacement, depth+1)
displacement += np.array(
tf.upsample_displacement_field3D(sub_displacement, shape))
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
print 'Energy after low-res iteration[second]:', energy
for i in range(k):
error = tf.iterate_residual_displacement_field_SSD3D(delta_field,
sigma_field,
gradient_field,
target,
lambda_param,
displacement)
if printEnergy and depth == 0:
energy = tf.compute_energy_SSD3D(delta_field,
sigma_field,
gradient_field,
lambda_param,
displacement)
print 'Energy after top-level iter', i+1, ' [third]:', energy
try:
energy
except NameError:
energy = tf.compute_energy_SSD3D(delta_field, sigma_field,
gradient_field, lambda_param,
displacement)
return energy