def test_image_with_invalid_shape(self): for invalid_shape in ([1], [2, 1], [2, 4, 4, 4, 1]): image = tf.zeros(invalid_shape, tf.uint8) # pylint: disable=bad-continuation with self.assertRaisesRegex( ValueError, "`images` should have rank between 3 and 4"): _ = distance_tranform_ops.euclidean_dist_transform(image) image = tf.zeros([2, 4, 3], tf.uint8) with self.assertRaisesRegex(ValueError, "`images` must have only one channel"): _ = distance_tranform_ops.euclidean_dist_transform(image)
def test_all_zeros(): image = tf.zeros([10, 10], tf.uint8) expected_output = np.zeros([10, 10]) for output_dtype in [tf.float16, tf.float32, tf.float64]: output = dist_ops.euclidean_dist_transform(image, dtype=output_dtype) np.testing.assert_allclose(output, expected_output)
def test_batch_binary_images(self): batch_size = 3 # yapf: disable image = [[[0], [0], [0], [0], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [0], [0], [0], [0]]] expected_output = np.array([ 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 ] * batch_size) # yapf: enable images = tf.constant([image] * batch_size, dtype=tf.uint8) for output_dtype in [tf.float16, tf.float32, tf.float64]: output = distance_tranform_ops.euclidean_dist_transform( images, dtype=output_dtype) output_flat = tf.reshape(output, [-1]) with self.subTest(output_dtype=output_dtype): self.assertEqual(output.dtype, output_dtype) self.assertEqual(output.shape, [batch_size, 5, 5, 1]) self.assertAllCloseAccordingToType(output_flat, expected_output)
def test_single_binary_image(self): # yapf: disable image = [[[1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1]], [[0], [1], [0], [1], [0]], [[1], [0], [1], [0], [1]], [[0], [1], [0], [1], [0]]] # pylint: disable=bad-whitespace expected_output = np.array([ 2, 2.23606801, 2, 2.23606801, 2, 1, 1.41421354, 1, 1.41421354, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]) # yapf: enable image = tf.constant(image, dtype=tf.uint8) for output_dtype in [tf.float16, tf.float32, tf.float64]: output = distance_tranform_ops.euclidean_dist_transform( image, dtype=output_dtype) output_flat = tf.reshape(output, [-1]) with self.subTest(output_dtype=output_dtype): self.assertEqual(output.dtype, output_dtype) self.assertEqual(output.shape, [5, 5, 1]) self.assertAllCloseAccordingToType(output_flat, expected_output)
def test_all_zeros(self): image = tf.zeros([10, 10, 1], tf.uint8) expected_output = np.zeros([10, 10, 1]) for output_dtype in [tf.float16, tf.float32, tf.float64]: output = distance_tranform_ops.euclidean_dist_transform( image, dtype=output_dtype) self.assertAllClose(output, expected_output)
def test_image_with_invalid_dtype(self): image = [[[1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1]], [[0], [1], [0], [1], [0]], [[1], [0], [1], [0], [1]], [[0], [1], [0], [1], [0]]] image = tf.constant(image, dtype=tf.uint8) for output_dtype in [tf.uint8, tf.int32, tf.int64]: with self.assertRaisesRegex( TypeError, "`dtype` must be float16, float32 or float64"): _ = dist_ops.euclidean_dist_transform(image, dtype=output_dtype)
def test_image_with_invalid_dtype(dtype): image = [ [[1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1]], [[0], [1], [0], [1], [0]], [[1], [0], [1], [0], [1]], [[0], [1], [0], [1], [0]], ] image = tf.constant(image, dtype=tf.uint8) with pytest.raises(TypeError, match="`dtype` must be float16, float32 or float64"): _ = dist_ops.euclidean_dist_transform(image, dtype=dtype)
def test_multi_channels(): channels = 3 image = [ [[0], [0], [0], [0], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [0], [0], [0], [0]], ] expected_output = np.tile( np.expand_dims( np.array([ 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, ]), axis=-1, ), [1, 3], ) image = np.tile(image, [1, 1, channels]) images = tf.constant([image], dtype=tf.uint8) output = dist_ops.euclidean_dist_transform(images, dtype=tf.float32) output_flat = tf.reshape(output, [-1, 3]) assert output.shape == [1, 5, 5, channels] test_utils.assert_allclose_according_to_type(output_flat, expected_output)
def test_image_with_invalid_dtype(self): # yapf: disable image = [[[1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1]], [[0], [1], [0], [1], [0]], [[1], [0], [1], [0], [1]], [[0], [1], [0], [1], [0]]] # yapf: enable image = tf.constant(image, dtype=tf.uint8) for output_dtype in [tf.uint8, tf.int32, tf.int64]: # pylint: disable=bad-continuation with self.assertRaisesRegex( TypeError, "`dtype` must be float16, float32 or float64"): _ = distance_tranform_ops.euclidean_dist_transform( image, dtype=output_dtype)
def test_single_binary_image(dtype): image = [ [[1], [1], [1], [1], [1]], [[1], [1], [1], [1], [1]], [[0], [1], [0], [1], [0]], [[1], [0], [1], [0], [1]], [[0], [1], [0], [1], [0]], ] expected_output = np.array( [ 2, 2.23606801, 2, 2.23606801, 2, 1, 1.41421354, 1, 1.41421354, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, ] ) image = tf.constant(image, dtype=tf.uint8) output = dist_ops.euclidean_dist_transform(image, dtype=dtype) output_flat = tf.reshape(output, [-1]) assert output.dtype == dtype assert output.shape == [5, 5, 1] test_utils.assert_allclose_according_to_type(output_flat, expected_output)
def test_batch_binary_images(dtype): batch_size = 3 image = [ [[0], [0], [0], [0], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [1], [1], [1], [0]], [[0], [0], [0], [0], [0]], ] expected_output = np.array([ 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 2, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 ] * batch_size) images = tf.constant([image] * batch_size, dtype=tf.uint8) output = dist_ops.euclidean_dist_transform(images, dtype=dtype) output_flat = tf.reshape(output, [-1]) assert output.shape == [batch_size, 5, 5, 1] test_utils.assert_allclose_according_to_type(output_flat, expected_output)
def test_all_ones(): image = tf.ones([10, 10, 1], tf.uint8) output = dist_ops.euclidean_dist_transform(image) expected_output = np.full([10, 10, 1], tf.float32.max) np.testing.assert_allclose(output, expected_output)
def test_image_with_invalid_shape(): image = tf.zeros([2, 4, 3], tf.uint8) with pytest.raises(ValueError, match="`images` must have only one channel"): _ = dist_ops.euclidean_dist_transform(image)
def test_image_with_invalid_shape(self): image = tf.zeros([2, 4, 3], tf.uint8) with self.assertRaisesRegex(ValueError, "`images` must have only one channel"): _ = dist_ops.euclidean_dist_transform(image)
def test_all_ones(self): image = tf.ones([10, 10, 1], tf.uint8) output = distance_tranform_ops.euclidean_dist_transform(image) expected_output = np.full([10, 10, 1], tf.float32.max) self.assertAllClose(output, expected_output)