This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
fftconvolve.py
145 lines (102 loc) · 3.76 KB
/
fftconvolve.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
'''
This script executes 2D FFT convolution on images in grayscale.
Usage:
Run without argument will use builtin Lena image:
python fftconvolve.py
Or, specify an image to use
python fftconvolve.py myimage.jpg
python fftconvolve.py myimage.png
= Getting The Requirements =
For Conda user, run the following to ensure the dependencies are fulfilled:
conda install scipy matplotlib
You may need to install PIL from pip.
conda install pip
pip install PIL
'''
from __future__ import print_function
import sys
from timeit import default_timer as timer
import numpy as np
from scipy.signal import fftconvolve
from scipy import misc, ndimage
from matplotlib import pyplot as plt
from accelerate.cuda.fft import FFTPlan
from numba import cuda
@cuda.jit('void(complex64[:,:], complex64[:,:])')
def mult_inplace(img, resp):
i, j = cuda.grid(2)
if j < img.shape[0] and i < img.shape[1]:
img[j, i] *= resp[j, i]
def best_grid_size(size, tpb):
bpg = np.ceil(np.array(size, dtype=np.float) / tpb).astype(np.int).tolist()
return tuple(bpg)
def main():
# Build Filter
laplacian_pts = '''
-4 -1 0 -1 -4
-1 2 3 2 -1
0 3 4 3 0
-1 2 3 2 -1
-4 -1 0 -1 -4
'''.split()
laplacian = np.array(laplacian_pts, dtype=np.float32).reshape(5, 5)
# Build Image
try:
filename = sys.argv[1]
image = ndimage.imread(filename, flatten=True).astype(np.float32)
except IndexError:
image = misc.face(gray=True).astype(np.float32)
print("Image size: %s" % (image.shape,))
response = np.zeros_like(image)
response[:5, :5] = laplacian
# CPU
ts = timer()
cvimage_cpu = fftconvolve(image, laplacian, mode='same')
te = timer()
print('CPU: %.2fs' % (te - ts))
# GPU
threadperblock = 32, 8
blockpergrid = best_grid_size(tuple(reversed(image.shape)), threadperblock)
print('kernel config: %s x %s' % (blockpergrid, threadperblock))
# Trigger initialization the cuFFT system.
# This takes significant time for small dataset.
# We should not be including the time wasted here
FFTPlan(shape=image.shape, itype=np.complex64, otype=np.complex64)
# Start GPU timer
ts = timer()
image_complex = image.astype(np.complex64)
response_complex = response.astype(np.complex64)
stream1 = cuda.stream()
stream2 = cuda.stream()
fftplan1 = FFTPlan(shape=image.shape, itype=np.complex64,
otype=np.complex64, stream=stream1)
fftplan2 = FFTPlan(shape=image.shape, itype=np.complex64,
otype=np.complex64, stream=stream2)
# pagelock memory
with cuda.pinned(image_complex, response_complex):
# We can overlap the transfer of response_complex with the forward FFT
# on image_complex.
d_image_complex = cuda.to_device(image_complex, stream=stream1)
d_response_complex = cuda.to_device(response_complex, stream=stream2)
fftplan1.forward(d_image_complex, out=d_image_complex)
fftplan2.forward(d_response_complex, out=d_response_complex)
stream2.synchronize()
mult_inplace[blockpergrid, threadperblock, stream1](d_image_complex,
d_response_complex)
fftplan1.inverse(d_image_complex, out=d_image_complex)
# implicitly synchronizes the streams
cvimage_gpu = d_image_complex.copy_to_host().real / np.prod(image.shape)
te = timer()
print('GPU: %.2fs' % (te - ts))
# Plot the results
plt.subplot(1, 2, 1)
plt.title('CPU')
plt.imshow(cvimage_cpu, cmap=plt.cm.gray)
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('GPU')
plt.imshow(cvimage_gpu, cmap=plt.cm.gray)
plt.axis('off')
plt.show()
if __name__ == '__main__':
main()