leedoming commited on
Commit
416f308
ยท
verified ยท
1 Parent(s): 25bc581

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -255,19 +255,19 @@ 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', '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
 
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
+ for metadata, distance in zip(results['metadatas'][0], results['distances'][0]):
266
+ # L2 ๊ฑฐ๋ฆฌ๋ฅผ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋กœ ๋ณ€ํ™˜
267
+ # ์ •๊ทœํ™”๋œ ๋ฒกํ„ฐ ๊ฐ„์˜ L2 ๊ฑฐ๋ฆฌ(d)์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„(cos_sim) ๊ด€๊ณ„:
268
+ # d^2 = 2(1 - cos_sim)
269
+ # cos_sim = 1 - (d^2/2)
270
+ cosine_similarity = 1 - (distance ** 2 / 2)
271
 
272
  # -1~1 ๋ฒ”์œ„์˜ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„๋ฅผ 0~100 ๋ฒ”์œ„๋กœ ๋ณ€ํ™˜
273
  similarity_score = ((cosine_similarity + 1) / 2) * 100