Spaces:
Sleeping
Sleeping
Yuxiang Wang
commited on
Commit
•
af9c1e6
1
Parent(s):
679611d
reference bucket img for closest sample
Browse files- .gitignore +1 -0
- app.py +3 -3
- closest_sample.py +35 -6
.gitignore
CHANGED
@@ -13,3 +13,4 @@ model/
|
|
13 |
model_classification/
|
14 |
dataset/
|
15 |
*.png
|
|
|
|
13 |
model_classification/
|
14 |
dataset/
|
15 |
*.png
|
16 |
+
*.jpg
|
app.py
CHANGED
@@ -100,7 +100,7 @@ def explain_image(input_image,model_name):
|
|
100 |
print('done')
|
101 |
return saliency, integrated, smoothgrad,
|
102 |
|
103 |
-
|
104 |
with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
|
105 |
|
106 |
with gr.Tab(" Florrissant Fossils"):
|
@@ -120,7 +120,7 @@ with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
|
|
120 |
model_name = gr.Dropdown(
|
121 |
["Mummified 170", "Rock 170"],
|
122 |
multiselect=False,
|
123 |
-
value="Rock 170",
|
124 |
label="Model",
|
125 |
interactive=True,
|
126 |
)
|
@@ -170,7 +170,7 @@ with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
|
|
170 |
find_closest_btn.click(find_closest, inputs=[input_image,model_name], outputs=[closest_image_0,closest_image_1,closest_image_2,closest_image_3,closest_image_4])
|
171 |
#classify_segmented_button.click(classify_image, inputs=[segmented_image,model_name], outputs=class_predicted)
|
172 |
|
173 |
-
demo.queue()
|
174 |
|
175 |
if os.getenv('SYSTEM') == 'spaces':
|
176 |
demo.launch(width='40%',auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD')))
|
|
|
100 |
print('done')
|
101 |
return saliency, integrated, smoothgrad,
|
102 |
|
103 |
+
#minimalist theme
|
104 |
with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
|
105 |
|
106 |
with gr.Tab(" Florrissant Fossils"):
|
|
|
120 |
model_name = gr.Dropdown(
|
121 |
["Mummified 170", "Rock 170"],
|
122 |
multiselect=False,
|
123 |
+
value="Rock 170", # default option
|
124 |
label="Model",
|
125 |
interactive=True,
|
126 |
)
|
|
|
170 |
find_closest_btn.click(find_closest, inputs=[input_image,model_name], outputs=[closest_image_0,closest_image_1,closest_image_2,closest_image_3,closest_image_4])
|
171 |
#classify_segmented_button.click(classify_image, inputs=[segmented_image,model_name], outputs=class_predicted)
|
172 |
|
173 |
+
demo.queue() # manage multiple incoming requests
|
174 |
|
175 |
if os.getenv('SYSTEM') == 'spaces':
|
176 |
demo.launch(width='40%',auth=(os.environ.get('USERNAME'), os.environ.get('PASSWORD')))
|
closest_sample.py
CHANGED
@@ -4,7 +4,7 @@ import numpy as np
|
|
4 |
import pandas as pd
|
5 |
import os
|
6 |
from huggingface_hub import snapshot_download
|
7 |
-
|
8 |
|
9 |
|
10 |
pca_fossils = pk.load(open('pca_fossils_170_finer.pkl','rb'))
|
@@ -24,10 +24,18 @@ embedding_fossils = np.load('dataset/embedding_fossils_170_finer.npy')
|
|
24 |
fossils_pd= pd.read_csv('fossils_paths.csv')
|
25 |
|
26 |
def pca_distance(pca,sample,embedding):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
s = pca.transform(sample.reshape(1,-1))
|
28 |
all = pca.transform(embedding[:,-1])
|
29 |
distances = np.linalg.norm(all - s, axis=1)
|
30 |
-
print(distances)
|
31 |
return np.argsort(distances)[:5]
|
32 |
|
33 |
def return_paths(argsorted,files):
|
@@ -36,6 +44,16 @@ def return_paths(argsorted,files):
|
|
36 |
paths.append(files[i])
|
37 |
return paths
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def get_images(embedding):
|
41 |
|
@@ -47,7 +65,18 @@ def get_images(embedding):
|
|
47 |
|
48 |
paths = return_paths(pca_d,fossils_paths)
|
49 |
print(paths)
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import pandas as pd
|
5 |
import os
|
6 |
from huggingface_hub import snapshot_download
|
7 |
+
import requests
|
8 |
|
9 |
|
10 |
pca_fossils = pk.load(open('pca_fossils_170_finer.pkl','rb'))
|
|
|
24 |
fossils_pd= pd.read_csv('fossils_paths.csv')
|
25 |
|
26 |
def pca_distance(pca,sample,embedding):
|
27 |
+
"""
|
28 |
+
Args:
|
29 |
+
pca:fitted PCA model
|
30 |
+
sample:sample for which to find the closest embeddings
|
31 |
+
embedding:embeddings of the dataset
|
32 |
+
Returns:
|
33 |
+
The indices of the five closest embeddings to the sample
|
34 |
+
"""
|
35 |
s = pca.transform(sample.reshape(1,-1))
|
36 |
all = pca.transform(embedding[:,-1])
|
37 |
distances = np.linalg.norm(all - s, axis=1)
|
38 |
+
#print(distances)
|
39 |
return np.argsort(distances)[:5]
|
40 |
|
41 |
def return_paths(argsorted,files):
|
|
|
44 |
paths.append(files[i])
|
45 |
return paths
|
46 |
|
47 |
+
def download_public_image(url, destination_path):
|
48 |
+
response = requests.get(url)
|
49 |
+
if response.status_code == 200:
|
50 |
+
with open(destination_path, 'wb') as f:
|
51 |
+
f.write(response.content)
|
52 |
+
print(f"Downloaded image to {destination_path}")
|
53 |
+
return True
|
54 |
+
else:
|
55 |
+
print(f"Failed to download image from bucket. Status code: {response.status_code}")
|
56 |
+
return False
|
57 |
|
58 |
def get_images(embedding):
|
59 |
|
|
|
65 |
|
66 |
paths = return_paths(pca_d,fossils_paths)
|
67 |
print(paths)
|
68 |
+
|
69 |
+
folder_florissant = 'https://storage.googleapis.com/serrelab/prj_fossils/2024/Florissant_Fossil_v2.0/'
|
70 |
+
folder_general = 'https://storage.googleapis.com/serrelab/prj_fossils/2024/General_Fossil_v2.0/'
|
71 |
+
|
72 |
+
for i, path in enumerate(paths):
|
73 |
+
local_file_path = f'image_{i}.jpg'
|
74 |
+
public_path_florissant = path.replace('/gpfs/data/tserre/irodri15/Fossils/new_data/leavesdb-v1_1/images/Fossil/Florissant_Fossil/512/full/jpg/', folder_florissant)
|
75 |
+
success = download_public_image(public_path_florissant, local_file_path)
|
76 |
+
|
77 |
+
if not success:
|
78 |
+
public_path_general = path.replace('/gpfs/data/tserre/irodri15/Fossils/new_data/leavesdb-v1_1/images/Fossil/Florissant_Fossil/512/full/jpg/', folder_general)
|
79 |
+
download_public_image(public_path_general, local_file_path)
|
80 |
+
#paths= [path.replace('/gpfs/data/tserre/irodri15/Fossils/new_data/leavesdb-v1_1/images/Fossil/Florissant_Fossil/512/full/jpg/',
|
81 |
+
# '/media/data_cifs/projects/prj_fossils/data/processed_data/leavesdb-v1_1/images/Fossil/Florissant_Fossil/original/full/jpg/') for path in paths]
|
82 |
+
return paths
|