Spaces:
Sleeping
Sleeping
shivangibithel
commited on
Commit
·
c45b099
1
Parent(s):
000b3b3
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,88 @@
|
|
1 |
import streamlit as st
|
2 |
st.set_page_config(page_title='ITR', page_icon="🧊", layout='centered')
|
3 |
st.title("LCM-Independent for Pascal Dataset")
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
#
|
17 |
-
#
|
18 |
-
|
19 |
-
#
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
#
|
25 |
-
|
26 |
-
#
|
27 |
-
#
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
#
|
39 |
-
#
|
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 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
|
85 |
query = st.text_input("Enter your search query here:")
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
1 |
import streamlit as st
|
2 |
st.set_page_config(page_title='ITR', page_icon="🧊", layout='centered')
|
3 |
st.title("LCM-Independent for Pascal Dataset")
|
4 |
+
import faiss
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import json
|
8 |
+
import zipfile
|
9 |
+
import pickle
|
10 |
+
from transformers import AutoTokenizer, CLIPTextModelWithProjection
|
11 |
|
12 |
+
# loading the train dataset
|
13 |
+
with open('clip_train.pkl', 'rb') as f:
|
14 |
+
temp_d = pickle.load(f)
|
15 |
+
# train_xv = temp_d['image'].astype(np.float64) # Array of image features : np ndarray
|
16 |
+
# train_xt = temp_d['text'].astype(np.float64) # Array of text features : np ndarray
|
17 |
+
# train_yv = temp_d['label'] # Array of labels
|
18 |
+
train_yt = temp_d['label'] # Array of labels
|
19 |
+
# ids = list(temp_d['ids']) # image names == len(images)
|
20 |
|
21 |
+
# loading the test dataset
|
22 |
+
with open('clip_test.pkl', 'rb') as f:
|
23 |
+
temp_d = pickle.load(f)
|
24 |
+
# test_xv = temp_d['image'].astype(np.float64)
|
25 |
+
test_xt = temp_d['text'].astype(np.float64)
|
26 |
+
# test_yv = temp_d['label']
|
27 |
+
# test_yt = temp_d['label']
|
28 |
|
29 |
+
# Map the image ids to the corresponding image URLs
|
30 |
+
image_map_name = 'pascal_dataset.csv'
|
31 |
+
df = pd.read_csv(image_map_name)
|
32 |
+
image_list = list(df['image'])
|
33 |
+
class_list = list(df['class'])
|
34 |
|
35 |
+
zip_path = "pascal_raw.zip"
|
36 |
+
zip_file = zipfile.ZipFile(zip_path)
|
37 |
|
38 |
+
# text_model = CLIPTextModelWithProjection.from_pretrained("openai/clip-vit-base-patch32")
|
39 |
+
# text_tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32")
|
40 |
|
41 |
+
text_index = faiss.read_index("text_index.index")
|
42 |
|
43 |
+
def T2Isearch(query, k=50):
|
44 |
+
# Encode the text query
|
45 |
+
# inputs = text_tokenizer([query], padding=True, return_tensors="pt")
|
46 |
+
# outputs = text_model(**inputs)
|
47 |
+
# query_embedding = outputs.text_embeds
|
48 |
+
query_embedding = test_xt[0]
|
49 |
+
query_vector = np.array([query_embedding])
|
50 |
+
faiss.normalize_L2(query_vector)
|
51 |
+
# text_index.nprobe = index.ntotal
|
52 |
+
text_index.nprobe = 100
|
53 |
|
54 |
+
# Search for the nearest neighbors in the FAISS text index
|
55 |
+
D, I = text_index.search(query_vector, k)
|
56 |
|
57 |
+
# get rank of all classes wrt to query
|
58 |
+
classes_all = []
|
59 |
+
Y = train_yt
|
60 |
+
neighbor_ys = Y[I]
|
61 |
+
class_freq = np.zeros(Y.shape[1])
|
62 |
+
for neighbor_y in neighbor_ys:
|
63 |
+
classes = np.where(neighbor_y > 0.5)[0]
|
64 |
+
for _class in classes:
|
65 |
+
class_freq[_class] += 1
|
66 |
|
67 |
+
count = 0
|
68 |
+
for i in range(len(class_freq)):
|
69 |
+
if class_freq[i]>0:
|
70 |
+
count +=1
|
71 |
+
ranked_classes = np.argsort(-class_freq) # chosen order of pivots -- predicted sequence of all labels for the query
|
72 |
+
ranked_classes_after_knn = ranked_classes[:count] # predicted sequence of top labels after knn search
|
73 |
|
74 |
+
lis = ['aeroplane', 'bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse','motorbike','person','pottedplant','sheep','sofa','train','tvmonitor']
|
75 |
+
class_ = lis[ranked_classes_after_knn[0]-1]
|
76 |
|
77 |
+
# Map the image ids to the corresponding image URLs
|
78 |
+
for i in range(len(image_list)):
|
79 |
+
if class_list[i] == class_ :
|
80 |
+
image_name = image_list[i]
|
81 |
+
image_data = zip_file.open("pascal_raw/images/dataset/"+ image_name)
|
82 |
+
image = Image.open(image_data)
|
83 |
+
st.image(image, width=600)
|
84 |
|
85 |
query = st.text_input("Enter your search query here:")
|
86 |
+
if st.button("Search"):
|
87 |
+
if query:
|
88 |
+
T2Isearch(query)
|