Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -130,23 +130,38 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
|
|
130 |
#
|
131 |
# return structured_results
|
132 |
|
133 |
-
def find_similar_images(query_embedding, collection, top_k=5):
|
134 |
-
# ๋ชจ๋ ์๋ฒ ๋ฉ๊ณผ ๋ฉํ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
135 |
-
all_data = collection.get(include=['embeddings', 'metadatas'])
|
136 |
-
all_embeddings = np.array(all_data['embeddings']) # ์๋ฒ ๋ฉ ๋ฐฐ์ด๋ก ๋ณํ
|
137 |
-
all_metadatas = all_data['metadatas'] # ๋ฉํ๋ฐ์ดํฐ ๋ฆฌ์คํธ
|
138 |
-
|
139 |
-
# ์ฟผ๋ฆฌ ์๋ฒ ๋ฉ๊ณผ์ ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
140 |
query_embedding = query_embedding.reshape(1, -1) # ์ฟผ๋ฆฌ ์๋ฒ ๋ฉ ์ฐจ์ ์กฐ์
|
141 |
-
|
142 |
-
|
143 |
-
#
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
# ์ต์ข
๊ฒฐ๊ณผ ํ์์ผ๋ก ๋ฐํ
|
148 |
-
results = [{'info': metadata, 'similarity': similarity} for similarity, metadata in sorted_pairs]
|
149 |
-
return results
|
150 |
|
151 |
|
152 |
|
|
|
130 |
#
|
131 |
# return structured_results
|
132 |
|
133 |
+
def find_similar_images(query_embedding, collection, top_k=5, batch_size=500):
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
query_embedding = query_embedding.reshape(1, -1) # ์ฟผ๋ฆฌ ์๋ฒ ๋ฉ ์ฐจ์ ์กฐ์
|
135 |
+
all_results = []
|
136 |
+
|
137 |
+
# ์ ์ฒด ๋ฐ์ดํฐ์ ๊ธธ์ด๋ฅผ ๊ณ์ฐ
|
138 |
+
data_length = len(collection.get(include=['metadatas'])['metadatas'])
|
139 |
+
|
140 |
+
for start in range(0, data_length, batch_size):
|
141 |
+
end = min(start + batch_size, data_length)
|
142 |
+
|
143 |
+
# ํด๋น ๋ฐฐ์น ๋ฒ์์ ์๋ฒ ๋ฉ๊ณผ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ด
|
144 |
+
all_data = collection.get(
|
145 |
+
include=['embeddings', 'metadatas'],
|
146 |
+
start=start,
|
147 |
+
end=end
|
148 |
+
)
|
149 |
+
|
150 |
+
batch_embeddings = np.array(all_data['embeddings'])
|
151 |
+
batch_metadatas = all_data['metadatas']
|
152 |
+
|
153 |
+
# ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
154 |
+
similarities = cosine_similarity(query_embedding, batch_embeddings).flatten()
|
155 |
+
|
156 |
+
# ํ์ฌ ๋ฐฐ์น์์ ์ ์ฌ๋์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์์ผ๋ก ๋ฌถ์ด ์ถ๊ฐ
|
157 |
+
batch_results = [{'info': metadata, 'similarity': similarity} for similarity, metadata in zip(similarities, batch_metadatas)]
|
158 |
+
all_results.extend(batch_results)
|
159 |
+
|
160 |
+
# ์ ์ฒด ๊ฒฐ๊ณผ ์ค์์ ์ ์ฌ๋๊ฐ ๋์ ์์๋๋ก top_k ๊ฐ๋ง ์ ํ
|
161 |
+
sorted_results = sorted(all_results, key=lambda x: x['similarity'], reverse=True)[:top_k]
|
162 |
+
|
163 |
+
return sorted_results
|
164 |
|
|
|
|
|
|
|
165 |
|
166 |
|
167 |
|