-
Notifications
You must be signed in to change notification settings - Fork 16
/
simple_cl_conv.py
247 lines (182 loc) · 7.78 KB
/
simple_cl_conv.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
import pyopencl as cl
import pyopencl.array as cl_array
from pyopencl import clmath
from pyopencl.elementwise import ElementwiseKernel
import numpy as np
import numpy.testing as nptest
from localmem_cl_conv import LocalMemorySeparableCorrelation
class NaiveSeparableCorrelation:
def __init__(self, ctx, queue):
self.ctx = ctx
self.queue = queue
code = """
__kernel void separable_correlation_row(__global float *result,
__global const float *input,
int image_width,
int image_height,
__global const float *kernel_row,
int kernel_width){
const int kernel_radius = kernel_width / 2;
int row = get_global_id(0);
int col = get_global_id(1);
float sum = 0.0;
int im_index = row * image_width + col;
for(int i = 0; i < kernel_width; i++){
int k = i - kernel_radius;
if( (col + k) < 0 ){
k *= -1;
k -= 1;
}
if( (col + k) >= image_width){
k *= -1;
k += 1;
}
sum += input[im_index + k] * kernel_row[i];
}
result[im_index] = sum;
return;
}
__kernel void separable_correlation_col(__global float *result,
__global const float *input,
int image_width,
int image_height,
__global const float *kernel_col,
int kernel_width){
const int kernel_radius = kernel_width / 2;
int row = get_global_id(0);
int col = get_global_id(1);
float sum = 0.0;
for(int i = 0; i < kernel_width; i++){
int k = i - kernel_radius;
if( (row + k) < 0 ){
k *= -1;
k -= 1;
}
if( (row + k) >= image_height ){
k *= -1;
k += 1;
}
int im_index = (row + k) * image_width + col;
sum = sum + input[im_index]*kernel_col[i];
}
result[row * image_width + col] = sum;
}
"""
self.program = cl.Program(self.ctx, code).build()
def __call__(self,
input_buf,
row_buf,
col_buf,
output_buf,
intermed_buf=None):
(h, w) = input_buf.shape
r = row_buf.shape[0]
c = col_buf.shape[0]
if intermed_buf is None:
intermed_buf = cl_array.empty_like(input_buf)
self.program.separable_correlation_row(self.queue,
(h, w),
None,
intermed_buf.data,
input_buf.data,
np.int32(w), np.int32(h),
row_buf.data,
np.int32(r))
self.program.separable_correlation_col(self.queue,
(h, w),
None,
output_buf.data,
intermed_buf.data,
np.int32(w), np.int32(h),
col_buf.data,
np.int32(c))
class Sobel:
def __init__(self, ctx, queue, dtype=np.float32):
self.ctx = ctx
self.queue = queue
sobel_c = np.array([1., 0., -1.]).astype(dtype)
sobel_r = np.array([1., 2., 1.]).astype(dtype)
self.sobel_c = cl_array.to_device(self.queue, sobel_c)
self.sobel_r = cl_array.to_device(self.queue, sobel_r)
self.scratch = None
self.sepconv_rc = LocalMemorySeparableCorrelation(self.ctx, self.queue, sobel_r, sobel_c)
self.sepconv_cr = LocalMemorySeparableCorrelation(self.ctx, self.queue, sobel_c, sobel_r)
TYPE = ""
if dtype == np.float32:
TYPE = "float"
elif dtype == np.uint8:
TYPE = "unsigned char"
elif dtype == np.uint16:
TYPE = "unsigned short"
self.mag = ElementwiseKernel(ctx,
"float *result, %s *imgx, %s *imgy" % (TYPE, TYPE),
"result[i] = sqrt((float)imgx[i]*imgx[i] + (float)imgy[i]*imgy[i])",
"mag")
def __call__(self,
input_buf,
imgx_buf,
imgy_buf,
mag_buf):
if self.scratch is None or self.scratch.shape != input_buf.shape:
self.scratch = cl_array.empty_like(input_buf)
self.sepconv_cr(input_buf, self.sobel_c, self.sobel_r, imgx_buf, self.scratch)
self.sepconv_rc(input_buf, self.sobel_r, self.sobel_c, imgy_buf, self.scratch)
self.mag(mag_buf, imgx_buf, imgy_buf)
def cl_test_sobel(im):
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
sobel = Sobel(ctx, queue)
im_buf = cl_array.to_device(queue, im)
mag_buf = cl_array.empty_like(im_buf)
imgx_buf = cl_array.empty_like(im_buf)
imgy_buf = cl_array.empty_like(im_buf)
sobel(im_buf, imgx_buf, imgy_buf, mag_buf)
return (mag_buf.get(), imgx_buf.get(), imgy_buf.get())
if __name__ == '__main__':
import matplotlib.pylab as plt
if True:
test_im = np.random.rand(217, 101).astype(np.float32)
row_k = np.random.rand(5,).astype(np.float32)
col_k = np.random.rand(5,).astype(np.float32)
elif False:
a = np.array(range(10, 1, -1), dtype=np.float32)
test_im = np.outer(a, a)
row_k = np.array([1, 2, 3]).astype(np.float32)
col_k = np.array([5, 6, 7]).astype(np.float32)
else:
test_im = np.ones([10, 10]).astype(np.float32)
row_k = np.array([1, 2, 3]).astype(np.float32)
col_k = np.array([2, 4, 5]).astype(np.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
in_buf = cl_array.to_device(queue, test_im)
row_buf = cl_array.to_device(queue, row_k)
col_buf = cl_array.to_device(queue, col_k)
out_buf = cl_array.empty_like(in_buf)
imgx_buf = cl_array.empty_like(in_buf)
imgy_buf = cl_array.empty_like(in_buf)
mag_buf = cl_array.empty_like(in_buf)
# Test the Sobel
sobel = Sobel(ctx, queue)
sobel(in_buf, imgx_buf, imgy_buf, mag_buf)
print(imgx_buf.get())
print(mag_buf.get())
# Test the conv
#conv = NaiveSeparableCorrelation(ctx, queue)
conv = LocalMemorySeparableCorrelation(ctx, queue)
conv(in_buf, row_buf, col_buf, out_buf)
full_kernel = np.outer(col_k, row_k)
print(full_kernel)
from scipy.signal import correlate2d as c2d
gt = c2d(test_im, full_kernel, mode='same', boundary='symm')
# print "Input: "
# print(test_im)
# print "ground truth"
# print(gt)
# print "cl output"
# print(out_buf.get())
# print "diff"
# print(gt - out_buf.get())
if not np.allclose(gt, out_buf.get()):
plt.imshow(gt - out_buf.get())
plt.show()