JoJosmin commited on
Commit
3c76019
โ€ข
1 Parent(s): 80c6ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -7
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, faiss_index, all_metadatas, top_k=5):
133
- query_embedding = query_embedding.astype('float32').reshape(1, -1) # ์ฐจ์› ์กฐ์ • ๋ฐ ํ˜•๋ณ€ํ™˜
134
- _, indices = faiss_index.search(query_embedding, top_k) # ์œ ์‚ฌํ•œ ๋ฒกํ„ฐ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- # ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์™€ ์œ ์‚ฌ๋„ ๊ฐ€์ ธ์˜ค๊ธฐ
137
  structured_results = []
138
- for idx in indices[0]:
139
- metadata = all_metadatas[idx]
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