Skip to content

Commit

Permalink
Add tests for Descriptors and metric
Browse files Browse the repository at this point in the history
  • Loading branch information
luisremis committed Feb 20, 2019
1 parent a5503e8 commit 83ea9fc
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 1 deletion.
186 changes: 186 additions & 0 deletions tests/python/TestDescriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@

class TestDescriptors(unittest.TestCase):

def addSet(self, name, dim, metric, engine):

db = vdms.vdms()
db.connect(hostname, port)

all_queries = []

descriptor_set = {}
descriptor_set["name"] = name
descriptor_set["dimensions"] = dim
descriptor_set["metric"] = metric
descriptor_set["engine"] = engine

query = {}
query["AddDescriptorSet"] = descriptor_set

all_queries.append(query)

response, img_array = db.query(all_queries)

# Check success
self.assertEqual(response[0]["AddDescriptorSet"]["status"], 0)

def test_addSet(self):
db = vdms.vdms()
db.connect(hostname, port)
Expand All @@ -33,6 +56,21 @@ def test_addSet(self):
# Check success
self.assertEqual(response[0]["AddDescriptorSet"]["status"], 0)

def test_addDifferentSets(self):

self.addSet("128-L2-FaissFlat", 128, "L2", "FaissFlat")
self.addSet("128-IP-FaissFlat", 128, "IP", "FaissFlat")
self.addSet("128-L2-FaissIVFFlat", 128, "L2", "FaissIVFFlat")
self.addSet("128-IP-FaissIVFFlat", 128, "IP", "FaissIVFFlat")
self.addSet("128-L2-TileDBDense", 128, "L2", "TileDBDense")
self.addSet("128-L2-TileDBSparse", 128, "L2", "TileDBSparse")

self.addSet("4075-L2-FaissFlat", 4075, "L2", "FaissFlat")
self.addSet("4075-IP-FaissFlat", 4075, "IP", "FaissFlat")
self.addSet("4075-L2-FaissIVFFlat", 4075, "L2", "FaissIVFFlat")
self.addSet("4075-IP-FaissIVFFlat", 4075, "IP", "FaissIVFFlat")
self.addSet("4075-L2-TileDBDense", 4075, "L2", "TileDBDense")

def test_addSetAndDescriptors(self):
db = vdms.vdms()
db.connect(hostname, port)
Expand Down Expand Up @@ -124,6 +162,154 @@ def test_addDescriptorsx1000(self):
for x in range(0,total-1):
self.assertEqual(response[x]["AddDescriptor"]["status"], 0)

def test_addDescriptorsx1000FaissIVFFlat(self):
db = vdms.vdms()
db.connect(hostname, port)

all_queries = []

# Add Set
set_name = "faissivfflat_ip_128dx1000"
dims = 128
descriptor_set = {}
descriptor_set["name"] = set_name
descriptor_set["dimensions"] = dims
descriptor_set["metric"] = "IP"
descriptor_set["engine"] = "FaissIVFFlat"

query = {}
query["AddDescriptorSet"] = descriptor_set

all_queries.append(query)

response, img_array = db.query(all_queries)
self.assertEqual(response[0]["AddDescriptorSet"]["status"], 0)

all_queries = []
descriptor_blob = []

total = 1000;

for i in range(1,total):
x = np.ones(dims)
x[2] = 2.34 + i*20
x = x.astype('float32')
descriptor_blob.append(x.tobytes())

descriptor = {}
descriptor["set"] = set_name
descriptor["label"] = "classX"

query = {}
query["AddDescriptor"] = descriptor

all_queries.append(query)

response, img_array = db.query(all_queries, [descriptor_blob])

# Check success
for x in range(0,total-1):
self.assertEqual(response[x]["AddDescriptor"]["status"], 0)


def test_addDescriptorsx1000TileDBSparse(self):
db = vdms.vdms()
db.connect(hostname, port)

all_queries = []

# Add Set
set_name = "tiledbsparse_l2_128dx1000"
dims = 128
descriptor_set = {}
descriptor_set["name"] = set_name
descriptor_set["dimensions"] = dims
descriptor_set["metric"] = "L2"
descriptor_set["engine"] = "TileDBSparse"

query = {}
query["AddDescriptorSet"] = descriptor_set

all_queries.append(query)

response, img_array = db.query(all_queries)
self.assertEqual(response[0]["AddDescriptorSet"]["status"], 0)

all_queries = []
descriptor_blob = []

total = 1000;

for i in range(1,total):
x = np.ones(dims)
x[2] = 2.34 + i*20
x = x.astype('float32')
descriptor_blob.append(x.tobytes())

descriptor = {}
descriptor["set"] = set_name
descriptor["label"] = "classX"

query = {}
query["AddDescriptor"] = descriptor

all_queries.append(query)

response, img_array = db.query(all_queries, [descriptor_blob])

# Check success
for x in range(0,total-1):
self.assertEqual(response[x]["AddDescriptor"]["status"], 0)

def test_addDescriptorsx1000TileDBDense(self):
db = vdms.vdms()
db.connect(hostname, port)

all_queries = []

# Add Set
set_name = "tiledbdense_l2_128dx1000"
dims = 128
descriptor_set = {}
descriptor_set["name"] = set_name
descriptor_set["dimensions"] = dims
descriptor_set["metric"] = "L2"
descriptor_set["engine"] = "TileDBDense"

query = {}
query["AddDescriptorSet"] = descriptor_set

all_queries.append(query)

response, img_array = db.query(all_queries)
# print(json.dumps(all_queries, indent=4, sort_keys=False))
self.assertEqual(response[0]["AddDescriptorSet"]["status"], 0)

all_queries = []
descriptor_blob = []

total = 1000;

for i in range(1,total):
x = np.ones(dims)
x[2] = 2.34 + i*20
x = x.astype('float32')
descriptor_blob.append(x.tobytes())

descriptor = {}
descriptor["set"] = set_name
descriptor["label"] = "classX"

query = {}
query["AddDescriptor"] = descriptor

all_queries.append(query)

response, img_array = db.query(all_queries, [descriptor_blob])

# Check success
for x in range(0,total-1):
self.assertEqual(response[x]["AddDescriptor"]["status"], 0)

def test_classifyDescriptor(self):
db = vdms.vdms()
Expand Down
85 changes: 84 additions & 1 deletion tests/python/TestFindDescriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,43 @@ def test_findDescByConstraints(self):
self.assertEqual(response[0]["FindDescriptor"]
["entities"][0]["myid"], 205)

def test_findDescUnusedRef(self):

# Add Set
set_name = "features_128d_4_findunusedRef"
dims = 128
total = 1000
self.create_set_and_insert(set_name, dims, total)

db = vdms.vdms()
db.connect(hostname, port)

all_queries = []

finddescriptor = {}
finddescriptor["set"] = set_name

constraints = {}
constraints["myid"] = ["==", 205]
finddescriptor["constraints"] = constraints

results = {}
results["list"] = ["myid"]
finddescriptor["results"] = results

query = {}
query["FindDescriptor"] = finddescriptor

all_queries = []
all_queries.append(query)

response, blob_array = db.query(all_queries)

# Check success
self.assertEqual(response[0]["FindDescriptor"]["status"], 0)
self.assertEqual(response[0]["FindDescriptor"]["returned"], 1)
self.assertEqual(response[0]["FindDescriptor"]
["entities"][0]["myid"], 205)

def test_findDescByConst_get_id(self):

Expand Down Expand Up @@ -230,17 +267,62 @@ def test_findDescByBlobNoResults(self):

response, blob_array = db.query(all_queries, [descriptor_blob])

# Check success
self.assertEqual(response[0]["FindDescriptor"]["status"], 0)
self.assertEqual(response[0]["FindDescriptor"]["returned"], kn)
self.assertEqual(len(blob_array), kn)
self.assertEqual(descriptor_blob[0], blob_array[0])

def test_findDescByBlobUnusedRef(self):

# Add Set
set_name = "findwith_blobUnusedRef"
dims = 50
total = 100
self.create_set_and_insert(set_name, dims, total)

db = vdms.vdms()
db.connect(hostname, port)

kn = 3

all_queries = []

finddescriptor = {}
finddescriptor["set"] = set_name
finddescriptor["_ref"] = 1

results = {}
results["blob"] = True
finddescriptor["results"] = results
finddescriptor["k_neighbors"] = kn

query = {}
query["FindDescriptor"] = finddescriptor

all_queries = []
all_queries.append(query)

descriptor_blob = []
x = np.ones(dims)
x[2] = 2.34 + 30*20
x = x.astype('float32')
descriptor_blob.append(x.tobytes())

response, blob_array = db.query(all_queries, [descriptor_blob])

# Check success
self.assertEqual(response[0]["FindDescriptor"]["status"], 0)
self.assertEqual(response[0]["FindDescriptor"]["returned"], kn)
self.assertEqual(len(blob_array), kn)
self.assertEqual(descriptor_blob[0], blob_array[0])

# This Test is not passing:
# It should do knn and filter by constraints.
# def test_findDescByBlobAndConstraints(self):

# # Add Set
# set_name = "findwith_blob"
# set_name = "findwith_blob_const"
# dims = 128
# total = 100
# self.create_set_and_insert(set_name, dims, total)
Expand Down Expand Up @@ -278,6 +360,7 @@ def test_findDescByBlobNoResults(self):
# descriptor_blob.append(x.tobytes())

# response, blob_array = db.query(all_queries, [descriptor_blob])
# print(db.get_last_response_str())

# self.assertEqual(len(blob_array), kn)
# self.assertEqual(descriptor_blob[0], blob_array[0])
Expand Down

0 comments on commit 83ea9fc

Please sign in to comment.