Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -19,11 +19,7 @@ st.set_page_config(
|
|
19 |
layout="wide"
|
20 |
)
|
21 |
|
22 |
-
|
23 |
-
#tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
|
24 |
-
|
25 |
-
#model, preprocess = open_clip.create_model_from_pretrained(clip_model_name)
|
26 |
-
#tokenizer = open_clip.get_tokenizer(clip_model_name)
|
27 |
|
28 |
#st.write("Available models:", open_clip.list_models())
|
29 |
|
@@ -50,22 +46,22 @@ def process_image(image, preprocess):
|
|
50 |
|
51 |
def knn_get_score(knn, k, cat, vec):
|
52 |
allvecs = knn[f'{cat}_vecs']
|
53 |
-
st.write('allvecs.shape', allvecs.shape)
|
54 |
scores = knn[f'{cat}_scores']
|
55 |
-
st.write('scores.shape', scores.shape)
|
56 |
# Compute cosine similiarity of vec against allvecs
|
57 |
# (both are already normalized)
|
58 |
cos_sim_table = vec @ allvecs.T
|
59 |
-
st.write('cos_sim_table.shape', cos_sim_table.shape)
|
60 |
# Get sorted array indices by similiarity in descending order
|
61 |
sortinds = np.flip(np.argsort(cos_sim_table))
|
62 |
-
st.write('sortinds.shape', sortinds.shape)
|
63 |
# Get corresponding scores for the sorted vectors
|
64 |
kscores = scores[sortinds][:k]
|
65 |
-
st.write('kscores.shape', kscores.shape)
|
66 |
# Get actual sorted similiarity scores
|
67 |
-
ksims = cos_sim_table[sortinds][:k]
|
68 |
-
st.write('ksims.shape', ksims.shape)
|
69 |
# Apply normalization after exponential formula
|
70 |
ksims = softmax(10**ksims)
|
71 |
# Weighted sum
|
@@ -91,7 +87,7 @@ def main():
|
|
91 |
|
92 |
with st.spinner('Loading KNN model... This may take a moment.'):
|
93 |
knn = load_knn()
|
94 |
-
st.write(knn['walkability_vecs'].shape)
|
95 |
|
96 |
file = st.file_uploader('Upload An Image')
|
97 |
|
@@ -112,7 +108,7 @@ def main():
|
|
112 |
|
113 |
# Normalize vector
|
114 |
vec /= vec.norm(dim=-1, keepdim=True)
|
115 |
-
st.write(vec.shape)
|
116 |
vec = vec.numpy()
|
117 |
k = 40
|
118 |
for cat in ['walkability']:
|
|
|
19 |
layout="wide"
|
20 |
)
|
21 |
|
22 |
+
debug = True
|
|
|
|
|
|
|
|
|
23 |
|
24 |
#st.write("Available models:", open_clip.list_models())
|
25 |
|
|
|
46 |
|
47 |
def knn_get_score(knn, k, cat, vec):
|
48 |
allvecs = knn[f'{cat}_vecs']
|
49 |
+
if debug: st.write('allvecs.shape', allvecs.shape)
|
50 |
scores = knn[f'{cat}_scores']
|
51 |
+
if debug: st.write('scores.shape', scores.shape)
|
52 |
# Compute cosine similiarity of vec against allvecs
|
53 |
# (both are already normalized)
|
54 |
cos_sim_table = vec @ allvecs.T
|
55 |
+
if debug: st.write('cos_sim_table.shape', cos_sim_table.shape)
|
56 |
# Get sorted array indices by similiarity in descending order
|
57 |
sortinds = np.flip(np.argsort(cos_sim_table))
|
58 |
+
if debug: st.write('sortinds.shape', sortinds.shape)
|
59 |
# Get corresponding scores for the sorted vectors
|
60 |
kscores = scores[sortinds][:k]
|
61 |
+
if debug: st.write('kscores.shape', kscores.shape)
|
62 |
# Get actual sorted similiarity scores
|
63 |
+
ksims = cos_sim_table[:, sortinds][:k]
|
64 |
+
if debug: st.write('ksims.shape', ksims.shape)
|
65 |
# Apply normalization after exponential formula
|
66 |
ksims = softmax(10**ksims)
|
67 |
# Weighted sum
|
|
|
87 |
|
88 |
with st.spinner('Loading KNN model... This may take a moment.'):
|
89 |
knn = load_knn()
|
90 |
+
if debug: st.write(knn['walkability_vecs'].shape)
|
91 |
|
92 |
file = st.file_uploader('Upload An Image')
|
93 |
|
|
|
108 |
|
109 |
# Normalize vector
|
110 |
vec /= vec.norm(dim=-1, keepdim=True)
|
111 |
+
if debug: st.write(vec.shape)
|
112 |
vec = vec.numpy()
|
113 |
k = 40
|
114 |
for cat in ['walkability']:
|