From 8fced506275c50fc66dc6c15e57ff59ae1fa7325 Mon Sep 17 00:00:00 2001 From: "paolo.bolettieri" Date: Wed, 13 Jul 2022 16:40:07 +0200 Subject: [PATCH] added FAISS Searcher --- src/BEBLIDParameters.py | 1 - src/GroundTruthEvaluation.py | 5 +++-- src/ImageRecognitionService.py | 35 ++++++++++++++++++++++------------ src/Searcher.py | 20 ++++++++++--------- src/SearcherParameters.py | 2 ++ 5 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 src/SearcherParameters.py diff --git a/src/BEBLIDParameters.py b/src/BEBLIDParameters.py index 5bdf0f5..671e3cc 100644 --- a/src/BEBLIDParameters.py +++ b/src/BEBLIDParameters.py @@ -5,4 +5,3 @@ KEYPOINTS_D = 250 IMG_SIZE_D = 500 KEYPOINTS_Q = 800 IMG_SIZE_Q = 500 -K_REORDERING = 1000 \ No newline at end of file diff --git a/src/GroundTruthEvaluation.py b/src/GroundTruthEvaluation.py index ed353cd..ccded25 100644 --- a/src/GroundTruthEvaluation.py +++ b/src/GroundTruthEvaluation.py @@ -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) diff --git a/src/ImageRecognitionService.py b/src/ImageRecognitionService.py index 392c2b8..4b3e89f 100644 --- a/src/ImageRecognitionService.py +++ b/src/ImageRecognitionService.py @@ -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 diff --git a/src/Searcher.py b/src/Searcher.py index b5cec40..bd1aaa4 100644 --- a/src/Searcher.py +++ b/src/Searcher.py @@ -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): diff --git a/src/SearcherParameters.py b/src/SearcherParameters.py new file mode 100644 index 0000000..2595672 --- /dev/null +++ b/src/SearcherParameters.py @@ -0,0 +1,2 @@ +SEARCH_DEEP_K = [0, 1000, 2000, 5000, 10000, 30000, 100000] +GEM_THRESHOLD = 0.25