def test_norm1_halide(self): """Halide Norm 1 test """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open( testimg_filename ) # opens the file using Pillow - it's not an array yet np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem v = np_img theta = 0.5 # Output output = np.zeros_like(np_img) Halide('prox_L1.cpp').prox_L1(v, theta, output) # Call # Reference output_ref = np.maximum(0.0, v - theta) - np.maximum( 0.0, -v - theta) self.assertItemsAlmostEqual(output, output_ref)
def test_mask_halide(self): """Test mask lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') # opens the file using Pillow - it's not an array yet img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Test problem output = np.zeros_like(np_img) mask = np.asfortranarray( np.random.randn(*list(np_img.shape)).astype(np.float32)) mask = np.maximum(mask, 0.) Halide('A_mask.cpp').A_mask(np_img, mask, output) # Call output_ref = mask * np_img # Transpose output_trans = np.zeros_like(np_img) Halide('At_mask.cpp').At_mask(np_img, mask, output_trans) # Call self.assertItemsAlmostEqual(output, output_ref) self.assertItemsAlmostEqual(output_trans, output_ref)
def test_poisson_halide(self): """Halide Poisson norm test """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem v = np_img theta = 0.5 mask = np.asfortranarray(np.random.randn(*list(np_img.shape)).astype(np.float32)) mask = np.maximum(mask, 0.) b = np_img * np_img # Output output = np.zeros_like(v) tic() Halide('prox_Poisson.cpp').prox_Poisson(v, mask, b, theta, output) # Call print('Running took: {0:.1f}ms'.format(toc())) # Reference output_ref = 0.5 * (v - theta + np.sqrt((v - theta) * (v - theta) + 4 * theta * b)) output_ref[mask <= 0.5] = v[mask <= 0.5] self.assertItemsAlmostEqual(output, output_ref)
def test_slicing(self): """Test slicing over numpy arrays in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') np_img = im2nparray(Image.open(testimg_filename)) np_img = np.asfortranarray( np.tile(np_img[..., np.newaxis], (1, 1, 1, 3))) # Test problem output = np.zeros_like(np_img) mask = np.asfortranarray( np.random.randn(*list(np_img.shape[0:3])).astype(np.float32)) mask = np.maximum(mask, 0.) for k in range(np_img.shape[3]): Halide('A_mask.cpp').A_mask( np.asfortranarray(np_img[:, :, :, k]), mask, output[:, :, :, k]) # Call output_ref = np.zeros_like(np_img) for k in range(np_img.shape[3]): output_ref[:, :, :, k] = mask * np_img[:, :, :, k] self.assertItemsAlmostEqual(output, output_ref)
def test_norm1_halide(self): """Halide Norm 1 test """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) # opens the file using Pillow - it's not an array yet np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem v = np_img theta = 0.5 # Output output = np.zeros_like(np_img) Halide('prox_L1.cpp').prox_L1(v, theta, output) # Call # Reference output_ref = np.maximum(0.0, v - theta) - np.maximum(0.0, -v - theta) self.assertItemsAlmostEqual(output, output_ref)
def test_mask_halide(self): """Test mask lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') # opens the file using Pillow - it's not an array yet img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Test problem output = np.zeros_like(np_img) mask = np.asfortranarray(np.random.randn(*list(np_img.shape)).astype(np.float32)) mask = np.maximum(mask, 0.) Halide('A_mask.cpp').A_mask(np_img, mask, output) # Call output_ref = mask * np_img # Transpose output_trans = np.zeros_like(np_img) Halide('At_mask.cpp').At_mask(np_img, mask, output_trans) # Call self.assertItemsAlmostEqual(output, output_ref) self.assertItemsAlmostEqual(output_trans, output_ref)
def test_conv_halide(self): """Test convolution lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') # opens the file using Pillow - it's not an array yet img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem output = np.zeros_like(np_img) K = np.ones((11, 11), dtype=np.float32, order='FORTRAN') K /= np.prod(K.shape) #Convolve in halide Halide('A_conv.cpp').A_conv(np_img, K, output) #Convolve in scipy output_ref = signal.convolve2d(np_img, K, mode='same', boundary='wrap') # Transpose output_corr = np.zeros_like(np_img) Halide('At_conv.cpp').At_conv(np_img, K, output_corr) # Call output_corr_ref = signal.convolve2d(np_img, np.flipud(np.fliplr(K)), mode='same', boundary='wrap') self.assertItemsAlmostEqual(output, output_ref) self.assertItemsAlmostEqual(output_corr, output_corr_ref)
def test_warp_halide(self): """Test warp lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Generate problem theta_rad = 5.0 * np.pi / 180.0 H = np.array([[np.cos(theta_rad), -np.sin(theta_rad), 0.0001], [np.sin(theta_rad), np.cos(theta_rad), 0.0003], [0., 0., 1.]], dtype=np.float32, order='F') # Reference output_ref = cv2.warpPerspective(np_img, H.T, np_img.shape[1::-1], flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0.) # Halide output = np.zeros_like(np_img) Hc = np.asfortranarray( np.linalg.pinv(H)[..., np.newaxis]) # Third axis for halide Halide('A_warp.cpp').A_warp(np_img, Hc, output) # Call # Transpose output_trans = np.zeros_like(np_img) Hinvc = np.asfortranarray(H[..., np.newaxis]) # Third axis for halide Halide('At_warp.cpp').At_warp(output, Hinvc, output_trans) # Call # Compute reference output_ref_trans = cv2.warpPerspective( output_ref, H.T, np_img.shape[1::-1], flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=0.) # Opencv does inverse warp self.assertItemsAlmostEqual(output, output_ref, places=1) # Opencv does inverse warp self.assertItemsAlmostEqual(output_trans, output_ref_trans, places=1)
def test_grad_halide(self): """Test gradient lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem output = np.zeros( (np_img.shape[0], np_img.shape[1], np_img.shape[2] if (len(np_img.shape) > 2) else 1, 2), dtype=np.float32, order='FORTRAN') # Gradient in halide Halide('A_grad.cpp').A_grad(np_img, output) # Call # Compute comparison f = np_img if len(np_img.shape) == 2: f = f[..., np.newaxis] ss = f.shape fx = f[:, np.r_[1:ss[1], ss[1] - 1], :] - f fy = f[np.r_[1:ss[0], ss[0] - 1], :, :] - f Kf = np.asfortranarray(np.stack((fy, fx), axis=-1)) # Transpose output_trans = np.zeros(f.shape, dtype=np.float32, order='F') Halide('At_grad.cpp').At_grad(Kf, output_trans) # Call # Compute comparison (Negative divergence) Kfy = Kf[:, :, :, 0] fy = Kfy - Kfy[np.r_[0, 0:ss[0] - 1], :, :] fy[0, :, :] = Kfy[0, :, :] fy[-1, :, :] = -Kfy[-2, :, :] Kfx = Kf[:, :, :, 1] ss = Kfx.shape fx = Kfx - Kfx[:, np.r_[0, 0:ss[1] - 1], :] fx[:, 0, :] = Kfx[:, 0, :] fx[:, -1, :] = -Kfx[:, -2, :]
def test_grad_halide(self): """Test gradient lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem output = np.zeros((np_img.shape[0], np_img.shape[1], np_img.shape[2] if (len(np_img.shape) > 2) else 1, 2), dtype=np.float32, order='FORTRAN') #Gradient in halide Halide('A_grad.cpp').A_grad(np_img, output) # Call # Compute comparison f = np_img if len(np_img.shape) == 2: f = f[..., np.newaxis] ss = f.shape fx = f[:, np.r_[1:ss[1], ss[1] - 1], :] - f fy = f[np.r_[1:ss[0], ss[0] - 1], :, :] - f Kf = np.asfortranarray(np.stack((fy, fx), axis=-1)) # Transpose output_trans = np.zeros(f.shape, dtype=np.float32, order='F') Halide('At_grad.cpp').At_grad(Kf, output_trans) # Call # Compute comparison (Negative divergence) Kfy = Kf[:, :, :, 0] fy = Kfy - Kfy[np.r_[0, 0:ss[0] - 1], :, :] fy[0, :, :] = Kfy[0, :, :] fy[-1, :, :] = -Kfy[-2, :, :] Kfx = Kf[:, :, :, 1] ss = Kfx.shape fx = Kfx - Kfx[:, np.r_[0, 0:ss[1] - 1], :] fx[:, 0, :] = Kfx[:, 0, :] fx[:, -1, :] = -Kfx[:, -2, :]
def test_warp_halide(self): """Test warp lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Generate problem theta_rad = 5.0 * np.pi / 180.0 H = np.array([[np.cos(theta_rad), -np.sin(theta_rad), 0.0001], [np.sin(theta_rad), np.cos(theta_rad), 0.0003], [0., 0., 1.]], dtype=np.float32, order='F') # Reference output_ref = cv2.warpPerspective(np_img, H.T, np_img.shape[1::-1], flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0.) # Halide output = np.zeros_like(np_img) Hc = np.asfortranarray(np.linalg.pinv(H)[..., np.newaxis]) # Third axis for halide Halide('A_warp.cpp').A_warp(np_img, Hc, output) # Call # Transpose output_trans = np.zeros_like(np_img) Hinvc = np.asfortranarray(H[..., np.newaxis]) # Third axis for halide Halide('At_warp.cpp').At_warp(output, Hinvc, output_trans) # Call # Compute reference output_ref_trans = cv2.warpPerspective(output_ref, H.T, np_img.shape[1::-1], flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=0.) # Opencv does inverse warp self.assertItemsAlmostEqual(output, output_ref, places=1) # Opencv does inverse warp self.assertItemsAlmostEqual(output_trans, output_ref_trans, places=1)
def test_conv_halide(self): """Test convolution lin op in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') # opens the file using Pillow - it's not an array yet img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem output = np.zeros_like(np_img) K = np.ones((11, 11), dtype=np.float32, order='FORTRAN') K /= np.prod(K.shape) # Convolve in halide Halide('A_conv.cpp').A_conv(np_img, K, output) # Convolve in scipy output_ref = signal.convolve2d(np_img, K, mode='same', boundary='wrap') # Transpose output_corr = np.zeros_like(np_img) Halide('At_conv.cpp').At_conv(np_img, K, output_corr) # Call output_corr_ref = signal.convolve2d(np_img, np.flipud(np.fliplr(K)), mode='same', boundary='wrap') self.assertItemsAlmostEqual(output, output_ref) self.assertItemsAlmostEqual(output_corr, output_corr_ref)
def test_isonorm1_halide(self): """Halide Norm 1 test """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem theta = 0.5 f = np_img if len(np_img.shape) == 2: f = f[..., np.newaxis] ss = f.shape fx = f[:, np.r_[1:ss[1], ss[1] - 1], :] - f fy = f[np.r_[1:ss[0], ss[0] - 1], :, :] - f v = np.asfortranarray(np.stack((fx, fy), axis=-1)) # Output output = np.zeros_like(v) Halide('prox_IsoL1.cpp').prox_IsoL1(v, theta, output) # Call # Reference normv = np.sqrt( np.multiply(v[:, :, :, 0], v[:, :, :, 0]) + np.multiply(v[:, :, :, 1], v[:, :, :, 1])) normv = np.stack((normv, normv), axis=-1) with np.errstate(divide='ignore'): output_ref = np.maximum(0.0, 1.0 - theta / normv) * v self.assertItemsAlmostEqual(output, output_ref)
def test_poisson_halide(self): """Halide Poisson norm test """ if halide_installed(): # Load image testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem v = np_img theta = 0.5 mask = np.asfortranarray( np.random.randn(*list(np_img.shape)).astype(np.float32)) mask = np.maximum(mask, 0.) b = np_img * np_img # Output output = np.zeros_like(v) tic() Halide('prox_Poisson.cpp').prox_Poisson(v, mask, b, theta, output) # Call print('Running took: {0:.1f}ms'.format(toc())) # Reference output_ref = 0.5 * (v - theta + np.sqrt((v - theta) * (v - theta) + 4 * theta * b)) output_ref[mask <= 0.5] = v[mask <= 0.5] self.assertItemsAlmostEqual(output, output_ref)
def test_isonorm1_halide(self): """Halide Norm 1 test """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') img = Image.open(testimg_filename) np_img = np.asfortranarray(im2nparray(img)) # Convert to gray np_img = np.mean(np_img, axis=2) # Test problem theta = 0.5 f = np_img if len(np_img.shape) == 2: f = f[..., np.newaxis] ss = f.shape fx = f[:, np.r_[1:ss[1], ss[1] - 1], :] - f fy = f[np.r_[1:ss[0], ss[0] - 1], :, :] - f v = np.asfortranarray(np.stack((fx, fy), axis=-1)) # Output output = np.zeros_like(v) Halide('prox_IsoL1.cpp').prox_IsoL1(v, theta, output) # Call # Reference normv = np.sqrt(np.multiply(v[:, :, :, 0], v[:, :, :, 0]) + np.multiply(v[:, :, :, 1], v[:, :, :, 1])) normv = np.stack((normv, normv), axis=-1) with np.errstate(divide='ignore'): output_ref = np.maximum(0.0, 1.0 - theta / normv) * v self.assertItemsAlmostEqual(output, output_ref)
def test_slicing(self): """Test slicing over numpy arrays in halide. """ if halide_installed(): # Load image testimg_filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg') np_img = im2nparray(Image.open(testimg_filename)) np_img = np.asfortranarray(np.tile(np_img[..., np.newaxis], (1, 1, 1, 3))) # Test problem output = np.zeros_like(np_img) mask = np.asfortranarray(np.random.randn(*list(np_img.shape[0:3])).astype(np.float32)) mask = np.maximum(mask, 0.) for k in range(np_img.shape[3]): Halide('A_mask.cpp').A_mask(np.asfortranarray(np_img[:, :, :, k]), mask, output[:, :, :, k]) # Call output_ref = np.zeros_like(np_img) for k in range(np_img.shape[3]): output_ref[:, :, :, k] = mask * np_img[:, :, :, k] self.assertItemsAlmostEqual(output, output_ref)
def test_compile(self): """Test compilation and run. """ if halide_installed(): # Force recompilation of conv Halide('A_conv.cpp', recompile=True, verbose=False)