def _filter3d(image, filter): output = np.zeros_like(image) output[:, :, 0] = gputools.convolve(image[:, :, 0], filter) output[:, :, 1] = gputools.convolve(image[:, :, 1], filter) output[:, :, 2] = gputools.convolve(image[:, :, 2], filter) return output
def _deconv_rl_gpu_conv(data_g, h_g, Niter=10): """ using convolve """ #set up some gpu buffers u_g = OCLArray.empty(data_g.shape, np.float32) u_g.copy_buffer(data_g) tmp_g = OCLArray.empty(data_g.shape, np.float32) tmp2_g = OCLArray.empty(data_g.shape, np.float32) #fix this hflip_g = OCLArray.from_array((h_g.get()[::-1, ::-1]).copy()) for i in range(Niter): convolve(u_g, h_g, res_g=tmp_g) _divide_inplace(data_g, tmp_g) # return data_g, tmp_g convolve(tmp_g, hflip_g, res_g=tmp2_g) _multiply_inplace(u_g, tmp2_g) return u_g
def _deconv_rl_gpu_conv(data_g, h_g, Niter = 10): """ using convolve """ #set up some gpu buffers u_g = OCLArray.empty(data_g.shape,np.float32) u_g.copy_buffer(data_g) tmp_g = OCLArray.empty(data_g.shape,np.float32) tmp2_g = OCLArray.empty(data_g.shape,np.float32) #fix this hflip_g = OCLArray.from_array((h_g.get()[::-1,::-1]).copy()) for i in range(Niter): convolve(u_g, h_g, res_g = tmp_g) _divide_inplace(data_g,tmp_g) # return data_g, tmp_g convolve(tmp_g, hflip_g, res_g = tmp2_g) _multiply_inplace(u_g,tmp2_g) return u_g
def _convolve_rand(dshape,hshape): print "convolving test: dshape = %s, hshape = %s"%(dshape,hshape) np.random.seed(1) d = np.random.uniform(-1,1,dshape).astype(np.float32) h = np.random.uniform(-1,1,hshape).astype(np.float32) out1 = sp_filter.convolve(d,h,mode="constant") out2 = gputools.convolve(d,h) out3 = gputools.convolve(d,h, sub_blocks=(2,3,4)) npt.assert_allclose(out1,out2,rtol=1.e-2,atol=1.e-5) npt.assert_allclose(out1,out3,rtol=1.e-2,atol=1.e-5)
def time_gputools(self): import numpy as np image = np.random.random((400, 400)) image[:200, :200] += 1 image[300:, 300] += 0.5 h = np.ones((17, 17)) res = convolve(image, h, mode='constant')
def _convolve_rand(dshape,hshape): print "convolving test: dshape = %s, hshape = %s"%(dshape,hshape) np.random.seed(1) d = np.random.uniform(-1,1,dshape).astype(np.float32) h = np.random.uniform(-1,1,hshape).astype(np.float32) out2 = gputools.convolve(d,h)
def _convolve_rand(dshape, hshape): print "convolving test: dshape = %s, hshape = %s" % (dshape, hshape) np.random.seed(1) d = np.random.uniform(-1, 1, dshape).astype(np.float32) h = np.random.uniform(-1, 1, hshape).astype(np.float32) out2 = gputools.convolve(d, h)
def test_reflect(): image = np.ones((5, 5)) image[2, 2] = 0 h = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) out = gputools.convolve(image, h, mode='reflect') npt.assert_allclose(out[0], [ 0, ] * 5) npt.assert_allclose(out[1], [0, 1, 2, 1, 0])
def naive_gpu_benchmark(): start = time.time() import numpy as np image = np.random.random((400, 400)) image[:200, :200] += 1 image[300:, 300] += 0.5 h = np.ones((17, 17)) res = convolve(image, h, mode='constant') taken = time.time() - start print('took', taken)
def _convolve_rand(dshape, hshape): print "convolving test: dshape = %s, hshape = %s" % (dshape, hshape) np.random.seed(1) d = np.random.uniform(-1, 1, dshape).astype(np.float32) h = np.random.uniform(-1, 1, hshape).astype(np.float32) out1 = sp_filter.convolve(d, h, mode="constant") out2 = gputools.convolve(d, h) npt.assert_allclose(out1, out2, rtol=1.e-2, atol=1.e-5)
def _convolve_rand(dshape, hshape, assert_close=True, test_subblocks=True): print("convolving test: dshape = %s, hshape = %s" % (dshape, hshape)) np.random.seed(1) d = np.random.uniform(-1, 1, dshape).astype(np.float32) h = np.random.uniform(-1, 1, hshape).astype(np.float32) print("gputools") outs = [gputools.convolve(d, h)] print("scipy") out1 = sp_filter.convolve(d, h, mode="constant", cval=0.) if test_subblocks: outs.append(gputools.convolve(d, h, sub_blocks=(2, 3, 4))) if assert_close: for out in outs: npt.assert_allclose(out1, out, rtol=1.e-2, atol=1.e-3) return [out1] + outs
def __solve_cuda(boundary_conds, mask, n=100): kernel = [[[0, 0, 0], [0, 1, 0], [0, 0, 0]], [[0, 1, 0], [1, 0, 1], [0, 1, 0]], [[0, 0, 0], [0, 1, 0], [0, 0, 0]]] kernel = np.asarray(kernel) * (1 / 6) output = boundary_conds.copy() for i in range(0, n): output = gputools.convolve(output, kernel) output = output * mask output += boundary_conds return output
def detect_corners_template(gray, template, mode='same'): img_corners = [None]*4 for i in range(4): if GPUTOOLS and mode == 'same': img_corners[i] = gputools.convolve(gray, template[i]) else: img_corners[i] = signal.convolve(gray, template[i], mode=mode) img_corners_mu = np.mean(img_corners, axis=0) arr = np.array([img_corners[0]-img_corners_mu, img_corners[1]-img_corners_mu, img_corners_mu-img_corners[2], img_corners_mu-img_corners[3]]) # case 1: a=white, b=black img_corners_1 = np.min(arr, axis=0) # case 2: b=white, a=black img_corners_2 = np.min(-arr, axis=0) # combine both img_corners = np.max([img_corners_1, img_corners_2], axis=0) return img_corners
def gputools_convolve(in1, in2, mode=None, method=None): return gputools.convolve(in1, in2)
def _get_convolution_method(self, input_image, psf_kernel): if self.backend == "scipy": lprint("Using scipy backend.") from scipy.signal import convolve return convolve elif self.backend == "scipy-cupy": try: lprint("Attempting to use scipy-cupy backend.") import cupy import scipy scipy.fft.set_backend(cupy.fft) self.backend = "scipy" lprint("Succeeded to use scipy-cupy backend.") return self._get_convolution_method(input_image, psf_kernel) except Exception: track = traceback.format_exc() lprint(track) lprint("Failed to use scipy-cupy backend.") self.backend = "cupy" return self._get_convolution_method(input_image, psf_kernel) elif self.backend == "gputools": try: lprint("Attempting to use gputools backend.") # testing if gputools works: import gputools import numpy # try something simple and see if it crashes... data = numpy.ones((30, 40, 50)) h = numpy.ones((10, 11, 12)) out = gputools.convolve(data, h) # noqa: F841 def gputools_convolve(in1, in2, mode=None, method=None): return gputools.convolve(in1, in2) # gputools backend does not need extra padding: self.padding = False lprint("Succeeded to use cupy backend.") return gputools_convolve except Exception: track = traceback.format_exc() lprint(track) lprint("Failed to use gputools backend.") pass elif self.backend == "cupy": try: lprint("Attempting to use cupy backend.") # try: # testing if gputools works: # try something simple and see if it crashes... import cupy import cupyx.scipy.ndimage data = cupy.ones((30, 40, 50)) h = cupy.ones((10, 11, 12)) cupyx.scipy.ndimage.convolve(data, h) # gputools backend does not need extra padding: self.padding = False def cupy_convolve(in1, in2, mode=None, method=None): return cupyx.scipy.ndimage.convolve(in1, in2, mode="reflect") lprint("Succeeded to use cupy backend.") if psf_kernel.size > 500: return self._cupy_convolve_fft else: return cupy_convolve except Exception: track = traceback.format_exc() lprint(track) lprint("Failed to use cupy backend, trying gputools") self.backend = "gputools" return self._get_convolution_method(input_image, psf_kernel) lprint("Faling back to scipy backend.") # this is scipy's convolve: from scipy.signal import convolve return convolve
def time_small_gputools(self): import numpy as np image = np.random.random((100, 100)) h = np.ones((17, 17)) res = convolve(image, h, mode='constant')
# return data_g, tmp_g convolve(tmp_g, hflip_g, res_g = tmp2_g) _multiply_inplace(u_g,tmp2_g) return u_g if __name__ == '__main__': from scipy.misc import lena d = np.pad(lena(),((50,)*2,)*2,mode="constant") h = np.ones((11,)*2)/121. # hpad = np.pad(h,((251,250),(251,250)),mode="constant") y = convolve(d,h) y += 0.02*np.max(d)*np.random.uniform(0,1,d.shape) print "start" # u = deconv_rl(y,h, 1) out = [r.get() for r in _deconv_rl_gpu_conv(OCLArray.from_array(y.astype(np.float32)),OCLArray.from_array(h.astype(np.float32)),1)]
def conv_gpu(d,h): return gputools.convolve(d, h)
# return data_g, tmp_g convolve(tmp_g, hflip_g, res_g=tmp2_g) _multiply_inplace(u_g, tmp2_g) return u_g if __name__ == '__main__': from scipy.misc import lena d = np.pad(lena(), ((50, ) * 2, ) * 2, mode="constant") h = np.ones((11, ) * 2) / 121. # hpad = np.pad(h,((251,250),(251,250)),mode="constant") y = convolve(d, h) y += 0.02 * np.max(d) * np.random.uniform(0, 1, d.shape) print("start") # u = deconv_rl(y,h, 1) out = [ r.get() for r in _deconv_rl_gpu_conv(OCLArray.from_array(y.astype( np.float32)), OCLArray.from_array(h.astype(np.float32)), 1) ]
def _filter2d(image, filter): return gputools.convolve(image, filter)