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 3, 2021
1 parent 94919c7 commit 7676c87
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 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,7 @@ 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])
distances = np.linalg.norm(embedding - self.embeddings, axis=1, ord=2)
sorted_indexes = np.argsort(distances)
return sorted_indexes, distances

Expand All @@ -84,7 +83,7 @@ def compute_gallery_embeddings(self):
image = crop_resize(image, self.input_size)
images.append(image)

embeddings = [self.model.predict(image).reshape([-1]) for image in tqdm(
images, desc='Computing embeddings of gallery images.')]
embeddings = np.vstack([self.model.predict(image) for image in tqdm(
images, desc='Computing embeddings of gallery images.')])

return embeddings

0 comments on commit 7676c87

Please sign in to comment.