Spaces:
Sleeping
Sleeping
Commit
·
92e3db3
1
Parent(s):
14b5411
implement user result saving
Browse files- app.py +28 -17
- config/config.yaml +6 -4
- src/utils.py +13 -4
app.py
CHANGED
|
@@ -20,6 +20,7 @@ def main():
|
|
| 20 |
title = gr.Markdown("# Saliency evaluation - experiment 1")
|
| 21 |
user_state = gr.State(0)
|
| 22 |
user_id = gr.State(0)
|
|
|
|
| 23 |
|
| 24 |
with gr.Row():
|
| 25 |
target_img_label = gr.Markdown(f"### Target image: {class_names[user_state.value]}")
|
|
@@ -45,7 +46,6 @@ def main():
|
|
| 45 |
|
| 46 |
gr.Markdown("### Image examples of the same class")
|
| 47 |
with gr.Row():
|
| 48 |
-
# generate random integer value
|
| 49 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 50 |
images = load_example_images(count, data_dir)
|
| 51 |
img1 = gr.Image(images[0])
|
|
@@ -70,14 +70,7 @@ def main():
|
|
| 70 |
|
| 71 |
def update_images(dropdown1, dropdown2, dropdown3, dropdown4, user_state):
|
| 72 |
|
| 73 |
-
|
| 74 |
-
print('dropdowns', dropdown1, dropdown2, dropdown3, dropdown4)
|
| 75 |
-
rank = [dropdown1,dropdown2,dropdown3,dropdown4]
|
| 76 |
-
print('rank', rank)
|
| 77 |
-
|
| 78 |
-
# image target and saliency images
|
| 79 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 80 |
-
print(count, config['dataset'][config['dataset']['name']]['n_classes'])
|
| 81 |
if count < config['dataset'][config['dataset']['name']]['n_classes']:
|
| 82 |
images = load_image_and_saliency(count, data_dir)
|
| 83 |
target_img = gr.Image(images[0], elem_classes="main-image")
|
|
@@ -130,10 +123,30 @@ def main():
|
|
| 130 |
dropdown4 = gr.Dropdown(choices=options, label="rise")
|
| 131 |
return dropdown1, dropdown2, dropdown3, dropdown4
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
submit_button.click(
|
| 134 |
update_state,
|
| 135 |
inputs=user_state,
|
| 136 |
outputs=user_state
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
).then(
|
| 138 |
update_img_label,
|
| 139 |
inputs=user_state,
|
|
@@ -151,15 +164,13 @@ def main():
|
|
| 151 |
inputs={dropdown1, dropdown2, dropdown3, dropdown4},
|
| 152 |
outputs={dropdown1, dropdown2, dropdown3, dropdown4}
|
| 153 |
)
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
user_id.value = id_generator.increment()
|
| 162 |
-
return user_id
|
| 163 |
|
| 164 |
demo.load(init, inputs=None, outputs=user_id)
|
| 165 |
|
|
|
|
| 20 |
title = gr.Markdown("# Saliency evaluation - experiment 1")
|
| 21 |
user_state = gr.State(0)
|
| 22 |
user_id = gr.State(0)
|
| 23 |
+
answers = gr.State([])
|
| 24 |
|
| 25 |
with gr.Row():
|
| 26 |
target_img_label = gr.Markdown(f"### Target image: {class_names[user_state.value]}")
|
|
|
|
| 46 |
|
| 47 |
gr.Markdown("### Image examples of the same class")
|
| 48 |
with gr.Row():
|
|
|
|
| 49 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 50 |
images = load_example_images(count, data_dir)
|
| 51 |
img1 = gr.Image(images[0])
|
|
|
|
| 70 |
|
| 71 |
def update_images(dropdown1, dropdown2, dropdown3, dropdown4, user_state):
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
count = user_state if isinstance(user_state, int) else user_state.value
|
|
|
|
| 74 |
if count < config['dataset'][config['dataset']['name']]['n_classes']:
|
| 75 |
images = load_image_and_saliency(count, data_dir)
|
| 76 |
target_img = gr.Image(images[0], elem_classes="main-image")
|
|
|
|
| 123 |
dropdown4 = gr.Dropdown(choices=options, label="rise")
|
| 124 |
return dropdown1, dropdown2, dropdown3, dropdown4
|
| 125 |
|
| 126 |
+
def init(request: gr.Request):
|
| 127 |
+
user_id.value = id_generator.increment()
|
| 128 |
+
return user_id
|
| 129 |
+
|
| 130 |
+
def redirect():
|
| 131 |
+
pass
|
| 132 |
+
|
| 133 |
+
def register_answers(answers):
|
| 134 |
+
experiment_dir = config['results']['exp1_dir']
|
| 135 |
+
save_results(user_id.value, experiment_dir, answers)
|
| 136 |
+
|
| 137 |
+
def add_answer(dropdown1,dropdown2,dropdown3,dropdown4, answers):
|
| 138 |
+
rank = [dropdown1,dropdown2,dropdown3,dropdown4]
|
| 139 |
+
answers.append(rank)
|
| 140 |
+
return answers
|
| 141 |
+
|
| 142 |
submit_button.click(
|
| 143 |
update_state,
|
| 144 |
inputs=user_state,
|
| 145 |
outputs=user_state
|
| 146 |
+
).then(
|
| 147 |
+
add_answer,
|
| 148 |
+
inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],
|
| 149 |
+
outputs=answers
|
| 150 |
).then(
|
| 151 |
update_img_label,
|
| 152 |
inputs=user_state,
|
|
|
|
| 164 |
inputs={dropdown1, dropdown2, dropdown3, dropdown4},
|
| 165 |
outputs={dropdown1, dropdown2, dropdown3, dropdown4}
|
| 166 |
)
|
| 167 |
+
|
| 168 |
+
finish_button.click(
|
| 169 |
+
add_answer, inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],outputs=answers
|
| 170 |
+
).then(
|
| 171 |
+
register_answers, inputs=answers
|
| 172 |
+
).then(
|
| 173 |
+
redirect, js="window.location = 'https://marcoparola.github.io/saliency-evaluation-app/end'")
|
|
|
|
|
|
|
| 174 |
|
| 175 |
demo.load(init, inputs=None, outputs=user_id)
|
| 176 |
|
config/config.yaml
CHANGED
|
@@ -1,24 +1,26 @@
|
|
| 1 |
data_dir: data
|
| 2 |
image_dir: images
|
| 3 |
saliency_dir: saliency
|
|
|
|
| 4 |
|
| 5 |
gui:
|
| 6 |
max_img_examples: 16
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
dataset:
|
| 11 |
name: intel_image
|
| 12 |
path: data
|
| 13 |
intel_image:
|
| 14 |
-
n_classes:
|
| 15 |
class_names: ['BUILDING', 'FOREST', 'GLACIER', 'MOUNTAIN', 'SEA', 'STREET']
|
| 16 |
imagenette:
|
| 17 |
n_classes: 10
|
| 18 |
class_names: ['tench', 'English springer', 'cassette player', 'chain saw', 'church', 'French horn', 'garbage truck', 'gas pump', 'golf ball', 'parachute']
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
saliency_methods:
|
| 23 |
- gradcam
|
| 24 |
- lime
|
|
|
|
| 1 |
data_dir: data
|
| 2 |
image_dir: images
|
| 3 |
saliency_dir: saliency
|
| 4 |
+
repo_id: "MarcoParola/saliency-evaluation"
|
| 5 |
|
| 6 |
gui:
|
| 7 |
max_img_examples: 16
|
| 8 |
|
| 9 |
+
results:
|
| 10 |
+
save_dir: results
|
| 11 |
+
exp1_dir: exp1
|
| 12 |
+
exp2_dir: exp2
|
| 13 |
|
| 14 |
dataset:
|
| 15 |
name: intel_image
|
| 16 |
path: data
|
| 17 |
intel_image:
|
| 18 |
+
n_classes: 2
|
| 19 |
class_names: ['BUILDING', 'FOREST', 'GLACIER', 'MOUNTAIN', 'SEA', 'STREET']
|
| 20 |
imagenette:
|
| 21 |
n_classes: 10
|
| 22 |
class_names: ['tench', 'English springer', 'cassette player', 'chain saw', 'church', 'French horn', 'garbage truck', 'gas pump', 'golf ball', 'parachute']
|
| 23 |
|
|
|
|
|
|
|
| 24 |
saliency_methods:
|
| 25 |
- gradcam
|
| 26 |
- lime
|
src/utils.py
CHANGED
|
@@ -5,8 +5,7 @@ import yaml
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
config = yaml.safe_load(open("./config/config.yaml"))
|
| 8 |
-
|
| 9 |
-
|
| 10 |
def load_image_and_saliency(class_idx, data_dir):
|
| 11 |
path = os.path.join(data_dir, 'images', str(class_idx))
|
| 12 |
images = os.listdir(path)
|
|
@@ -34,8 +33,17 @@ def load_words(idx):
|
|
| 34 |
return words
|
| 35 |
|
| 36 |
# Function to save results and increment global variable
|
| 37 |
-
def save_results(
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
filename = "results.txt"
|
| 40 |
print('ooooooo', global_counter)
|
| 41 |
print(dropdowns)
|
|
@@ -48,6 +56,7 @@ def save_results(dropdowns):
|
|
| 48 |
str_dropdowns = "\n".join([str(r) for r in dropdowns])
|
| 49 |
with open(filename, 'w') as f:
|
| 50 |
f.write(str_dropdowns)
|
|
|
|
| 51 |
|
| 52 |
# Upload the file to Hugging Face Hub
|
| 53 |
api = HfApi()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
config = yaml.safe_load(open("./config/config.yaml"))
|
| 8 |
+
|
|
|
|
| 9 |
def load_image_and_saliency(class_idx, data_dir):
|
| 10 |
path = os.path.join(data_dir, 'images', str(class_idx))
|
| 11 |
images = os.listdir(path)
|
|
|
|
| 33 |
return words
|
| 34 |
|
| 35 |
# Function to save results and increment global variable
|
| 36 |
+
def save_results(user_it, experiment_dir, answers):
|
| 37 |
+
folder = os.path.join(config['results']['save_dir'], experiment_dir, str(user_it))
|
| 38 |
+
|
| 39 |
+
# convert answers (list of list) to a pandas dataframe
|
| 40 |
+
df = pd.DataFrame(answers, columns=config['saliency_methods'])
|
| 41 |
+
if not os.path.exists(folder):
|
| 42 |
+
os.makedirs(folder)
|
| 43 |
+
df.to_csv(os.path.join(folder, 'results.csv'), index=False)
|
| 44 |
+
print(f"Results saved to {folder}", df)
|
| 45 |
+
|
| 46 |
+
'''
|
| 47 |
filename = "results.txt"
|
| 48 |
print('ooooooo', global_counter)
|
| 49 |
print(dropdowns)
|
|
|
|
| 56 |
str_dropdowns = "\n".join([str(r) for r in dropdowns])
|
| 57 |
with open(filename, 'w') as f:
|
| 58 |
f.write(str_dropdowns)
|
| 59 |
+
'''
|
| 60 |
|
| 61 |
# Upload the file to Hugging Face Hub
|
| 62 |
api = HfApi()
|