Example #1
0
    def test_build_matrices_jacobian_random(self, size, channels):
        """Tests the Jacobian of the build_matrices function."""
        tensor_shape = np.random.randint(size, 6, size=3)
        image_init = np.random.uniform(0.0,
                                       1.0,
                                       size=tensor_shape.tolist() + [channels])

        with self.subTest(name="laplacian"):
            self.assert_jacobian_is_correct_fn(
                lambda image: matting.build_matrices(image, size=size)[0],
                [image_init])
        with self.subTest(name="pseudo_inverse"):
            self.assert_jacobian_is_correct_fn(
                lambda image: matting.build_matrices(image, size=size)[1],
                [image_init])
Example #2
0
    def test_loss_same_images(self, size):
        """Tests that passing same images results in a loss close to 0.0."""
        tensor_shape = np.random.randint(size, 6, size=3).tolist()
        image = np.random.uniform(0.0, 1.0, size=tensor_shape + [1])

        laplacian, _ = matting.build_matrices(image, size=size)
        loss = matting.loss(image, laplacian)

        self.assertAllClose(loss, 0.0, atol=1e-4)
Example #3
0
  def test_loss_positive(self, size):
    """Tests that the loss is always greater or equal to 0.0."""
    tensor_shape = np.random.randint(size, 6, size=3).tolist()
    image = tf.random.uniform(minval=0.0, maxval=1.0, shape=tensor_shape + [3])
    matte = tf.random.uniform(minval=0.0, maxval=1.0, shape=tensor_shape + [1])

    laplacian, _ = matting.build_matrices(image, size=size)
    loss = matting.loss(matte, laplacian)

    self.assertAllGreaterEqual(loss, 0.0)
Example #4
0
    def test_linear_coefficients_reconstruction_opposite_images(self, size):
        """Tests that the matte can be reconstructed by using the coefficients ."""
        tensor_shape = np.random.randint(size, 6, size=3).tolist()
        image = np.random.uniform(0.0, 1.0, size=tensor_shape + [1])

        _, pseudo_inverse = matting.build_matrices(image, size=size)
        a, b = matting.linear_coefficients(1.0 - image, pseudo_inverse)
        reconstructed = matting.reconstruct(image, a, b)

        self.assertAllClose(1.0 - image, reconstructed, atol=1e-4)
Example #5
0
  def test_build_matrices_laplacian_versions(self, size, channels):
    """Compares two ways of computing the laplacian matrix."""
    tensor_shape = np.random.randint(size, 6, size=3)
    image_init = np.random.uniform(
        0.0, 1.0, size=tensor_shape.tolist() + [channels])
    image = tf.convert_to_tensor(value=image_init)

    laplacian_v1, _ = matting.build_matrices(image, size=size)
    laplacian_v2 = _laplacian_matrix(image, size=size)

    self.assertAllClose(laplacian_v1, laplacian_v2)
Example #6
0
  def test_build_matrices_jacobian_random(self, size, channels):
    """Tests the Jacobian of the build_matrices function."""
    tensor_shape = np.random.randint(size, 6, size=3)
    image_init = np.random.uniform(
        0.0, 1.0, size=tensor_shape.tolist() + [channels])
    image = tf.convert_to_tensor(value=image_init)

    laplacian, pseudo_inverse = matting.build_matrices(image, size=size)

    with self.subTest(name="laplacian"):
      self.assert_jacobian_is_correct(image, image_init, laplacian)
    with self.subTest(name="pseudo_inverse"):
      self.assert_jacobian_is_correct(image, image_init, pseudo_inverse)
Example #7
0
  def test_build_matrices_laplacian_zero_rows_and_columns(self, size, channels):
    """Tests that the laplacian matrix rows and columns sum to zero."""
    tensor_shape = np.random.randint(size, 6, size=3)
    image_init = np.random.uniform(
        0.0, 1.0, size=tensor_shape.tolist() + [channels])
    image = tf.convert_to_tensor(value=image_init)

    laplacian, _ = matting.build_matrices(image, size=size)
    rows = tf.reduce_sum(input_tensor=laplacian, axis=-2)
    columns = tf.reduce_sum(input_tensor=laplacian, axis=-1)

    with self.subTest(name="rows"):
      self.assertAllClose(rows, tf.zeros_like(rows))
    with self.subTest(name="columns"):
      self.assertAllClose(columns, tf.zeros_like(columns))
Example #8
0
 def test_build_matrices_raised(self, error_msg, size, *shapes):
     """Tests that the shape exceptions are properly raised."""
     build_matrices = lambda image: matting.build_matrices(image, size=size)
     self.assert_exception_is_raised(build_matrices, error_msg, shapes)
Example #9
0
 def test_build_matrices_not_raised(self, size, *shapes):
     """Tests that the shape exceptions are not raised."""
     build_matrices = lambda image: matting.build_matrices(image, size=size)
     self.assert_exception_is_not_raised(build_matrices, shapes)