Commit
·
f040850
1
Parent(s):
05a88d5
save image id storing them into an array
Browse files- app.py +20 -7
- src/utils.py +8 -6
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import time
|
|
| 7 |
from pathlib import Path
|
| 8 |
from huggingface_hub import CommitScheduler, HfApi
|
| 9 |
|
| 10 |
-
from src.utils import load_words, load_image_and_saliency, load_example_images, load_csv_concepts
|
| 11 |
from src.style import css
|
| 12 |
from src.user import UserID
|
| 13 |
|
|
@@ -68,8 +68,9 @@ def main():
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 71 |
-
|
| 72 |
-
img_ids
|
|
|
|
| 73 |
target_img = gr.Image(images[1], elem_classes="main-image delay", visible=False)
|
| 74 |
saliency_gradcam = gr.Image(images[2], elem_classes="main-image", visible=False)
|
| 75 |
saliency_lime = gr.Image(images[3], elem_classes="main-image", visible=False)
|
|
@@ -117,16 +118,16 @@ def main():
|
|
| 117 |
def update_saliencies(dropdown1, dropdown2, dropdown3, dropdown4, user_state, img_ids):
|
| 118 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 119 |
if count < config['dataset'][config['dataset']['name']]['n_classes']:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
target_img = gr.Image(images[1], elem_classes="main-image", visible=True)
|
| 123 |
saliency_gradcam = gr.Image(images[2], elem_classes="main-image", visible=True)
|
| 124 |
saliency_lime = gr.Image(images[3], elem_classes="main-image", visible=True)
|
| 125 |
saliency_sidu = gr.Image(images[5], elem_classes="main-image", visible=True)
|
| 126 |
saliency_rise = gr.Image(images[4], elem_classes="main-image", visible=True)
|
| 127 |
-
return
|
| 128 |
else:
|
| 129 |
-
return
|
| 130 |
|
| 131 |
def update_state(state):
|
| 132 |
count = state if isinstance(state, int) else state.value
|
|
@@ -136,6 +137,12 @@ def main():
|
|
| 136 |
count = state if isinstance(state, int) else state.value
|
| 137 |
return f" Target image: **{class_names[count]}**"
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
def update_buttons():
|
| 140 |
submit_button = gr.Button("Submit", visible=False)
|
| 141 |
continue_button = gr.Button("Continue", visible=True)
|
|
@@ -202,6 +209,7 @@ def main():
|
|
| 202 |
info_to_push = {
|
| 203 |
"user_id": time.time(),
|
| 204 |
"answer": {i: answer for i, answer in enumerate(answers)},
|
|
|
|
| 205 |
"duration": duration
|
| 206 |
}
|
| 207 |
|
|
@@ -212,6 +220,7 @@ def main():
|
|
| 212 |
"user_id": info_to_push["user_id"],
|
| 213 |
"answers": info_to_push["answer"],
|
| 214 |
"duration": info_to_push["duration"],
|
|
|
|
| 215 |
"datetime": datetime.now().isoformat()
|
| 216 |
}, f)
|
| 217 |
f.write("\n")
|
|
@@ -241,6 +250,10 @@ def main():
|
|
| 241 |
add_answer,
|
| 242 |
inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],
|
| 243 |
outputs=answers
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
).then(
|
| 245 |
update_img_label,
|
| 246 |
inputs=user_state,
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from huggingface_hub import CommitScheduler, HfApi
|
| 9 |
|
| 10 |
+
from src.utils import load_words, get_random_image_id, load_image_and_saliency, load_example_images, load_csv_concepts
|
| 11 |
from src.style import css
|
| 12 |
from src.user import UserID
|
| 13 |
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 71 |
+
random_img_id = get_random_image_id(count, data_dir)
|
| 72 |
+
img_ids.append(random_img_id)
|
| 73 |
+
images = load_image_and_saliency(count, data_dir, random_img_id)
|
| 74 |
target_img = gr.Image(images[1], elem_classes="main-image delay", visible=False)
|
| 75 |
saliency_gradcam = gr.Image(images[2], elem_classes="main-image", visible=False)
|
| 76 |
saliency_lime = gr.Image(images[3], elem_classes="main-image", visible=False)
|
|
|
|
| 118 |
def update_saliencies(dropdown1, dropdown2, dropdown3, dropdown4, user_state, img_ids):
|
| 119 |
count = user_state if isinstance(user_state, int) else user_state.value
|
| 120 |
if count < config['dataset'][config['dataset']['name']]['n_classes']:
|
| 121 |
+
img_id = img_ids[-1] if isinstance(img_ids[-1], int) else img_ids[-1].value
|
| 122 |
+
images = load_image_and_saliency(count, data_dir, img_id)
|
| 123 |
target_img = gr.Image(images[1], elem_classes="main-image", visible=True)
|
| 124 |
saliency_gradcam = gr.Image(images[2], elem_classes="main-image", visible=True)
|
| 125 |
saliency_lime = gr.Image(images[3], elem_classes="main-image", visible=True)
|
| 126 |
saliency_sidu = gr.Image(images[5], elem_classes="main-image", visible=True)
|
| 127 |
saliency_rise = gr.Image(images[4], elem_classes="main-image", visible=True)
|
| 128 |
+
return target_img, saliency_gradcam, saliency_lime, saliency_rise, saliency_sidu
|
| 129 |
else:
|
| 130 |
+
return target_img, saliency_gradcam, saliency_lime, saliency_rise, saliency_sidu
|
| 131 |
|
| 132 |
def update_state(state):
|
| 133 |
count = state if isinstance(state, int) else state.value
|
|
|
|
| 137 |
count = state if isinstance(state, int) else state.value
|
| 138 |
return f" Target image: **{class_names[count]}**"
|
| 139 |
|
| 140 |
+
def update_random_img_id(img_ids):
|
| 141 |
+
count = user_state if isinstance(user_state, int) else user_state.value
|
| 142 |
+
random_img_id = get_random_image_id(count, data_dir)
|
| 143 |
+
img_ids.append(random_img_id)
|
| 144 |
+
return img_ids
|
| 145 |
+
|
| 146 |
def update_buttons():
|
| 147 |
submit_button = gr.Button("Submit", visible=False)
|
| 148 |
continue_button = gr.Button("Continue", visible=True)
|
|
|
|
| 209 |
info_to_push = {
|
| 210 |
"user_id": time.time(),
|
| 211 |
"answer": {i: answer for i, answer in enumerate(answers)},
|
| 212 |
+
"img_ids": [img_id for img_id in img_ids],
|
| 213 |
"duration": duration
|
| 214 |
}
|
| 215 |
|
|
|
|
| 220 |
"user_id": info_to_push["user_id"],
|
| 221 |
"answers": info_to_push["answer"],
|
| 222 |
"duration": info_to_push["duration"],
|
| 223 |
+
"img_ids": info_to_push["img_ids"],
|
| 224 |
"datetime": datetime.now().isoformat()
|
| 225 |
}, f)
|
| 226 |
f.write("\n")
|
|
|
|
| 250 |
add_answer,
|
| 251 |
inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],
|
| 252 |
outputs=answers
|
| 253 |
+
).then(
|
| 254 |
+
update_random_img_id,
|
| 255 |
+
inputs=img_ids,
|
| 256 |
+
outputs=img_ids
|
| 257 |
).then(
|
| 258 |
update_img_label,
|
| 259 |
inputs=user_state,
|
src/utils.py
CHANGED
|
@@ -7,19 +7,21 @@ import time
|
|
| 7 |
|
| 8 |
config = yaml.safe_load(open("./config/config.yaml"))
|
| 9 |
|
| 10 |
-
def
|
| 11 |
path = os.path.join(data_dir, 'images', str(class_idx))
|
| 12 |
images = os.listdir(path)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
image = os.path.join(path, images[id])
|
| 18 |
gradcam_image = os.path.join(data_dir, 'saliency', 'gradcam', images[id])
|
| 19 |
lime_image = os.path.join(data_dir, 'saliency', 'lime', images[id])
|
| 20 |
sidu_image = os.path.join(data_dir, 'saliency', 'sidu', images[id])
|
| 21 |
rise_image = os.path.join(data_dir, 'saliency', 'rise', images[id])
|
| 22 |
-
return
|
| 23 |
|
| 24 |
def load_example_images(class_idx, data_dir, max_images=16):
|
| 25 |
path = os.path.join(data_dir, 'images', str(class_idx))
|
|
|
|
| 7 |
|
| 8 |
config = yaml.safe_load(open("./config/config.yaml"))
|
| 9 |
|
| 10 |
+
def get_random_image_id(class_idx, data_dir):
|
| 11 |
path = os.path.join(data_dir, 'images', str(class_idx))
|
| 12 |
images = os.listdir(path)
|
| 13 |
+
return np.random.randint(0, len(images))
|
| 14 |
+
|
| 15 |
+
def load_image_and_saliency(class_idx, data_dir, img_id):
|
| 16 |
+
path = os.path.join(data_dir, 'images', str(class_idx))
|
| 17 |
+
images = os.listdir(path)
|
| 18 |
+
id = img_id
|
| 19 |
image = os.path.join(path, images[id])
|
| 20 |
gradcam_image = os.path.join(data_dir, 'saliency', 'gradcam', images[id])
|
| 21 |
lime_image = os.path.join(data_dir, 'saliency', 'lime', images[id])
|
| 22 |
sidu_image = os.path.join(data_dir, 'saliency', 'sidu', images[id])
|
| 23 |
rise_image = os.path.join(data_dir, 'saliency', 'rise', images[id])
|
| 24 |
+
return image, gradcam_image, lime_image, sidu_image, rise_image
|
| 25 |
|
| 26 |
def load_example_images(class_idx, data_dir, max_images=16):
|
| 27 |
path = os.path.join(data_dir, 'images', str(class_idx))
|