def test_cpp_extension(self): self.assertTrue((hashranking.hamming_rank([[1.0]], [[1.0]]) == [[0]]).all()) self.assertTrue((hashranking.hamming_rank( [[1.0, -1.0, -1.0], [-1.0, 1.0, -1.0], [1.0, 1.0, -1.0]], [[1.0, -1.0, -1.0], [1.0, 1.0, -1.0], [-1.0, 1.0, -1.0]]) == [[ 0, 1, 2 ], [2, 1, 0], [1, 0, 2]]).all())
def test_on_random_64(self): b1 = np.random.rand(200, 64).astype(np.float32) - 0.5 b2 = np.random.rand(500, 64).astype(np.float32) - 0.5 d1 = hashranking.hamming_rank(b1, b2) d2 = hashranking.numpy_implementation.hamming_rank(b1, b2) self.assertTrue((d1 == d2).all())
def test_map_from_rank(self): db_size = 1000 query_size = 200 class_count = 10 hash_size = 24 top_n = 50 db = np.random.randint(class_count, size=db_size, dtype=np.uint32) query = np.random.randint(class_count, size=query_size, dtype=np.uint32) hashes_class = np.random.rand(class_count, hash_size).astype( np.float32) - 0.5 hashes_db = hashes_class[db] + 0.3 * np.random.randn( db_size, hash_size).astype(np.float32) hashes_query = hashes_class[query] + 0.3 * np.random.randn( query_size, hash_size).astype(np.float32) s = hashranking.numpy_implementation._compute_similarity(db, query) rank = hashranking.numpy_implementation.hamming_rank( hashes_query, hashes_db) mAP_py, p, r = hashranking.numpy_implementation.compute_map_from_rank( rank, s, top_n) rank = hashranking.hamming_rank(hashes_query, hashes_db) mAP_cpp, p, r = hashranking.compute_map_from_rank(rank, s, top_n) self.assertEqual(mAP_py, mAP_cpp)