JLD commited on
Commit
ea9d736
1 Parent(s): 5a1068e

Fixed n_searched_images=1 case

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -23,10 +23,12 @@ def text_2_image(model, img_emb, img_names, img_urls, n_top_k_images):
23
  if st.button("Convert"):
24
  st.write("The image with the most similar embedding is:")
25
  cosine_sim = get_match(model, text, img_emb)
26
- logger.info(cosine_sim.shape)
27
- top_k_images_indices = torch.topk(cosine_sim, n_top_k_images, 1).indices
28
- logger.info(top_k_images_indices.squeeze().tolist())
29
- images_found = [img_names[top_k_best_image] for top_k_best_image in top_k_images_indices.squeeze().tolist()]
 
 
30
  cols = st.columns(n_top_k_images)
31
  for i, image_found in enumerate(images_found):
32
  logger.success(f"Image match found: {image_found}")
@@ -51,11 +53,12 @@ def image_2_image(model, img_emb, img_names, img_urls,n_top_k_images):
51
  if st.button("Convert"):
52
  st.write("The image with the most similar embedding is:")
53
  cosine_sim = get_match(model, image.convert("RGB"), img_emb)
54
- logger.info(cosine_sim.shape)
55
- top_k_images_indices = torch.topk(cosine_sim, n_top_k_images, 1).indices
56
- logger.info(top_k_images_indices.squeeze().tolist())
57
- # The problem is here
58
- images_found = [img_names[top_k_best_image] for top_k_best_image in top_k_images_indices.squeeze().tolist()]
 
59
  cols = st.columns(n_top_k_images)
60
  for i, image_found in enumerate(images_found):
61
  logger.success(f"Image match found: {image_found}")
 
23
  if st.button("Convert"):
24
  st.write("The image with the most similar embedding is:")
25
  cosine_sim = get_match(model, text, img_emb)
26
+ top_k_images_indices = torch.topk(cosine_sim, n_top_k_images, 1).indices.squeeze()
27
+ if top_k_images_indices.nelement() == 1:
28
+ top_k_images_indices = [top_k_images_indices.tolist()]
29
+ else:
30
+ top_k_images_indices = top_k_images_indices.tolist()
31
+ images_found = [img_names[top_k_best_image] for top_k_best_image in top_k_images_indices]
32
  cols = st.columns(n_top_k_images)
33
  for i, image_found in enumerate(images_found):
34
  logger.success(f"Image match found: {image_found}")
 
53
  if st.button("Convert"):
54
  st.write("The image with the most similar embedding is:")
55
  cosine_sim = get_match(model, image.convert("RGB"), img_emb)
56
+ top_k_images_indices = torch.topk(cosine_sim, n_top_k_images, 1).indices.squeeze()
57
+ if top_k_images_indices.nelement() == 1:
58
+ top_k_images_indices = [top_k_images_indices.tolist()]
59
+ else:
60
+ top_k_images_indices = top_k_images_indices.tolist()
61
+ images_found = [img_names[top_k_best_image] for top_k_best_image in top_k_images_indices]
62
  cols = st.columns(n_top_k_images)
63
  for i, image_found in enumerate(images_found):
64
  logger.success(f"Image match found: {image_found}")