-
Notifications
You must be signed in to change notification settings - Fork 5
/
profile_tfkbnufft.py
94 lines (74 loc) · 2.76 KB
/
profile_tfkbnufft.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
import time
import numpy as np
from PIL import Image
from skimage.data import camera
import tensorflow as tf
from tfkbnufft import kbnufft_forward, kbnufft_adjoint
from tfkbnufft.kbnufft import KbNufftModule
def profile_tfkbnufft(
image,
ktraj,
im_size,
device,
):
if device == 'CPU':
num_nuffts = 20
else:
num_nuffts = 50
print(f'Using {device}')
device_name = f'/{device}:0'
with tf.device(device_name):
image = tf.constant(image)
if device == 'GPU':
image = tf.cast(image, tf.complex64)
ktraj = tf.constant(ktraj)
nufft_ob = KbNufftModule(im_size=im_size, grid_size=None, norm='ortho')
forward_op = kbnufft_forward(nufft_ob._extract_nufft_interpob())
adjoint_op = kbnufft_adjoint(nufft_ob._extract_nufft_interpob())
# warm-up computation
for _ in range(2):
y = forward_op(image, ktraj)
start_time = time.perf_counter()
for _ in range(num_nuffts):
y = forward_op(image, ktraj)
end_time = time.perf_counter()
avg_time = (end_time-start_time) / num_nuffts
print('forward average time: {}'.format(avg_time))
# warm-up computation
for _ in range(2):
x = adjoint_op(y, ktraj)
# run the adjoint speed tests
start_time = time.perf_counter()
for _ in range(num_nuffts):
x = adjoint_op(y, ktraj)
end_time = time.perf_counter()
avg_time = (end_time-start_time) / num_nuffts
print('backward average time: {}'.format(avg_time))
def run_all_profiles():
print('running profiler...')
spokelength = 512
nspokes = 405
print('problem size (radial trajectory, 2-factor oversampling):')
print('number of spokes: {}'.format(nspokes))
print('spokelength: {}'.format(spokelength))
# create an example to run on
image = np.array(Image.fromarray(camera()).resize((256, 256)))
image = image.astype(np.complex)
im_size = image.shape
image = image[None, None, ...]
# create k-space trajectory
ga = np.deg2rad(180 / ((1 + np.sqrt(5)) / 2))
kx = np.zeros(shape=(spokelength, nspokes))
ky = np.zeros(shape=(spokelength, nspokes))
ky[:, 0] = np.linspace(-np.pi, np.pi, spokelength)
for i in range(1, nspokes):
kx[:, i] = np.cos(ga) * kx[:, i - 1] - np.sin(ga) * ky[:, i - 1]
ky[:, i] = np.sin(ga) * kx[:, i - 1] + np.cos(ga) * ky[:, i - 1]
ky = np.transpose(ky)
kx = np.transpose(kx)
ktraj = np.stack((ky.flatten(), kx.flatten()), axis=0)
ktraj = ktraj[None, ...]
profile_tfkbnufft(image, ktraj, im_size, device='CPU')
profile_tfkbnufft(image, ktraj, im_size, device='GPU')
if __name__ == '__main__':
run_all_profiles()