-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_torch_osem_update.py
171 lines (137 loc) · 6.71 KB
/
04_torch_osem_update.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
from __future__ import annotations
import utils
import parallelproj
import array_api_compat.numpy as np
import array_api_compat.torch as torch
from array_api_compat import to_device
import matplotlib.pyplot as plt
from layers import EMUpdateModule
# device variable (cpu or cuda) that determines whether calculations
# are performed on the cpu or cuda gpu
if parallelproj.cuda_present:
dev = 'cuda'
else:
dev = 'cpu'
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
#--- setup the scanner / LOR geometry ---------------------------------------
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# setup a line of response descriptor that describes the LOR start / endpoints of
# a "narrow" clinical PET scanner with 9 rings
lor_descriptor = utils.DemoPETScannerLORDescriptor(torch, dev, num_rings=4)
# image properties
voxel_size = (2.66, 2.66, 2.66)
n0 = 160
n1 = n0
img_shape = (n0, n1, 2 * lor_descriptor.scanner.num_modules)
num_subsets = 34
subset_projectors = parallelproj.SubsetOperator([
utils.RegularPolygonPETProjector(lor_descriptor,
img_shape,
voxel_size,
views=torch.arange(
i, lor_descriptor.num_views,
num_subsets, device = dev))
for i in range(num_subsets)
])
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
batch_size = 2
emission_image_batch = torch.zeros(
(batch_size, 1) + subset_projectors.in_shape,
device=dev,
dtype=torch.float32,
requires_grad=False)
emission_image_batch[:, 0, (n0 // 4):(3 * n0 // 4),
(n1 // 4):(3 * n1 // 4), :] = 1.
emission_image_batch[0, 0, (9 * n0 // 16):(11 * n0 // 16),
(9 * n1 // 16):(11 * n1 // 16), :] *= 2
emission_image_batch[1, 0, (5 * n0 // 16):(7 * n0 // 16),
(5 * n1 // 16):(7 * n1 // 16), :] *= 0.5
attenuation_image_batch = 0.01 * (emission_image_batch > 0)
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# mini batch of multiplicative corrections (attenuation and normalization)
correction_batch = torch.zeros(
(num_subsets, batch_size) + subset_projectors.out_shapes[0],
device=dev,
dtype=torch.float32)
# mini batch of emission data
emission_data_batch = torch.zeros(
(num_subsets, batch_size) + subset_projectors.out_shapes[0],
device=dev,
dtype=torch.float32)
# calculate the adjoint ones (back projection of the multiplicative corrections) - sensitivity images
adjoint_ones_batch = torch.zeros(
(num_subsets, batch_size, 1) + subset_projectors.in_shape,
device=dev,
dtype=torch.float32)
# mini batch of additive contamination (scatter)
contamination_batch = torch.zeros(
(num_subsets, batch_size) + subset_projectors.out_shapes[0],
device=dev,
dtype=torch.float32)
for j in range(num_subsets):
for i in range(batch_size):
correction_batch[j, i,
...] = torch.exp(-subset_projectors.apply_subset(
attenuation_image_batch[i, 0, ...], j))
adjoint_ones_batch[j, i, 0, ...] = subset_projectors.adjoint_subset(
correction_batch[j, i, ...], j)
emission_data_batch[j, i, ...] = correction_batch[
j, i, ...] * subset_projectors.apply_subset(
emission_image_batch[i, 0, ...], j)
contamination_batch[j, i, ...] = emission_data_batch[j, i, ...].mean()
emission_data_batch[j, i, ...] += contamination_batch[j, i, ...]
emission_data_batch[j, i,
...] = torch.poisson(emission_data_batch[j, i,
...])
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
osem_update_modules = [
EMUpdateModule(projector) for projector in subset_projectors.operators
]
x = torch.ones((batch_size, 1) + subset_projectors.in_shape,
device=dev,
dtype=torch.float32)
num_iter = 136 // num_subsets
for i in range(num_iter):
subset_order = torch.randperm(num_subsets)
for j in range(num_subsets):
subset = subset_order[j]
print(f'OSEM iteration {(subset+1):03}/{(i+1):03}/{num_iter:03}',
end='\r')
x = osem_update_modules[subset](x, emission_data_batch[subset, ...],
correction_batch[subset, ...],
contamination_batch[subset, ...],
adjoint_ones_batch[subset, ...])
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
sl = 1
kwgs = dict(vmax=1.1 * float(emission_image_batch.max()), cmap='Greys')
fig, ax = plt.subplots(2, 2, figsize=(8, 8))
ax[0, 0].imshow(
np.asarray(to_device(emission_image_batch[0, 0, :, :, sl], 'cpu')), **kwgs)
ax[0, 1].imshow(
np.asarray(to_device(emission_image_batch[1, 0, :, :, sl], 'cpu')), **kwgs)
ax[1, 0].imshow(np.asarray(to_device(x[0, 0, :, :, sl], 'cpu')), **kwgs)
ax[1, 1].imshow(np.asarray(to_device(x[1, 0, :, :, sl], 'cpu')), **kwgs)
ax[0, 0].set_title(f'true img - slice {sl} - batch item 0', fontsize='small')
ax[0, 1].set_title(f'true img - slice {sl} - batch item 1', fontsize='small')
ax[1, 0].set_title(
f'OSEM {num_iter}it/{num_subsets}ss - slice {sl} - batch item 0',
fontsize='small')
ax[1, 1].set_title(
f'OSEM {num_iter}it/{num_subsets}ss - slice {sl} - batch item 1',
fontsize='small')
fig.tight_layout()
fig.show()