def testKnnIndicesMaxDistance(self): points = tf.constant([[[1, 1, 1], [2, 2, 2], [4, 4, 4], [5, 5, 5]]], dtype=tf.float32) query_points = tf.constant([[ [1, 1, 1], [4, 4, 4], ]], dtype=tf.float32) valid_num = tf.constant([4], dtype=tf.int32) # Max distance so that one neighbor can be selected. max_distance = 2.0 expected_1nn = np.array([[[0], [2]]], dtype=np.int32) expected_1nn_padding = np.array([[[0], [0]]], dtype=np.float32) expected_2nn = np.array([[[0, 1], [2, 3]]], dtype=np.int32) expected_2nn_padding = np.array([[[0, 0], [0, 0]]], dtype=np.float32) expected_3nn = np.array([[[0, 1, 0], [2, 3, 2]]], dtype=np.int32) expected_3nn_padding = np.array([[[0, 0, 1], [0, 0, 1]]], dtype=np.float32) with self.session() as sess: output_1nn, output_1nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 1, valid_num, max_distance)) self.assertAllEqual(output_1nn, expected_1nn) self.assertAllEqual(output_1nn_padding, expected_1nn_padding) output_2nn, output_2nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 2, valid_num, max_distance)) self.assertAllEqual(output_2nn, expected_2nn) self.assertAllEqual(output_2nn_padding, expected_2nn_padding) output_3nn, output_3nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 3, valid_num, max_distance)) self.assertAllEqual(output_3nn, expected_3nn) self.assertAllEqual(output_3nn_padding, expected_3nn_padding) # Max distance so that only itself can be selected. max_distance = 0.1 expected_1nn = np.array([[[0], [2]]], dtype=np.int32) expected_1nn_padding = np.array([[[0], [0]]], dtype=np.float32) expected_2nn = np.array([[[0, 0], [2, 2]]], dtype=np.int32) expected_2nn_padding = np.array([[[0, 1], [0, 1]]], dtype=np.float32) expected_3nn = np.array([[[0, 0, 0], [2, 2, 2]]], dtype=np.int32) expected_3nn_padding = np.array([[[0, 1, 1], [0, 1, 1]]], dtype=np.float32) with self.session() as sess: output_1nn, output_1nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 1, valid_num, max_distance)) self.assertAllEqual(output_1nn, expected_1nn) self.assertAllEqual(output_1nn_padding, expected_1nn_padding) output_2nn, output_2nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 2, valid_num, max_distance)) self.assertAllEqual(output_2nn, expected_2nn) self.assertAllEqual(output_2nn_padding, expected_2nn_padding) output_3nn, output_3nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 3, valid_num, max_distance)) self.assertAllEqual(output_3nn, expected_3nn) self.assertAllEqual(output_3nn_padding, expected_3nn_padding)
def testKnnIndices(self): points = tf.constant([[[1, 1], [2, 2], [4, 4], [5, 5]]], dtype=tf.float32) query_points = tf.constant([[ [1, 1], [4, 4], ]], dtype=tf.float32) # Case 1: when all points are valid. valid_num = tf.constant([4], dtype=tf.int32) expected_1nn = np.array([[[0], [2]]], dtype=np.int32) expected_2nn = np.array([[[0, 1], [2, 3]]], dtype=np.int32) expected_3nn = np.array([[[0, 1, 2], [2, 3, 1]]], dtype=np.int32) with self.session() as sess: output_1nn, _ = sess.run( car_lib.KnnIndices(points, query_points, 1, valid_num)) self.assertAllEqual(output_1nn, expected_1nn) output_2nn, _ = sess.run( car_lib.KnnIndices(points, query_points, 2, valid_num)) self.assertAllEqual(output_2nn, expected_2nn) output_3nn, _ = sess.run( car_lib.KnnIndices(points, query_points, 3, valid_num)) self.assertAllEqual(output_3nn, expected_3nn) # Case 2: not all points are valid. valid_num = tf.constant([2], dtype=tf.int32) expected_1nn = np.array([[[0], [1]]], dtype=np.int32) expected_1nn_padding = np.array([[[0], [0]]], dtype=np.float32) expected_2nn = np.array([[[0, 1], [1, 0]]], dtype=np.int32) expected_2nn_padding = np.array([[[0, 0], [0, 0]]], dtype=np.float32) expected_3nn = np.array([[[0, 1, 2], [1, 0, 2]]], dtype=np.int32) expected_3nn_padding = np.array([[[0, 0, 1], [0, 0, 1]]], dtype=np.float32) with self.session() as sess: output_1nn, output_1nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 1, valid_num)) self.assertAllEqual(output_1nn, expected_1nn) self.assertAllEqual(output_1nn_padding, expected_1nn_padding) output_2nn, output_2nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 2, valid_num)) self.assertAllEqual(output_2nn, expected_2nn) self.assertAllEqual(output_2nn_padding, expected_2nn_padding) output_3nn, output_3nn_padding = sess.run( car_lib.KnnIndices(points, query_points, 3, valid_num)) self.assertAllEqual(output_3nn, expected_3nn) self.assertAllEqual(output_3nn_padding, expected_3nn_padding) # Case 3: explicit padding. padding = tf.constant([[1, 0, 1, 0]]) expected_1nn = np.array([[[1], [3]]], dtype=np.int32) expected_1nn_padding = np.array([[[0], [0]]], dtype=np.float32) expected_2nn = np.array([[[1, 3], [3, 1]]], dtype=np.int32) expected_2nn_padding = np.array([[[0, 0], [0, 0]]], dtype=np.float32) expected_3nn = np.array([[[1, 3, 0], [3, 1, 2]]], dtype=np.int32) expected_3nn_padding = np.array([[[0, 0, 1], [0, 0, 1]]], dtype=np.float32) with self.session() as sess: output_1nn, output_1nn_padding = sess.run( car_lib.NeighborhoodIndices(points, query_points, 1, padding)) self.assertAllEqual(output_1nn, expected_1nn) self.assertAllEqual(output_1nn_padding, expected_1nn_padding) output_2nn, output_2nn_padding = sess.run( car_lib.NeighborhoodIndices(points, query_points, 2, padding)) self.assertAllEqual(output_2nn, expected_2nn) self.assertAllEqual(output_2nn_padding, expected_2nn_padding) output_3nn, output_3nn_padding = sess.run( car_lib.NeighborhoodIndices(points, query_points, 3, padding)) self.assertAllEqual(output_3nn, expected_3nn) self.assertAllEqual(output_3nn_padding, expected_3nn_padding)