-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_torch_em_update.py
144 lines (112 loc) · 6.08 KB
/
02_torch_em_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
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=2)
# image properties
voxel_size = (2.66, 2.66, 2.66)
n0 = 160
n1 = n0
img_shape = (n0, n1, 2 * lor_descriptor.scanner.num_modules)
projector = utils.RegularPolygonPETProjector(lor_descriptor, img_shape,
voxel_size)
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
batch_size = 2
emission_image_batch = torch.zeros((batch_size, 1) + projector.in_shape,
device=dev,
dtype=torch.float32,
requires_grad=False)
emission_image_batch[:, 0, (n0 // 4):(3 * n0 // 4),
(n1 // 4):(3 * n1 // 4), :] = 0.4
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((batch_size, ) + projector.out_shape,
device=dev,
dtype=torch.float32)
# mini batch of emission data
emission_data_batch = torch.zeros((batch_size, ) + projector.out_shape,
device=dev,
dtype=torch.float32)
# calculate the adjoint ones (back projection of the multiplicative corrections) - sensitivity images
adjoint_ones_batch = torch.zeros((batch_size, 1) + projector.in_shape,
device=dev,
dtype=torch.float32)
# mini batch of additive contamination (scatter)
contamination_batch = torch.zeros((batch_size, ) + projector.out_shape,
device=dev,
dtype=torch.float32)
for i in range(batch_size):
correction_batch[i,
...] = torch.exp(-projector(attenuation_image_batch[i, 0,
...]))
emission_data_batch[i, ...] = correction_batch[i, ...] * projector(
emission_image_batch[i, 0, ...])
contamination_batch[i, ...] = emission_data_batch[i, ...].mean()
emission_data_batch[i, ...] += contamination_batch[i, ...]
emission_data_batch[i, ...] = torch.poisson(emission_data_batch[i, ...])
adjoint_ones_batch[i, 0, ...] = projector.adjoint(correction_batch[i, ...])
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# the EMUpdateModule that subclasses torch.nn.Module is defined in layers.py
# have look at the code there to see how the forward pass is implemtend
em_update_module = EMUpdateModule(projector)
x = torch.ones((batch_size, 1) + projector.in_shape,
device=dev,
dtype=torch.float32)
num_iter = 50
for i in range(num_iter):
print(f'EM iteration {(i+1):03}/{num_iter:03}', end='\r')
x = em_update_module(x, emission_data_batch, correction_batch,
contamination_batch, adjoint_ones_batch)
print('')
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
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'MLEM {num_iter}it - slice {sl} - batch item 0',
fontsize='small')
ax[1, 1].set_title(f'MLEM {num_iter}it- slice {sl} - batch item 1',
fontsize='small')
fig.tight_layout()
fig.show()