-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_utils.py
708 lines (539 loc) · 21 KB
/
sim_utils.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
from __future__ import annotations
import abc
import parallelproj
import array_api_compat.numpy as np
from array_api_compat import get_namespace, device
from types import ModuleType
from typing import TypeAlias, Callable
Array: TypeAlias = np.ndarray
try:
import array_api_compat.cupy as cp
Array: TypeAlias = Array | cp.ndarray
except:
pass
from copy import copy
from rdp import SmoothFunction, SmoothFunctionWithDiagonalHessian
class SmoothSubsetFunction(abc.ABC):
def __init__(
self,
in_shape: tuple[int],
num_subsets: int,
xp: ModuleType,
dev: str,
) -> None:
self._in_shape = in_shape
self._num_subsets = num_subsets
self._xp = xp
self._dev = dev
self._subset_gradients = None
@property
def in_shape(self) -> tuple[int, ...]:
return self._in_shape
@property
def num_subsets(self) -> int:
return self._num_subsets
@property
def xp(self):
return self._xp
@property
def dev(self):
return self._dev
@abc.abstractmethod
def call_subset(self, x: Array, subset: int) -> float:
raise NotImplementedError
@abc.abstractmethod
def subset_gradient(self, x: Array, subset: int) -> Array:
raise NotImplementedError
@abc.abstractmethod
def all_subset_gradients(self, x: Array) -> list[Array]:
raise NotImplementedError
def __call__(self, x: Array) -> float:
x = self._xp.asarray(x, device=self._dev)
flat_input = x.ndim == 1
if flat_input:
x = self._xp.reshape(x, self._in_shape)
res = sum([self.call_subset(x, subset) for subset in range(self._num_subsets)])
return res
def gradient(self, x: Array) -> Array:
dev_input = device(x)
if dev_input != self._dev:
x = self._xp.asarray(x, device=self._dev)
flat_input = x.ndim == 1
if flat_input:
x = self._xp.reshape(x, self._in_shape)
res = sum(self.all_subset_gradients(x))
if flat_input:
res = self._xp.reshape(res, (res.size,))
if dev_input != self._dev:
res = self._xp.to_device(res, dev_input)
return res
class SubsetNegPoissonLogLWithPrior(SmoothSubsetFunction):
def __init__(
self,
data: Array,
subset_fwd_operators: parallelproj.LinearOperatorSequence,
contamination: Array,
subset_slices: list[tuple[slice, ...]],
prior: None | SmoothFunction = None,
) -> None:
self._data = data
self._subset_fwd_operators = subset_fwd_operators
self._contamination = contamination
self._subset_slices = subset_slices
self._prior = prior
self._adjoint_ones = None
super().__init__(
in_shape=subset_fwd_operators[0].in_shape,
num_subsets=len(subset_fwd_operators),
xp=get_namespace(data),
dev=device(data),
)
@property
def data(self) -> Array:
return self._data
@property
def subset_fwd_operators(self) -> parallelproj.LinearOperatorSequence:
return self._subset_fwd_operators
@property
def contamination(self) -> Array:
return self._contamination
@property
def subset_slices(self) -> list[slice]:
return self._subset_slices
@property
def prior(self) -> None | SmoothFunction:
return self._prior
def call_subset(self, x: Array, subset: int) -> float:
sl = self._subset_slices[subset]
exp = self._subset_fwd_operators[subset](x) + self._contamination[sl]
res = float(self.xp.sum(exp - self._data[sl] * self.xp.log(exp)))
if self._prior is not None:
res += self._prior(x) / self._num_subsets
return res
def subset_gradient(self, x: Array, subset: int) -> Array:
sl = self._subset_slices[subset]
exp = self._subset_fwd_operators[subset](x) + self._contamination[sl]
res = self._subset_fwd_operators[subset].adjoint((exp - self._data[sl]) / exp)
if self._prior is not None:
res += self._prior.gradient(x) / self._num_subsets
return res
def all_subset_gradients(self, x: Array) -> list[Array]:
"""dedicated implementation to avoid multiple evaluations of the prior"""
if self._prior is not None:
prior_grad = self._prior.gradient(x)
subset_grads = []
for subset in range(self._num_subsets):
sl = self._subset_slices[subset]
exp = self._subset_fwd_operators[subset](x) + self._contamination[sl]
subset_grad = self._subset_fwd_operators[subset].adjoint(
(exp - self._data[sl]) / exp
)
if self._prior is not None:
subset_grad += prior_grad / self._num_subsets
subset_grads.append(subset_grad)
return subset_grads
class OSEM:
def __init__(self, data_fidelity: SubsetNegPoissonLogLWithPrior) -> None:
self._data_fidelity = data_fidelity
self._num_subsets = data_fidelity.num_subsets
self._adjoint_ones = []
for i in range(self._num_subsets):
self._adjoint_ones.append(
data_fidelity.subset_fwd_operators[i].adjoint(
data_fidelity.xp.ones(
data_fidelity.subset_fwd_operators[i].out_shape,
device=data_fidelity.dev,
)
)
)
def update(self, x: Array, subset: int) -> Array:
step = x / self._adjoint_ones[subset]
return x - step * self._data_fidelity.subset_gradient(x, subset)
def run(self, x: Array, num_epochs: int) -> Array:
for _ in range(num_epochs):
for subset in range(self._num_subsets):
x = self.update(x, subset)
return x
def split_fwd_model(
pet_lin_op: parallelproj.CompositeLinearOperator,
num_subsets: int,
):
"""split PET forward model into a sequence of subset forward operators"""
att_sino: Array = pet_lin_op[0].values
proj: parallelproj.RegularPolygonPETProjector = pet_lin_op[1]
res_model: parallelproj.LinearOperator = pet_lin_op[2]
subset_views, subset_slices = proj.lor_descriptor.get_distributed_views_and_slices(
num_subsets, len(proj.out_shape)
)
_, subset_slices_non_tof = proj.lor_descriptor.get_distributed_views_and_slices(
num_subsets, 3
)
# clear the cached LOR endpoints since we will create many copies of the projector
proj.clear_cached_lor_endpoints()
pet_subset_linop_seq = []
# we setup a sequence of subset forward operators each constisting of
# (1) image-based resolution model
# (2) subset projector
# (3) multiplication with the corresponding subset of the attenuation sinogram
for i in range(num_subsets):
# make a copy of the full projector and reset the views to project
subset_proj = copy(proj)
subset_proj.views = subset_views[i]
if subset_proj.tof:
subset_att_op = parallelproj.TOFNonTOFElementwiseMultiplicationOperator(
subset_proj.out_shape, att_sino[subset_slices_non_tof[i]]
)
else:
subset_att_op = parallelproj.ElementwiseMultiplicationOperator(
att_sino[subset_slices_non_tof[i]]
)
# add the resolution model and multiplication with a subset of the attenuation sinogram
pet_subset_linop_seq.append(
parallelproj.CompositeLinearOperator(
[
subset_att_op,
subset_proj,
res_model,
]
)
)
pet_subset_lin_op_seq = parallelproj.LinearOperatorSequence(pet_subset_linop_seq)
return pet_subset_lin_op_seq, subset_slices
class SVRG:
def __init__(
self,
subset_neglogL: SmoothSubsetFunction,
prior: SmoothFunctionWithDiagonalHessian,
x_init: Array,
complete_gradient_epochs: None | list[int] = None,
precond_update_epochs: None | list[int] = None,
verbose: bool = False,
seed: int = 1,
precond_version: int = 2,
):
np.random.seed(seed)
self._verbose = verbose
self._subset = 0
self._x = x_init
self._subset_neglogL = subset_neglogL
self._prior = prior
self._num_subsets = subset_neglogL.num_subsets
self._xp = get_namespace(x_init)
self._dev = device(x_init)
self._adjoint_ones = self._xp.zeros_like(x_init)
# calculate adjoint ones of data operator
for i in range(self._num_subsets):
self._adjoint_ones += self._subset_neglogL.subset_fwd_operators[i].adjoint(
self._xp.ones(
self._subset_neglogL.subset_fwd_operators[i].out_shape,
device=self._dev,
)
)
if complete_gradient_epochs is None:
self._complete_gradient_epochs: list[int] = [x for x in range(0, 1000, 2)]
else:
self._complete_gradient_epochs = complete_gradient_epochs
if precond_update_epochs is None:
self._precond_update_epochs: list[int] = [1, 2, 3]
else:
self._precond_update_epochs = precond_update_epochs
self._precond_filter = parallelproj.GaussianFilterOperator(
x_init.shape, sigma=2.0
)
self._update = 0
self._step_size_factor = 1.0
self._subset_number_list = []
self._precond_version = precond_version
self._precond = self.calc_precond(self._x)
self._step_size = 1.0
self._prior_diag_hess = None
@property
def epoch(self) -> int:
return self._update // self._num_subsets
@property
def x(self) -> Array:
return self._x
def update_step_size(self):
if self.epoch <= 4:
self._step_size = self._step_size_factor * 2.0
elif self.epoch > 4 and self.epoch <= 8:
self._step_size = self._step_size_factor * 1.5
elif self.epoch > 8 and self.epoch <= 12:
self._step_size = self._step_size_factor * 1.0
else:
self._step_size = self._step_size_factor * 0.5
if self._verbose:
print(self._update, self.epoch, self._step_size)
def calc_precond(
self,
x: Array,
delta_rel: float = 1e-6,
) -> Array:
# generate a smoothed version of the input image
# to avoid high values, especially in first and last slices
x_sm = self._precond_filter(x)
delta = delta_rel * x_sm.max()
if self._precond_version == 1:
precond = (x_sm + delta) / self._adjoint_ones
elif self._precond_version == 2:
self._prior_diag_hess = self._prior.diag_hessian(x_sm)
precond = (x_sm + delta) / (
self._adjoint_ones + 2 * self._prior_diag_hess * x_sm
)
else:
raise ValueError(f"Unknown preconditioner version {self._precond_version}")
return precond
def update_all_subset_gradients(self) -> None:
self._subset_gradients = []
subset_prior_gradient = self._prior.gradient(self._x) / self._num_subsets
for i in range(self._num_subsets):
self._subset_gradients.append(
self._subset_neglogL.subset_gradient(self._x, i) + subset_prior_gradient
)
self._summed_subset_gradients = sum(self._subset_gradients)
def update(self):
update_all_subset_gradients = (
self._update % self._num_subsets == 0
) and self.epoch in self._complete_gradient_epochs
update_precond = (
self._update % self._num_subsets == 0
) and self.epoch in self._precond_update_epochs
if self._update % self._num_subsets == 0:
self.update_step_size()
if update_precond:
if self._verbose:
print(f" {self._update}, updating preconditioner")
self._precond = self.calc_precond(self._x)
if update_all_subset_gradients:
if self._verbose:
print(
f" {self._update}, {self._subset}, recalculating all subset gradients"
)
self.update_all_subset_gradients()
approximated_gradient = self._summed_subset_gradients
else:
if self._subset_number_list == []:
self.create_subset_number_list()
self._subset = self._subset_number_list.pop()
if self._verbose:
print(f" {self._update}, {self._subset}, subset gradient update")
subset_prior_gradient = self._prior.gradient(self._x) / self._num_subsets
approximated_gradient = (
self._num_subsets
* (
(
self._subset_neglogL.subset_gradient(self._x, self._subset)
+ subset_prior_gradient
)
- self._subset_gradients[self._subset]
)
+ self._summed_subset_gradients
)
self._x = self._x - self._step_size * self._precond * approximated_gradient
# enforce non-negative constraint
self._xp.clip(self._x, 0, None, out=self._x)
self._update += 1
def run(
self, num_updates: int, callback: None | Callable[[Array], float] = None
) -> list:
callback_res = []
for _ in range(num_updates):
if callback is not None:
callback_res.append(callback(self._x))
self.update()
return callback_res
def create_subset_number_list(self):
tmp = np.arange(self._num_subsets)
np.random.shuffle(tmp)
self._subset_number_list = tmp.tolist()
class ProxRDP:
def __init__(
self,
prior: SmoothFunctionWithDiagonalHessian,
init_step: float = 1.0,
niter: int = 5,
adaptive_step_size: bool = True,
up: float = 1.1,
down: float = 2.0,
):
self._prior = prior
self._step = init_step
self._niter = niter
self._up = up
self._down = down
self._adaptive_step_size = adaptive_step_size
@property
def prior(self) -> SmoothFunctionWithDiagonalHessian:
return self._prior
def __call__(
self, z, tau, T: float | Array = 1.0, precond: float | Array = 1.0
) -> Array:
xp = get_namespace(z)
u = xp.clip(z, 0, None)
for k in range(self._niter):
# compute gradient step
grad = (u - z) / T + tau * self._prior.gradient(u)
tmp = u - self._step * precond * grad
u_new = xp.clip(tmp, 0, None)
# update step size
if self._adaptive_step_size:
diff_new = xp.linalg.norm(u_new - u)
if k == 0:
u = u_new
diff = diff_new
else:
if diff_new <= diff:
self._step *= self._up
u = u_new
diff = diff_new
else:
self._step /= self._down
else:
u = u_new
return u
class ProxSVRG:
def __init__(
self,
subset_neglogL: SmoothSubsetFunction,
prior_prox,
x_init: Array,
complete_gradient_epochs: None | list[int] = None,
precond_update_epochs: None | list[int] = None,
verbose: bool = False,
seed: int = 1,
precond_version: int = 2,
**kwargs,
):
np.random.seed(seed)
self._verbose = verbose
self._subset = 0
self._x = x_init
self._subset_neglogL = subset_neglogL
self._num_subsets = subset_neglogL.num_subsets
self._xp = get_namespace(x_init)
self._dev = device(x_init)
self._adjoint_ones = self._xp.zeros_like(x_init)
# calculate adjoint ones of data operator
for i in range(self._num_subsets):
self._adjoint_ones += self._subset_neglogL.subset_fwd_operators[i].adjoint(
self._xp.ones(
self._subset_neglogL.subset_fwd_operators[i].out_shape,
device=self._dev,
)
)
if complete_gradient_epochs is None:
self._complete_gradient_epochs: list[int] = [x for x in range(0, 1000, 2)]
else:
self._complete_gradient_epochs = complete_gradient_epochs
if precond_update_epochs is None:
self._precond_update_epochs: list[int] = [1, 2, 3]
else:
self._precond_update_epochs = precond_update_epochs
self._precond_filter = parallelproj.GaussianFilterOperator(
x_init.shape, sigma=2.0
)
self._update = 0
self._step_size_factor = 1.0
self._subset_number_list = []
self._prior_prox = prior_prox
self._prior_diag_hess = None
self._precond_version = precond_version
self._precond = self.calc_precond(self._x)
self._step_size = 1.0
self._subset_gradients = []
self._summed_subset_gradients = None
@property
def epoch(self) -> int:
return self._update // self._num_subsets
@property
def x(self) -> Array:
return self._x
def update_step_size(self):
if self.epoch <= 4:
self._step_size = self._step_size_factor * 2.0
elif self.epoch > 4 and self.epoch <= 8:
self._step_size = self._step_size_factor * 1.5
elif self.epoch > 8 and self.epoch <= 12:
self._step_size = self._step_size_factor * 1.0
else:
self._step_size = self._step_size_factor * 0.5
if self._verbose:
print(self._update, self.epoch, self._step_size)
def calc_precond(
self,
x: Array,
delta_rel: float = 1e-6,
) -> Array:
# generate a smoothed version of the input image
# to avoid high values, especially in first and last slices
x_sm = self._precond_filter(x)
delta = delta_rel * x_sm.max()
if self._precond_version == 1:
precond = (x_sm + delta) / self._adjoint_ones
elif self._precond_version == 2:
self._prior_diag_hess = self._prior_prox.prior.diag_hessian(x_sm)
precond = (x_sm + delta) / (
self._adjoint_ones + 2 * self._prior_diag_hess * x_sm
)
else:
raise ValueError(f"Unknown preconditioner version {self._precond_version}")
return precond
def update_all_subset_gradients(self) -> None:
self._subset_gradients = self._subset_neglogL.all_subset_gradients(self._x)
self._summed_subset_gradients = sum(self._subset_gradients)
def update(self):
update_all_subset_gradients = (
self._update % self._num_subsets == 0
) and self.epoch in self._complete_gradient_epochs
update_precond = (
self._update % self._num_subsets == 0
) and self.epoch in self._precond_update_epochs
if self._update % self._num_subsets == 0:
self.update_step_size()
if update_precond:
if self._verbose:
print(f" {self._update}, updating preconditioner")
self._precond = self.calc_precond(self._x)
if update_all_subset_gradients:
if self._verbose:
print(
f" {self._update}, {self._subset}, recalculating all subset gradients"
)
self.update_all_subset_gradients()
approximated_gradient = self._summed_subset_gradients
else:
if self._subset_number_list == []:
self.create_subset_number_list()
self._subset = self._subset_number_list.pop()
if self._verbose:
print(f" {self._update}, {self._subset}, subset gradient update")
approximated_gradient = (
self._num_subsets
* (
self._subset_neglogL.subset_gradient(self._x, self._subset)
- self._subset_gradients[self._subset]
)
+ self._summed_subset_gradients
)
tmp = self._x - self._step_size * self._precond * approximated_gradient
tau = self._step_size
T = self._precond
if self._precond_version == 1:
pc = 1 / T
elif self._precond_version == 2:
pc = 1 / (1 / T + tau * self._prior_diag_hess)
self._x = self._prior_prox(tmp, tau=tau, T=T, precond=pc)
self._update += 1
def run(
self, num_updates: int, callback: None | Callable[[Array], float] = None
) -> list:
callback_res = []
for _ in range(num_updates):
if callback is not None:
callback_res.append(callback(self._x))
self.update()
return callback_res
def create_subset_number_list(self):
tmp = np.arange(self._num_subsets)
np.random.shuffle(tmp)
self._subset_number_list = tmp.tolist()