@@ -48,6 +48,11 @@ def insert_images(
4848 }
4949
5050
51+ def similarity_score (distance ):
52+ # 2 is the maximum distance between normalised vectors
53+ return 100 * (1 - distance / 2 )
54+
55+
5156def search (model_name , url , limit = 10 ):
5257 embedding = embeddings [model_name ].extract (load_image_from_url (url ))
5358 search_results = collections [model_name ].search (
@@ -68,7 +73,7 @@ def search(model_name, url, limit=10):
6873 {
6974 "url" : hit .id ,
7075 "metadata" : json .loads (hit .entity .get ("metadata" )),
71- "similarity" : 100 * ( 1 - hit .distance ),
76+ "similarity" : similarity_score ( hit .distance ),
7277 }
7378 for hit in search_results [0 ]
7479 ]
@@ -84,7 +89,7 @@ def compare(model_name, url_left, url_right):
8489 # it's a bit overkill anyway if we don't compare with vectors from the db
8590 if metrics [model_name ] == "L2" :
8691 # _squared_ L2, to be consistent with the distances in milvus' search
87- return 100 * ( 1 - np .sum (np .square (np .array (left ) - np .array (right ))))
92+ return similarity_score ( np .sum (np .square (np .array (left ) - np .array (right ))))
8893
8994 raise RuntimeError (
9095 "Distance calculation has not been implemented in the API. "
0 commit comments