added FAISS Searcher

This commit is contained in:
Paolo Bolettieri 2022-07-13 16:40:07 +02:00
parent 7484f767b3
commit 8fced50627
5 changed files with 39 additions and 24 deletions

View File

@ -5,4 +5,3 @@ KEYPOINTS_D = 250
IMG_SIZE_D = 500
KEYPOINTS_Q = 800
IMG_SIZE_Q = 500
K_REORDERING = 1000

View File

@ -47,14 +47,15 @@ if __name__ == '__main__':
try:
img_file = {'image': (
'query', open(os.path.join(path.parent, path.name), 'rb'))}
params = {'rescorer':'true'}
params = {'searchDeepLevel': 1}
r = requests.post(IMG_REC_SERVICE + 'searchByImg', data=params, files=img_file)
res = r.json()
print(res)
for i in range (0, len(res)):
#print(res[i][0])
if res[i][0] in exprected_id:
print("found at " + str(i))
precision_at[i] = precision_at[i] + 1
except Exception as e:
print("cannot process '%s'" % path)

View File

@ -65,10 +65,13 @@ def get_res(results, query_url=None):
@app.route('/bcir/searchById')
def search_by_id():
id = request.args.get('id')
rescorer = True
if request.args.get("rescorer") == 'false':
rescorer = False
results = searcher.search_by_id(id, settings.k, rescorer)
try:
searchDeepLevel = int(request.args.get("searchDeepLevel"))
except Exception as e:
print(e)
print('Setting default deep level 1')
search_deep_level = 1
results = searcher.search_by_id(id, settings.k, searchDeepLevel)
query_url = None
if request.args.get("tohtml") is not None:
query_url = id + ".jpg"
@ -83,16 +86,20 @@ def search_by_img():
file = request.files['image']
img_file = post_to_file(file)
rescorer = True
if request.form.get("rescorer") == 'false':
rescorer = False
try:
search_deep_level = int(request.form.get("searchDeepLevel"))
except Exception as e:
print(e)
print('Setting default deep level 1')
search_deep_level = 1
#dest_file = uuid.uuid4().hex + ".jpg"
#dest_path = settings.logs + "/" + dest_file
#file.save(dest_path)
#files = {'image': (dest_file, open(dest_path, 'rb'))}
#r = requests.post(settings.rmac_service, files=files)
#results = search_engine.search_by_img(np.array(r.json()), settings.k)
results = searcher.search_by_img(img_file, settings.k, rescorer)
results = searcher.search_by_img(img_file, settings.k, search_deep_level)
query_url = None
if request.form.get("tohtml") is not None:
query_url = ""
@ -102,9 +109,13 @@ def search_by_img():
@app.route('/bcir/searchByURL')
def search_by_url():
url = request.args.get('url')
rescorer = True
if request.args.get("rescorer") == 'false':
rescorer = False
try:
search_deep_level = int(request.args.get("searchDeepLevel"))
except Exception as e:
print(e)
print('Setting default deep level 1')
search_deep_level = 1
img_file = url_to_file(url)
# query = cv2.imdecode(image, cv2.IMREAD_COLOR)
# dest_file = uuid.uuid4().hex + ".jpg"
@ -113,7 +124,7 @@ def search_by_url():
# files = {'image': open(dest_path, 'rb')}
# r = requests.post(settings.rmac_service, files=files)
# results = search_engine.search_by_img(np.array(r.json()), settings.k)
results = searcher.search_by_img(img_file, settings.k, rescorer)
results = searcher.search_by_img(img_file, settings.k, search_deep_level)
query_url = None
if request.args.get("tohtml") is not None:
query_url = url

View File

@ -6,7 +6,7 @@ import LFUtilities
import ImageRecognitionSettings as settings
#from BEBLIDRescorerDB import BEBLIDRescorerDB
from BEBLIDRescorer import BEBLIDRescorer
import BEBLIDParameters
import SearcherParameters
from FAISSSearchEngine import FAISSSearchEngine
import FeatureExtractor as fe
@ -44,27 +44,29 @@ class Searcher:
self.save()
print('removed ' + id)
def search_by_id(self, query_id, k=10, rescorer=False):
def search_by_id(self, query_id, k=10, search_deep_level=1):
kq = k
if rescorer:
kq = BEBLIDParameters.K_REORDERING
if search_deep_level > 0:
kq = SearcherParameters.SEARCH_DEEP_K[search_deep_level]
res = self.search_engine.search_by_id(query_id, kq)
if rescorer:
if search_deep_level > 0:
res_lf = self.rescorer.rescore_by_id(query_id, res)
res = res_lf if res_lf else res[:k]
return res
@profile
def search_by_img(self, query_img, k=10, rescorer=False):
def search_by_img(self, query_img, k=10, search_deep_level=1):
kq = k
if rescorer:
kq = BEBLIDParameters.K_REORDERING
if search_deep_level:
kq = SearcherParameters.SEARCH_DEEP_K[search_deep_level]
query_desc = fe.extract(query_img)
res = self.search_engine.search_by_img(query_desc, kq)
if rescorer:
if search_deep_level > 0:
query_lf = lf.extract(query_img)
res_lf = self.rescorer.rescore_by_img(query_lf, res)
#res = res_lf if res_lf else res[:k]
res = res_lf if res_lf else res[:k]
res = [result for result in res if result[1] >= SearcherParameters.GEM_THRESHOLD]
return res
def save(self, is_backup=False):

View File

@ -0,0 +1,2 @@
SEARCH_DEEP_K = [0, 1000, 2000, 5000, 10000, 30000, 100000]
GEM_THRESHOLD = 0.25