How to properly get the distance of an image embedding against a list/group of another image embedding?
I have a pretrained model that i am using to extract embeddings from images, and i would like to get the distance of an image against few other images i.e.
Embedding (1028,) against Embedding (5, 1028)
I am trying to do an image similarity experiment where im using the Cosine similarity metric from Tensorflow to compute the distance between two embedding, and it works well on a 1-to-1 computation i.e.
Embedding_1 = (1028,)
Embedding_2 = (1028,)
metrics.CosineSimilarity(Embedding_1, Embedding_2)
but i can't figure out how to do it on a 1-to-N distance computation.
Embedding_1 = (1028,)
Embedding_Group = [(1028,),(1028,),(1028,),(1028,),(1028,)]
CodePudding user response:
It could be done with broadcasting. Iterating over images and computing distance for each individual pair is bad idea in this case since it won't be parallelized (unless you know how to do it yourself).
import tensorflow as tf
embedding = tf.constant([1., 1.]) # your shape here is (1028,) instead of (2,)
embedding_group = tf.constant([[1., 1.], [1., 2.], [0., 1.]]) # your shape here is (5, 1028) instead of (3, 2)
norm_embedding = tf.nn.l2_normalize(embedding[None, ...], axis=-1)
norm_embedding_group = tf.nn.l2_normalize(embedding_group, axis=-1)
similarity = tf.reduce_sum(norm_embedding * norm_embedding_group, axis=-1) # cosine similarity of same shape as number of samples
print(norm_embedding.numpy())
print(norm_embedding_group.numpy())
print(similarity.numpy())
# [[0.7071067 0.7071067]]
# [[0.7071067 0.7071067 ]
# [0.44721356 0.8944271 ]
# [0. 1. ]]
# [0.9999998 0.94868314 0.7071067 ]