示例#1
0
    def test_conv_halide(self):
        """Test convolution lin op in halide.
        """
        np_img = get_test_image(512)

        output = np.zeros_like(np_img)

        K = get_kernel(11, np_img.ndim)

        #Convolve in halide
        Halide('A_conv', recompile=True).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', recompile=True).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)
示例#2
0
    def test_poisson_halide(self):
        """Halide Poisson norm test
        """
        # Test problem
        np_img = get_test_image(512)
        v = np_img
        theta = 0.5

        mask = np.asfortranarray(
            np.random.randn(*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',
               recompile=True).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)
示例#3
0
    def test_isonorm1_halide(self):
        """Halide Norm 1 test
        """
        # Test problem
        theta = 0.5

        np_img = get_test_image(512)
        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', recompile=True).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)
示例#4
0
    def test_warp_halide(self):
        """Test warp lin op in halide.
        """
        # Load image
        WIDTH = 512
        np_img = get_test_image(WIDTH)

        # Generate problem
        theta_rad = 5.0 * np.pi / 180.0
        H = np.array(
            [[np.cos(theta_rad), -np.sin(theta_rad), WIDTH * 0.5],
             [np.sin(theta_rad), np.cos(theta_rad), 0.], [0., 0., 1.]],
            dtype=np.float32,
            order='F')

        # Reference
        output_ref = cv2.warpPerspective(np_img,
                                         H,
                                         np_img.shape[1::-1],
                                         flags=cv2.INTER_LINEAR
                                         | cv2.WARP_INVERSE_MAP,
                                         borderMode=cv2.BORDER_CONSTANT,
                                         borderValue=0.)

        # Halide
        output = np.zeros_like(np_img)
        Halide('A_warp', recompile=True).A_warp(np_img, H, output)  # Call

        self.assertItemsAlmostEqual(output, output_ref, eps=1e-1)

        # Transpose
        output_trans = np.zeros_like(np_img)
        Hinvc = np.asfortranarray(np.linalg.pinv(H))
        Halide('At_warp', recompile=True).At_warp(output, Hinvc,
                                                  output_trans)  # Call

        # Compute reference
        output_ref_trans = cv2.warpPerspective(output,
                                               H,
                                               np_img.shape[1::-1],
                                               flags=cv2.INTER_LINEAR,
                                               borderMode=cv2.BORDER_CONSTANT,
                                               borderValue=0.)

        self.assertItemsAlmostEqual(output_trans, output_ref_trans, eps=1e-1)
示例#5
0
    def test_norm1_halide(self):
        """Halide Norm 1 test
        """
        # Load image
        np_img = get_test_image(512)

        # Test problem
        v = np_img
        theta = 0.5

        # Output
        output = np.zeros_like(np_img)
        Halide('prox_L1', recompile=True).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)
示例#6
0
    def test_mask_halide(self):
        """Test mask lin op in halide.
        """
        np_img = get_test_image(512)

        # Test problem
        output = np.zeros_like(np_img)
        mask = np.asfortranarray(
            np.random.randn(*np_img.shape).astype(np.float32))
        mask = np.maximum(mask, 0.)

        Halide('A_mask', recompile=True).A_mask(np_img, mask, output)  # Call
        output_ref = mask * np_img

        # Transpose
        output_trans = np.zeros_like(np_img)
        Halide('At_mask', recompile=True).At_mask(np_img, mask,
                                                  output_trans)  # Call

        self.assertItemsAlmostEqual(output, output_ref)
        self.assertItemsAlmostEqual(output_trans, output_ref)
示例#7
0
    def test_grad_halide(self):
        """Test gradient lin op in halide.
        """
        # Load image
        np_img = get_test_image(512)

        # Test problem
        output = np.zeros((np_img.shape[0], np_img.shape[1], 1),
                          dtype=np.float32,
                          order='F')

        #Gradient in halide
        Halide('A_grad', recompile=True).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', recompile=True).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, :]