Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -129,15 +129,53 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
|
|
129 |
|
130 |
# return structured_results
|
131 |
|
132 |
-
def find_similar_images(query_embedding,
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
-
#
|
137 |
structured_results = []
|
138 |
-
for idx in indices[0]:
|
139 |
-
|
140 |
-
similarity = cosine_similarity(query_embedding, faiss_index.reconstruct(idx).reshape(1, -1))[0][0]
|
141 |
structured_results.append({
|
142 |
'info': metadata,
|
143 |
'similarity': similarity
|
|
|
129 |
|
130 |
# return structured_results
|
131 |
|
132 |
+
#def find_similar_images(query_embedding, collection, top_k=5):
|
133 |
+
# # ChromaDB ์ฟผ๋ฆฌ
|
134 |
+
# results = collection.query(
|
135 |
+
# query_embeddings=query_embedding.reshape(1, -1), # 2D ๋ฐฐ์ด๋ก ๋ณํ
|
136 |
+
# n_results=top_k,
|
137 |
+
# include=['metadatas', 'embeddings'] # ๋ฉํ๋ฐ์ดํฐ์ ์๋ฒ ๋ฉ ํฌํจ
|
138 |
+
# )
|
139 |
+
#
|
140 |
+
# # ๋ฉํ๋ฐ์ดํฐ์ ์๋ฒ ๋ฉ ์ถ์ถ
|
141 |
+
# top_metadatas = results['metadatas'][0]
|
142 |
+
# top_embeddings = np.array(results['embeddings'][0]) # numpy ๋ฐฐ์ด๋ก ๋ณํ
|
143 |
+
|
144 |
+
# # ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
145 |
+
# query_embedding_normalized = normalize(query_embedding.reshape(1, -1), axis=1)
|
146 |
+
# top_embeddings_normalized = normalize(top_embeddings, axis=1)
|
147 |
+
# similarities = cosine_similarity(query_embedding_normalized, top_embeddings_normalized).flatten()
|
148 |
+
|
149 |
+
# structured_results = []
|
150 |
+
# for metadata, similarity in zip(top_metadatas, similarities):
|
151 |
+
# structured_results.append({
|
152 |
+
# 'info': metadata,
|
153 |
+
# 'similarity': similarity
|
154 |
+
# })
|
155 |
+
#
|
156 |
+
# return structured_results
|
157 |
+
|
158 |
+
|
159 |
+
def find_similar_images(query_embedding, collection, top_k=5):
|
160 |
+
# ChromaDB์์ ์๋ฒ ๋ฉ๊ณผ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
161 |
+
all_data = collection.get(include=['embeddings', 'metadatas'])
|
162 |
+
all_embeddings = np.array(all_data['embeddings']).astype('float32') # faiss๋ float32 ํ์
|
163 |
+
all_metadatas = all_data['metadatas']
|
164 |
+
|
165 |
+
# faiss ์ธ๋ฑ์ค๋ฅผ L2 ๊ฑฐ๋ฆฌ ๊ธฐ๋ฐ์ผ๋ก ์์ฑ (์ฝ์ฌ์ธ ์ ์ฌ๋๋ ์ ๊ทํ ํ์)
|
166 |
+
faiss.normalize_L2(all_embeddings) # L2 ์ ๊ทํ
|
167 |
+
index = faiss.IndexFlatIP(all_embeddings.shape[1]) # IP๋ Inner Product(์ฝ์ฌ์ธ ์ ์ฌ๋)
|
168 |
+
index.add(all_embeddings)
|
169 |
+
|
170 |
+
# ์ฟผ๋ฆฌ ์๋ฒ ๋ฉ ์ ๊ทํ ํ faiss ๊ฒ์
|
171 |
+
query_embedding = query_embedding.reshape(1, -1).astype('float32')
|
172 |
+
faiss.normalize_L2(query_embedding)
|
173 |
+
_, indices = index.search(query_embedding, top_k)
|
174 |
|
175 |
+
# ๊ฒ์๋ ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
|
176 |
structured_results = []
|
177 |
+
for metadata, idx in zip(all_metadatas, indices[0]):
|
178 |
+
similarity = np.dot(query_embedding, all_embeddings[idx]) # ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
|
|
179 |
structured_results.append({
|
180 |
'info': metadata,
|
181 |
'similarity': similarity
|