Spaces:
Running
Running
Instantaneous1
commited on
Commit
•
3a4f968
1
Parent(s):
d1407c6
norm
Browse files
app.py
CHANGED
@@ -190,8 +190,10 @@ def build_annoy_index(features):
|
|
190 |
print("Building faiss index:")
|
191 |
f = features[0].shape[0] # Feature dimensionality
|
192 |
index = faiss.IndexIDMap(faiss.IndexFlatIP(f))
|
|
|
|
|
193 |
index.add_with_ids(
|
194 |
-
features
|
195 |
) # Adjust num_trees for accuracy vs. speed trade-off
|
196 |
print("built faiss index:")
|
197 |
return index
|
@@ -206,6 +208,7 @@ def search_similar_images(query_image, num_results, f=FEATURES):
|
|
206 |
proc_image = preprocess_image(query_image).unsqueeze(0).to(DEVICE)
|
207 |
query_feature = model(proc_image)
|
208 |
query_feature = query_feature.cpu().detach().numpy()
|
|
|
209 |
distances, nearest_neighbors = index.search(
|
210 |
query_feature,
|
211 |
num_results,
|
@@ -232,7 +235,7 @@ def display_image(idx, dist):
|
|
232 |
# print(file_paths[idx])
|
233 |
image = Image.open(file_paths[idx])
|
234 |
st.image(image.resize([256, 256]))
|
235 |
-
st.markdown("SimScore:
|
236 |
# st.markdown(file_paths[idx])
|
237 |
|
238 |
|
|
|
190 |
print("Building faiss index:")
|
191 |
f = features[0].shape[0] # Feature dimensionality
|
192 |
index = faiss.IndexIDMap(faiss.IndexFlatIP(f))
|
193 |
+
features = features.cpu().detach().numpy()
|
194 |
+
faiss.normalize_L2(features)
|
195 |
index.add_with_ids(
|
196 |
+
features, np.array(range(len(features)))
|
197 |
) # Adjust num_trees for accuracy vs. speed trade-off
|
198 |
print("built faiss index:")
|
199 |
return index
|
|
|
208 |
proc_image = preprocess_image(query_image).unsqueeze(0).to(DEVICE)
|
209 |
query_feature = model(proc_image)
|
210 |
query_feature = query_feature.cpu().detach().numpy()
|
211 |
+
faiss.normalize_L2(query_feature)
|
212 |
distances, nearest_neighbors = index.search(
|
213 |
query_feature,
|
214 |
num_results,
|
|
|
235 |
# print(file_paths[idx])
|
236 |
image = Image.open(file_paths[idx])
|
237 |
st.image(image.resize([256, 256]))
|
238 |
+
st.markdown("SimScore: " + str(round(dist, 2)))
|
239 |
# st.markdown(file_paths[idx])
|
240 |
|
241 |
|