def run(self, data: np.ndarray): if data.shape != self.shape: raise ValueError("data and h have to be same shape") # set up some gpu buffers data64 = data.astype(np.complex64) y_g = OCLArray.from_array(data64) u_g = OCLArray.from_array(data64) # hflipped_g = OCLArray.from_array(h.astype(np.complex64)) for i in range(self.n_iter): # logger.info("Iteration: {}".format(i)) fft_convolve(u_g, self.psf_g, plan=self.plan, res_g=self.tmp_g, kernel_is_fft=True) _complex_divide_inplace(y_g, self.tmp_g) fft_convolve(self.tmp_g, self.psfflip_f_g, plan=self.plan, inplace=True, kernel_is_fft=True) _complex_multiply_inplace(u_g, self.tmp_g) # can abs be calculated on the gpu ? return np.abs(u_g.get())
def _deconv_rl_np_fft(data, h, Niter = 10, h_is_fftshifted = False): """ deconvolves data with given psf (kernel) h data and h have to be same shape via lucy richardson deconvolution """ if data.shape != h.shape: raise ValueError("data and h have to be same shape") if not h_is_fftshifted: h = np.fft.fftshift(h) hflip = h[::-1,::-1] #set up some gpu buffers y_g = OCLArray.from_array(data.astype(np.complex64)) u_g = OCLArray.from_array(data.astype(np.complex64)) tmp_g = OCLArray.empty(data.shape,np.complex64) hf_g = OCLArray.from_array(h.astype(np.complex64)) hflip_f_g = OCLArray.from_array(hflip.astype(np.complex64)) # hflipped_g = OCLArray.from_array(h.astype(np.complex64)) plan = fft_plan(data.shape) #transform psf fft(hf_g,inplace = True) fft(hflip_f_g,inplace = True) for i in range(Niter): print i fft_convolve(u_g, hf_g, res_g = tmp_g, kernel_is_fft = True) _complex_divide_inplace(y_g,tmp_g) fft_convolve(tmp_g,hflip_f_g, inplace = True, kernel_is_fft = True) _complex_multiply_inplace(u_g,tmp_g) return np.abs(u_g.get())
def _deconv_rl_np_fft(data, h, Niter=10, h_is_fftshifted=False): """ deconvolves data with given psf (kernel) h data and h have to be same shape via lucy richardson deconvolution """ if data.shape != h.shape: raise ValueError("data and h have to be same shape") if not h_is_fftshifted: h = np.fft.fftshift(h) hflip = h[::-1, ::-1] #set up some gpu buffers y_g = OCLArray.from_array(data.astype(np.complex64)) u_g = OCLArray.from_array(data.astype(np.complex64)) tmp_g = OCLArray.empty(data.shape, np.complex64) hf_g = OCLArray.from_array(h.astype(np.complex64)) hflip_f_g = OCLArray.from_array(hflip.astype(np.complex64)) # hflipped_g = OCLArray.from_array(h.astype(np.complex64)) plan = fft_plan(data.shape) #transform psf fft(hf_g, inplace=True) fft(hflip_f_g, inplace=True) for i in range(Niter): logger.info("Iteration: {}".format(i)) fft_convolve(u_g, hf_g, res_g=tmp_g, kernel_is_fft=True) _complex_divide_inplace(y_g, tmp_g) fft_convolve(tmp_g, hflip_f_g, inplace=True, kernel_is_fft=True) _complex_multiply_inplace(u_g, tmp_g) return np.abs(u_g.get())
def _deconv_rl_gpu_fft(data_g, h_g, Niter = 10): """ using fft_convolve """ if data_g.shape != h_g.shape: raise ValueError("data and h have to be same shape") #set up some gpu buffers u_g = OCLArray.empty(data_g.shape,np.complex64) u_g.copy_buffer(data_g) tmp_g = OCLArray.empty(data_g.shape,np.complex64) #fix this hflip_g = OCLArray.from_array((h_g.get()[::-1,::-1]).copy()) plan = fft_plan(data_g.shape) #transform psf fft(h_g,inplace = True) fft(hflip_g,inplace = True) for i in range(Niter): print i fft_convolve(u_g, h_g, res_g = tmp_g, kernel_is_fft = True) _complex_divide_inplace(data_g,tmp_g) fft_convolve(tmp_g,hflip_g, inplace = True, kernel_is_fft = True) _complex_multiply_inplace(u_g,tmp_g) return u_g
def _deconv_rl_gpu_fft(data_g, h_g, Niter=10): """ using fft_convolve """ if data_g.shape != h_g.shape: raise ValueError("data and h have to be same shape") #set up some gpu buffers u_g = OCLArray.empty(data_g.shape, np.complex64) u_g.copy_buffer(data_g) tmp_g = OCLArray.empty(data_g.shape, np.complex64) #fix this hflip_g = OCLArray.from_array((h_g.get()[::-1, ::-1]).copy()) plan = fft_plan(data_g.shape) #transform psf fft(h_g, inplace=True) fft(hflip_g, inplace=True) for i in range(Niter): logger.info("Iteration: {}".format(i)) fft_convolve(u_g, h_g, res_g=tmp_g, kernel_is_fft=True) _complex_divide_inplace(data_g, tmp_g) fft_convolve(tmp_g, hflip_g, inplace=True, kernel_is_fft=True) _complex_multiply_inplace(u_g, tmp_g) return u_g
def test_convolve2(): pass if __name__ == '__main__': test_convolve2() from scipy.misc import lena # N = 256 # d = np.random.uniform(-1,1,(N,)*2) # h = np.random.uniform(-1,1,(N,)*2) d = lena()[100:164, 100:164] x = np.linspace(-1, 1, 65)[:-1] Y, X = np.meshgrid(x, x, indexing="ij") h = np.exp(-1000 * (Y**2 + X**2)) h *= 1. / np.sum(h) t = time() res1 = convolve2d(d, h, mode="same") print time() - t t = time() res2 = gputools.fft_convolve(d, h) print time() - t
def test_fftconv_np(): d = np.ones((128,)*2, np.float32) res = fft_convolve(d,d)
def test_convolve2(): pass if __name__ == '__main__': test_convolve2() from scipy.misc import lena # N = 256 # d = np.random.uniform(-1,1,(N,)*2) # h = np.random.uniform(-1,1,(N,)*2) d = lena()[100:164,100:164] x = np.linspace(-1,1,65)[:-1] Y,X = np.meshgrid(x,x,indexing="ij") h = np.exp(-1000*(Y**2+X**2)) h *= 1./np.sum(h) t = time() res1 = convolve2d(d,h,mode="same") print time()-t t = time() res2 = gputools.fft_convolve(d,h) print time()-t
kernel_is_fft=True) _complex_divide_inplace(data_g, tmp_g) fft_convolve(tmp_g, hflip_g, inplace=True, kernel_is_fft=True) _complex_multiply_inplace(u_g, tmp_g) return u_g if __name__=='__main__': from scipy.misc import face from gputools import pad_to_shape d = np.pad(face()[200:642, 400:842, 0].astype(np.float32),((35,)*2,)*2, mode = "constant") h = np.ones((31,)*2) h *= 1./np.sum(h) h = pad_to_shape(h,d.shape) y = fft_convolve(d, h) y += 0.1*np.max(d)*np.random.uniform(0, 1, d.shape) print "start" u = deconv_rl(y, h, 20, mode_conv="fft")