Skip to content

Commit

Permalink
Changed l2_distance to np/linalg.norm for distance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Mironova committed Feb 2, 2021
1 parent 94919c7 commit 457ca80
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,3 @@ def crop_resize(image, input_size):
image = cv2.resize(image, (input_size[1], input_size[0]))
image = np.expand_dims(image, axis=0)
return image


def l2_distance(query_embedding, gallery_embedding):
query_embedding = np.array(query_embedding)
gallery_embedding = np.array(gallery_embedding)
distance = (
np.sum(query_embedding ** 2, axis=1, keepdims=True)
+ np.sum(gallery_embedding ** 2, axis=1)
- 2 * np.dot(query_embedding, gallery_embedding.transpose())
)
return distance
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import cv2
from tqdm import tqdm

from place_recognition_demo.common import crop_resize, l2_distance
from place_recognition_demo.common import crop_resize

from openvino.inference_engine import IECore # pylint: disable=no-name-in-module

Expand Down Expand Up @@ -66,8 +66,10 @@ def compute_embedding(self, image):
def search_in_gallery(self, embedding):
''' Takes input embedding vector and searches it in the gallery. '''

distances = l2_distance(embedding, self.embeddings)
distances = distances.reshape([-1])
query_embedding = np.array(embedding)
gallery_embeddings = np.array(self.embeddings)
distances = np.array([np.linalg.norm(query_embedding - np.expand_dims(gallery_embedding, axis=0), ord=2)
for gallery_embedding in gallery_embeddings])
sorted_indexes = np.argsort(distances)
return sorted_indexes, distances

Expand Down

0 comments on commit 457ca80

Please sign in to comment.