유클리드 -> cosine으로 변경
Browse files
app.py
CHANGED
@@ -255,15 +255,23 @@ def search_similar_items(image, mask=None, top_k=10):
|
|
255 |
results = collection.query(
|
256 |
query_images=[np.array(query_image)],
|
257 |
n_results=top_k,
|
258 |
-
include=['metadatas', 'distances']
|
259 |
)
|
260 |
|
261 |
if not results or 'metadatas' not in results:
|
262 |
return []
|
263 |
|
264 |
similar_items = []
|
265 |
-
|
266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
item_data = metadata.copy()
|
268 |
item_data['similarity_score'] = similarity_score
|
269 |
similar_items.append(item_data)
|
|
|
255 |
results = collection.query(
|
256 |
query_images=[np.array(query_image)],
|
257 |
n_results=top_k,
|
258 |
+
include=['metadatas', 'distances', 'embeddings']
|
259 |
)
|
260 |
|
261 |
if not results or 'metadatas' not in results:
|
262 |
return []
|
263 |
|
264 |
similar_items = []
|
265 |
+
query_embedding = results.get('embeddings', [[]])[0] # 쿼리 이미지의 임베딩
|
266 |
+
|
267 |
+
for metadata, embedding in zip(results['metadatas'][0], results.get('embeddings', [[]] * len(results['metadatas'][0]))):
|
268 |
+
# 코사인 유사도 계산
|
269 |
+
# 이미 정규화되어 있으므로 내적만으로 코사인 유사도를 구할 수 있음
|
270 |
+
cosine_similarity = np.dot(query_embedding, embedding)
|
271 |
+
|
272 |
+
# -1~1 범위의 코사인 유사도를 0~100 범위로 변환
|
273 |
+
similarity_score = ((cosine_similarity + 1) / 2) * 100
|
274 |
+
|
275 |
item_data = metadata.copy()
|
276 |
item_data['similarity_score'] = similarity_score
|
277 |
similar_items.append(item_data)
|