Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run():
import pyopencl as cl
import pyopencl.array as cla
q = cl.CommandQueue(cx, properties=cl.command_queue_properties.PROFILING_ENABLE)
img = np.zeros((H, W), np.uint8)
import moonshine
data = moonshine.open('samples/sonata.png')[0].im
img[:data.shape[0], :data.shape[1]] = data != 0
imgbits = np.packbits(img)
dimg = cla.to_device(q, imgbits)
temp = cl.LocalMemory(8*H*localW)
rl = cla.zeros(q, (H, W), np.uint8)
prg = cl.Program(cx, open("runlength.cl").read()).build()
return prg.runlength(q, (H, W/8), (localH, localW), dimg.data, temp, rl.data), rl
def run():
import pyopencl as cl
import pyopencl.array as cla
q = cl.CommandQueue(cx, properties=cl.command_queue_properties.PROFILING_ENABLE)
img = np.zeros((H, W), np.uint8)
#img[np.arange(500, 520), np.arange(200, 240, 2)] = 1
import moonshine
data = moonshine.open('samples/sonata.png')[0].im
img[:data.shape[0], :data.shape[1]] = data != 0
imgbits = np.packbits(img)
dimg = cla.to_device(q, imgbits)
temp = cl.LocalMemory(4*numrho)
bins = cla.zeros(q, (len(theta), numrho), np.int32)
prg = cl.Program(cx, open("hough_line.cl").read()).build()
prg.hough_line.set_scalar_arg_dtypes([None, None, np.int32, np.int32, None, None])
return prg.hough_line(q, (W/8, H, len(theta)), (8, 1, 1), dimg.data, dtheta.data, np.int32(rhores), np.int32(numrho), temp, bins.data), bins
def test_fancy_fill(ctx_factory):
if _PYPY:
pytest.xfail("numpypy: multi value setting is not supported")
context = ctx_factory()
queue = cl.CommandQueue(context)
numpy_dest = np.zeros((4,), np.int32)
numpy_idx = np.arange(3, dtype=np.int32)
numpy_src = np.arange(8, 9, dtype=np.int32)
numpy_dest[numpy_idx] = numpy_src
cl_dest = cl_array.zeros(queue, (4,), np.int32)
cl_idx = cl_array.arange(queue, 3, dtype=np.int32)
cl_src = cl_array.arange(queue, 8, 9, dtype=np.int32)
cl_dest[cl_idx] = cl_src
assert np.all(numpy_dest == cl_dest.get())
if shape[0] != shape[1]:
raise ValueError, 'Square linear system matrix expected'
if len(b) != shape[0]:
raise ValueError, 'Matrix and independet term dimensions must match'
n = len(b)
# Set x0 if not provided
if x0 == None:
x0 = np.zeros((n), dtype=np.float32)
if len(x0) != n:
raise ValueError, 'Initial solution estimator length does not match with linear system dimensions'
# Create OpenCL objects if not already generated
if not self.A:
mf = cl.mem_flags
self.A = cl.Buffer( self.context, mf.READ_WRITE, size = n*n * np.dtype('float32').itemsize )
self.b = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.x = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.r = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.u = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.v = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.w = cl_array.zeros(self.context,self.queue, (n), np.float32)
# Transfer data to buffers
events = []
events.append(cl.enqueue_write_buffer(self.queue, self.A, A.reshape((n*n)) ))
self.b.set(b)
self.x.set(x0)
self.u.set(b)
for e in events:
e.wait()
raise ValueError, 'Matrix A must be 2 dimensional array'
if shape[0] != shape[1]:
raise ValueError, 'Square linear system matrix expected'
if len(b) != shape[0]:
raise ValueError, 'Matrix and independet term dimensions must match'
n = len(b)
# Set x0 if not provided
if x0 == None:
x0 = np.zeros((n), dtype=np.float32)
if len(x0) != n:
raise ValueError, 'Initial solution estimator length does not match with linear system dimensions'
# Create OpenCL objects if not already generated
if not self.A:
mf = cl.mem_flags
self.A = cl.Buffer( self.context, mf.READ_WRITE, size = n*n * np.dtype('float32').itemsize )
self.b = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.x = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.r = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.u = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.v = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.w = cl_array.zeros(self.context,self.queue, (n), np.float32)
# Transfer data to buffers
events = []
events.append(cl.enqueue_write_buffer(self.queue, self.A, A.reshape((n*n)) ))
self.b.set(b)
self.x.set(x0)
self.u.set(b)
for e in events:
e.wait()
raise ValueError, 'Matrix and independet term dimensions must match'
n = len(b)
# Set x0 if not provided
if x0 == None:
x0 = np.zeros((n), dtype=np.float32)
if len(x0) != n:
raise ValueError, 'Initial solution estimator length does not match with linear system dimensions'
# Create OpenCL objects if not already generated
if not self.A:
mf = cl.mem_flags
self.A = cl.Buffer( self.context, mf.READ_WRITE, size = n*n * np.dtype('float32').itemsize )
self.b = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.x = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.r = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.u = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.v = cl_array.zeros(self.context,self.queue, (n), np.float32)
self.w = cl_array.zeros(self.context,self.queue, (n), np.float32)
# Transfer data to buffers
events = []
events.append(cl.enqueue_write_buffer(self.queue, self.A, A.reshape((n*n)) ))
self.b.set(b)
self.x.set(x0)
self.u.set(b)
for e in events:
e.wait()
def process(self, img, strong_threshold=5.0, weak_threshold=2.5):
h, w = img.shape[:2]
if len(img.shape) == 2:
img = np.float32(img)
else:
img = colors.rgb_to_grayscale(img)
img = np.pad(img, ((2, 2), (2, 2)), mode='edge')
self._compile(w, h)
queue = cl.CommandQueue(self._context)
img_cl = cl.array.to_device(queue, img)
gaussed_img_cl = cl.array.zeros(queue, (h + 2, w + 2), np.float32)
self._program.convolve_gaussian(queue, (w, h), None,
img_cl.data, gaussed_img_cl.data)
intensity_dxy_cl = cl.array.zeros(queue, (h, w), cl.array.vec.float2)
self._program.convolve_sobel(queue, (w, h), None,
gaussed_img_cl.data, intensity_dxy_cl.data)
is_extremum_cl = cl.array.zeros(queue, (h, w), np.int32)
self._program.non_maximum_suppression(queue, (w, h), None,
gaussed_img_cl.data, intensity_dxy_cl.data, is_extremum_cl.data)
intensity_dxy = intensity_dxy_cl.get()
intensity_dxy = np.dstack([intensity_dxy['x'], intensity_dxy['y']])
def output_zeros(self):
from pytools.obj_array import make_obj_array
return make_obj_array([
cl.array.zeros(
self.queue,
self.tree.ntargets,
dtype=self.dtype)
for k in self.code.out_kernels])
def my_realloc_zeros_nocopy(ary):
result = cl.array.zeros(queue, allocator=allocator,
shape=nboxes_guess, dtype=ary.dtype)
return result, result.events[0]
}
"""
threshold_kernel = ElementwiseKernel(mgr.state.context,
" %(tp_in)s *in, %(tp_out1)s *outv, %(tp_out2)s *outl, %(tp_th)s threshold, %(tp_n)s *bn" % {
"tp_in": dtype_to_ctype(numpy.complex64),
"tp_out1": dtype_to_ctype(numpy.complex64),
"tp_out2": dtype_to_ctype(numpy.uint32),
"tp_th": dtype_to_ctype(numpy.float32),
"tp_n": dtype_to_ctype(numpy.uint32),
},
threshold_op,
"getstuff")
n = pzeros(mgr.state.queue, 1, numpy.uint32)
val = pzeros(mgr.state.queue, 4096*256, numpy.complex64)
loc = pzeros(mgr.state.queue, 4096*256, numpy.uint32)
def threshold(series, value):
threshold_kernel(series.data, val, loc, value, n)
n0 = n.get()[0]
return loc[0:n0].get(), val[0:n0].get()