def testZeros(self): self.assertAllEqual( connected_components( self.evaluate(tf.zeros((100, 20, 50), tf.bool))), np.zeros((100, 20, 50)), )
def testOnes(self): self.assertAllEqual( self.evaluate(connected_components(tf.ones((100, 20, 50), tf.bool))), np.tile(np.arange(100)[:, None, None] + 1, [1, 20, 50]), )
def test_random_scipy(): np.random.seed(42) images = np.random.randint(0, 2, size=(10, 100, 200)).astype(np.bool) expected = connected_components_reference_implementation(images) if expected is None: return np.testing.assert_equal(connected_components(images).numpy(), expected)
def testRandom_scipy(self): np.random.seed(42) images = np.random.randint(0, 2, size=(10, 100, 200)).astype(np.bool) expected = connected_components_reference_implementation(images) if expected is None: return self.assertAllEqual(self.evaluate(connected_components(images)), expected)
def testDisconnected(self): arr = tf.cast( [[1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0]], tf.bool) # pylint: disable expected = ([[1, 0, 0, 2, 0, 0, 0, 0, 3], [0, 4, 0, 0, 0, 5, 0, 6, 0], [7, 0, 8, 0, 0, 0, 9, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0], [0, 0, 11, 0, 0, 0, 0, 0, 0]]) # pylint: disable self.assertAllEqual(self.evaluate(connected_components(arr)), expected)
def testMultipleImages(self): images = [[[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]], [[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]], [[1, 1, 0, 1], [0, 1, 1, 0], [1, 0, 1, 0], [0, 0, 1, 1]]] # pylint: disable expected = [[[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]], [[2, 0, 0, 3], [0, 0, 0, 0], [0, 0, 0, 0], [4, 0, 0, 5]], [[6, 6, 0, 7], [0, 6, 6, 0], [8, 0, 6, 0], [0, 0, 6, 6]]] # pylint: disable self.assertAllEqual( self.evaluate(connected_components(tf.cast(images, tf.bool))), expected)
def test_multiple_images(): images = tf.cast( [ [[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]], [[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 1]], [[1, 1, 0, 1], [0, 1, 1, 0], [1, 0, 1, 0], [0, 0, 1, 1]], ], tf.bool, ) expected = [ [[1, 1, 1, 1], [1, 0, 0, 1], [1, 0, 0, 1], [1, 1, 1, 1]], [[2, 0, 0, 3], [0, 0, 0, 0], [0, 0, 0, 0], [4, 0, 0, 5]], [[6, 6, 0, 7], [0, 6, 6, 0], [8, 0, 6, 0], [0, 0, 6, 6]], ] np.testing.assert_equal(connected_components(images).numpy(), expected)
def test_snake_disconnected(): for i in range(SNAKE.shape[0]): for j in range(SNAKE.shape[1]): # If we disconnect any part of the snake except for the endpoints, # there will be 2 components. if SNAKE[i, j] and (i, j) not in [(1, 1), (6, 3)]: disconnected_snake = SNAKE.copy() disconnected_snake[i, j] = 0 components = connected_components( tf.cast(disconnected_snake, tf.bool)) assert np.max(components) == 2 bins = np.bincount(components.numpy().ravel()) # Nonzero number of pixels labeled 0, 1, or 2. assert bins[0] > 0 assert bins[1] > 0 assert bins[2] > 0
def testDisconnected(self): arr = tf.cast( [ [1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], ], tf.bool, ) expected = [ [1, 0, 0, 2, 0, 0, 0, 0, 3], [0, 4, 0, 0, 0, 5, 0, 6, 0], [7, 0, 8, 0, 0, 0, 9, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0], [0, 0, 11, 0, 0, 0, 0, 0, 0], ] self.assertAllEqual(self.evaluate(connected_components(arr)), expected)
def testSnake_disconnected(self): for i in range(SNAKE.shape[0]): for j in range(SNAKE.shape[1]): # If we disconnect any part of the snake except for the endpoints, # there will be 2 components. if SNAKE[i, j] and (i, j) not in [(1, 1), (6, 3)]: disconnected_snake = SNAKE.copy() disconnected_snake[i, j] = 0 components = self.evaluate( connected_components( tf.cast(disconnected_snake, tf.bool))) self.assertEqual(components.max(), 2, "disconnect (%d, %d)" % (i, j)) bins = np.bincount(components.ravel()) # Nonzero number of pixels labeled 0, 1, or 2. self.assertGreater(bins[0], 0) self.assertGreater(bins[1], 0) self.assertGreater(bins[2], 0)
def test_disconnected(): arr = tf.cast( [ [1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], ], tf.bool, ) expected = [ [1, 0, 0, 2, 0, 0, 0, 0, 3], [0, 4, 0, 0, 0, 5, 0, 6, 0], [7, 0, 8, 0, 0, 0, 9, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0], [0, 0, 11, 0, 0, 0, 0, 0, 0], ] np.testing.assert_equal(connected_components(arr).numpy(), expected)
def testSnake(self): # Single component with id 1. self.assertAllEqual( self.evaluate(connected_components(tf.cast(SNAKE, tf.bool))), SNAKE)
def testSimple(self): arr = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] # Single component with id 1. self.assertAllEqual( self.evaluate(connected_components(tf.cast(arr, tf.bool))), arr)
def test_zeros(): np.testing.assert_equal( connected_components(tf.zeros((100, 20, 50), tf.bool)), np.zeros((100, 20, 50)))
def testOnes_small(self): self.assertAllEqual( self.evaluate(connected_components(tf.ones((3, 5), tf.bool))), np.ones((3, 5)), )
def test_ones(): np.testing.assert_equal( connected_components(tf.ones((100, 20, 50), tf.bool)), np.tile(np.arange(100)[:, None, None] + 1, [1, 20, 50]), )
def test_ones_small(): np.testing.assert_equal( connected_components(tf.ones((3, 5), tf.bool)).numpy(), np.ones( (3, 5)))
def test_simple(): arr = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] # Single component with id 1. np.testing.assert_equal( connected_components(tf.cast(arr, tf.bool)).numpy(), arr)
def test_snake(): # Single component with id 1. np.testing.assert_equal( connected_components(tf.cast(SNAKE, tf.bool)).numpy(), SNAKE)