added faiss
Browse files
app.py
CHANGED
@@ -5,28 +5,42 @@ from setup import *
|
|
5 |
|
6 |
from PIL import Image
|
7 |
|
|
|
8 |
def show_result(search_request,
|
9 |
search_result,
|
10 |
img_dir,
|
11 |
container) :
|
12 |
|
13 |
-
# lorax = Image.open('img/Lorax.jpg')
|
14 |
-
# print(lorax.width, lorax.height)
|
15 |
-
# st.image(lorax, width = 250)
|
16 |
-
|
17 |
container.header("\"" +search_request+ "\" reminds me of :")
|
18 |
i = 0
|
19 |
-
for _ in range(0,
|
20 |
-
for col in container.columns(2)
|
|
|
|
|
21 |
image_name, comment, score = search_result[i]
|
22 |
-
col.image(img_dir + image_name, width = 300)
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if score != '' :
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
else :
|
28 |
-
|
|
|
|
|
|
|
29 |
i = i + 1
|
|
|
30 |
return
|
31 |
|
32 |
def show_landing() :
|
@@ -52,6 +66,13 @@ def show_landing() :
|
|
52 |
search_result,
|
53 |
IMAGE_DIR+'/',
|
54 |
results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return
|
56 |
|
57 |
|
|
|
5 |
|
6 |
from PIL import Image
|
7 |
|
8 |
+
thumbnail_width = 300
|
9 |
def show_result(search_request,
|
10 |
search_result,
|
11 |
img_dir,
|
12 |
container) :
|
13 |
|
|
|
|
|
|
|
|
|
14 |
container.header("\"" +search_request+ "\" reminds me of :")
|
15 |
i = 0
|
16 |
+
for _ in range(0, 3):
|
17 |
+
for col in container.columns(2):
|
18 |
+
if i >= len(search_result):
|
19 |
+
break
|
20 |
image_name, comment, score = search_result[i]
|
|
|
21 |
|
22 |
+
# Загрузка изображения
|
23 |
+
image = Image.open(img_dir + image_name)
|
24 |
+
|
25 |
+
# Выравнивание изображения по ширине
|
26 |
+
image_width, image_height = image.size
|
27 |
+
aspect_ratio = thumbnail_width / image_width
|
28 |
+
new_height = int(image_height * aspect_ratio)
|
29 |
+
resized_image = image.resize((thumbnail_width, new_height), Image.ANTIALIAS)
|
30 |
+
|
31 |
+
# Добавление подписи
|
32 |
if score != '' :
|
33 |
+
sim_score = f"{float(100 * score):.2f}"
|
34 |
+
sim='similarity='+sim_score + "%"
|
35 |
+
col.markdown(comment)
|
36 |
+
col.markdown(f'<p style="font-size: 10px;">{sim}</p>', unsafe_allow_html=True)
|
37 |
else :
|
38 |
+
# Вывод изображения в контейнер
|
39 |
+
col.markdown(comment)
|
40 |
+
|
41 |
+
col.image(resized_image, width=thumbnail_width)
|
42 |
i = i + 1
|
43 |
+
|
44 |
return
|
45 |
|
46 |
def show_landing() :
|
|
|
66 |
search_result,
|
67 |
IMAGE_DIR+'/',
|
68 |
results)
|
69 |
+
|
70 |
+
if action.button('Find Relsease 3!') and os.path.exists(IMAGE_DIR) :
|
71 |
+
search_result = search3(search_request)
|
72 |
+
show_result(search_request,
|
73 |
+
search_result,
|
74 |
+
IMAGE_DIR+'/',
|
75 |
+
results)
|
76 |
return
|
77 |
|
78 |
|
dataframe.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
|
4 |
-
def get_image_data() :
|
5 |
|
6 |
-
|
7 |
-
image_data_df = pd.read_csv ('data/output2.csv')
|
8 |
|
9 |
image_data_df['text_embeddings'] = image_data_df['text_embeddings'].apply(lambda x: np.fromstring(x[2:-2], sep=' ')).values
|
10 |
image_data_df['text_embeddings'] = image_data_df['text_embeddings'].apply(lambda x: np.reshape(x, (1, -1)))
|
11 |
|
12 |
-
return image_data_df
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
|
4 |
+
def get_image_data(csv_file) :
|
5 |
|
6 |
+
image_data_df = pd.read_csv (csv_file)
|
|
|
7 |
|
8 |
image_data_df['text_embeddings'] = image_data_df['text_embeddings'].apply(lambda x: np.fromstring(x[2:-2], sep=' ')).values
|
9 |
image_data_df['text_embeddings'] = image_data_df['text_embeddings'].apply(lambda x: np.reshape(x, (1, -1)))
|
10 |
|
11 |
+
return image_data_df
|
main.py
CHANGED
@@ -38,10 +38,29 @@ def search2(search_prompt : str) :
|
|
38 |
# Get model, processor & tokenizer
|
39 |
model, tokenizer = get_model_info(model_ID, device)
|
40 |
|
41 |
-
image_data_df = get_image_data()
|
42 |
|
43 |
return get_top_N_images(search_prompt,
|
44 |
data = image_data_df,
|
45 |
model=model, tokenizer=tokenizer,
|
46 |
device = device,
|
47 |
-
top_K=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Get model, processor & tokenizer
|
39 |
model, tokenizer = get_model_info(model_ID, device)
|
40 |
|
41 |
+
image_data_df = get_image_data('data/output2.csv')
|
42 |
|
43 |
return get_top_N_images(search_prompt,
|
44 |
data = image_data_df,
|
45 |
model=model, tokenizer=tokenizer,
|
46 |
device = device,
|
47 |
+
top_K=4)
|
48 |
+
|
49 |
+
def search3(search_prompt : str) :
|
50 |
+
|
51 |
+
# Set the device
|
52 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
53 |
+
|
54 |
+
# Define the model ID
|
55 |
+
model_ID = "openai/clip-vit-base-patch32"
|
56 |
+
|
57 |
+
# Get model, processor & tokenizer
|
58 |
+
model, tokenizer = get_model_info(model_ID, device)
|
59 |
+
|
60 |
+
image_data_df = get_image_data('data/output2.csv')
|
61 |
+
|
62 |
+
return faiss_get_top_N_images(search_prompt,
|
63 |
+
data = image_data_df,
|
64 |
+
model=model, tokenizer=tokenizer,
|
65 |
+
device = device,
|
66 |
+
top_K=4)
|
model.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
from transformers import
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
3 |
|
4 |
from dataframe import *
|
5 |
|
@@ -22,13 +23,13 @@ def get_single_text_embedding(text, model, tokenizer, device):
|
|
22 |
|
23 |
return embedding_as_np
|
24 |
|
25 |
-
def get_item_data(result, index) :
|
26 |
|
27 |
img_name = str(result['image_name'][index])
|
28 |
|
29 |
# TODO: add code to get the original comment
|
30 |
comment = str(result['comment'][index])
|
31 |
-
cos_sim = result[
|
32 |
|
33 |
return (img_name, comment, cos_sim)
|
34 |
|
@@ -36,29 +37,81 @@ def get_top_N_images(query,
|
|
36 |
data,
|
37 |
model, tokenizer,
|
38 |
device,
|
39 |
-
top_K=4
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import CLIPModel, CLIPTokenizer
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
import faiss
|
4 |
|
5 |
from dataframe import *
|
6 |
|
|
|
23 |
|
24 |
return embedding_as_np
|
25 |
|
26 |
+
def get_item_data(result, index, measure_column) :
|
27 |
|
28 |
img_name = str(result['image_name'][index])
|
29 |
|
30 |
# TODO: add code to get the original comment
|
31 |
comment = str(result['comment'][index])
|
32 |
+
cos_sim = result[measure_column][index]
|
33 |
|
34 |
return (img_name, comment, cos_sim)
|
35 |
|
|
|
37 |
data,
|
38 |
model, tokenizer,
|
39 |
device,
|
40 |
+
top_K=4) :
|
41 |
+
|
42 |
+
query_vect = get_single_text_embedding(query,
|
43 |
+
model, tokenizer,
|
44 |
+
device)
|
45 |
+
|
46 |
+
# Relevant columns
|
47 |
+
relevant_cols = ["comment", "image_name", "cos_sim"]
|
48 |
+
|
49 |
+
# Run similarity Search
|
50 |
+
data["cos_sim"] = data["text_embeddings"].apply(lambda x: cosine_similarity(query_vect, x))# line 17
|
51 |
+
data["cos_sim"] = data["cos_sim"].apply(lambda x: x[0][0])
|
52 |
+
|
53 |
+
data_sorted = data.sort_values(by='cos_sim', ascending=False)
|
54 |
+
non_repeated_images = ~data_sorted["image_name"].duplicated()
|
55 |
+
most_similar_articles = data_sorted[non_repeated_images].head(top_K)
|
56 |
+
|
57 |
+
"""
|
58 |
+
Retrieve top_K (4 is default value) articles similar to the query
|
59 |
+
"""
|
60 |
+
|
61 |
+
result_df = most_similar_articles[relevant_cols].reset_index()
|
62 |
+
|
63 |
+
return [get_item_data(result_df, i, 'cos_sim') for i in range(len(result_df))]
|
64 |
+
|
65 |
+
|
66 |
+
###### with faiss ###########
|
67 |
+
|
68 |
+
import faiss
|
69 |
+
import numpy as np
|
70 |
+
|
71 |
+
def faiss_add_index_cos(df, column):
|
72 |
+
|
73 |
+
# Get the embeddings from the specified column
|
74 |
+
embeddings = np.vstack(df[column].values).astype(np.float32) # Convert to float32
|
75 |
+
|
76 |
+
# Create an index
|
77 |
+
index = faiss.IndexFlatIP(embeddings.shape[1])
|
78 |
+
print("<<<<faiss_ after normalize")
|
79 |
+
faiss.normalize_L2(embeddings)
|
80 |
+
print("<<<<faiss_ after normalize")
|
81 |
+
|
82 |
+
index.train(embeddings)
|
83 |
+
print("<<<<faiss_ after index.train")
|
84 |
+
|
85 |
+
# Add the embeddings to the index
|
86 |
+
index.add(embeddings)
|
87 |
+
print("<<<<faiss_add")
|
88 |
+
|
89 |
+
# Return the index
|
90 |
+
return index
|
91 |
+
|
92 |
+
|
93 |
+
def faiss_get_top_N_images(query,
|
94 |
+
data,
|
95 |
+
model, tokenizer,
|
96 |
+
device,
|
97 |
+
top_K=4) :
|
98 |
+
|
99 |
+
query_vect = get_single_text_embedding(query,
|
100 |
+
model, tokenizer,
|
101 |
+
device)
|
102 |
+
# Relevant columns
|
103 |
+
relevant_cols = ["comment", "image_name", "similarity"]
|
104 |
+
|
105 |
+
#faiss search with cos similarity
|
106 |
+
index = faiss_add_index_cos(data, column="text_embeddings")
|
107 |
+
|
108 |
+
faiss.normalize_L2(query_vect)
|
109 |
+
D, I = index.search(query_vect, len(data))
|
110 |
+
|
111 |
+
data_sorted = data.iloc[I.flatten()]
|
112 |
+
|
113 |
+
non_repeated_images = ~data_sorted["image_name"].duplicated()
|
114 |
+
most_similar_articles = data_sorted[non_repeated_images].head(top_K)
|
115 |
+
|
116 |
+
result_df = most_similar_articles[relevant_cols].reset_index(), D.reshape(-1,1)[:top_K]
|
117 |
+
return [get_item_data(result_df, i, 'similarity') for i in range(len(result_df))]
|