MarcoParola commited on
Commit
2c80634
·
1 Parent(s): 21088a7

develop a preliminary version of experiment1

Browse files
Files changed (5) hide show
  1. .gitignore +24 -0
  2. app.py +175 -30
  3. src/__pycache__/utils.cpython-310.pyc +0 -0
  4. src/style.py +20 -0
  5. src/utils.py +30 -7
.gitignore ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /env
2
+ __pycache__/
3
+
4
+ /logs
5
+ /outputs
6
+ /.hydra
7
+ /checkpoints
8
+ /wandb
9
+ /models
10
+ /share
11
+ /bin
12
+ /lib
13
+ /lib64
14
+ /include
15
+ pyvenv.cfg
16
+ requirements.txt
17
+
18
+ *.log
19
+ *.pth
20
+ *.png
21
+
22
+
23
+ /lightning_logs
24
+ __pycache__/
app.py CHANGED
@@ -1,46 +1,191 @@
1
  import gradio as gr
2
- import pandas as pd
3
- import csv
4
- import os
5
- from huggingface_hub import HfApi, HfFolder
6
  import yaml
7
- from src.utils import load_images, load_words, save_results, load_global_variable
 
 
8
 
9
- # set css style for gallery, specifically for the width of the container
10
- css = """
11
- #gallery {
12
- height: 300px;
13
- }
14
- """
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
 
 
 
 
 
 
18
  def main():
19
-
20
  config = yaml.safe_load(open("config/config.yaml"))
21
- print(config)
22
-
 
 
 
 
 
 
23
  with gr.Blocks(theme=gr.themes.Glass(), css=css) as demo:
24
- title = gr.Markdown(f"# My Gradio App Title")
25
- welcome_message = gr.Markdown("# Welcome to the Gradio App!")
26
-
27
- # Load global variable, images, and words after confirming alert
28
- global_var = load_global_variable()
29
- images = load_images(global_var)
30
- words = load_words(global_var)
31
-
32
- image_gallery = gr.Gallery(images, columns=10, elem_classes="gallery-container", elem_id="gallery")
33
- checkbox_group = gr.CheckboxGroup(words, label="Select 3 words")
34
-
35
- result_message = gr.Textbox(label="Result")
 
 
 
 
 
 
 
 
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  submit_button = gr.Button("Submit")
38
- submit_button.click(save_results, inputs=checkbox_group, outputs=result_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- demo.load(lambda: None, inputs=None, outputs=welcome_message)
 
 
 
 
41
 
42
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
 
44
 
45
  if __name__ == "__main__":
46
- main()
 
1
  import gradio as gr
 
 
 
 
2
  import yaml
3
+ from src.utils import load_words, save_results, load_global_variable, load_saliencies
4
+ from src.style import css
5
+ import random
6
 
7
+ random_images = [
8
+ "https://picsum.photos/200",
9
+ "https://picsum.photos/201",
10
+ "https://picsum.photos/202",
11
+ "https://picsum.photos/203",
12
+ "https://picsum.photos/204",
13
+ "https://picsum.photos/205",
14
+ "https://picsum.photos/206",
15
+ "https://picsum.photos/207",
16
+ "https://picsum.photos/208",
17
+ "https://picsum.photos/209",
18
+ "https://picsum.photos/210",
19
+ "https://picsum.photos/211",
20
+ "https://picsum.photos/212",
21
+ "https://picsum.photos/213",
22
+ "https://picsum.photos/214",
23
+ ]
24
 
25
 
26
 
27
+ def update_img_count(state):
28
+ count = state
29
+ print('oooooooo', count)
30
+ return gr.State(count + 1)
31
+
32
  def main():
 
33
  config = yaml.safe_load(open("config/config.yaml"))
34
+
35
+ global_var = load_global_variable()
36
+
37
+ #images = load_images(global_var)
38
+ #saliency = load_saliencies(global_var)
39
+ words = ['grad-cam', 'lime', 'sidu', 'rise']
40
+ options = ['1', '2', '3', '4']
41
+
42
  with gr.Blocks(theme=gr.themes.Glass(), css=css) as demo:
43
+ # Main App Components
44
+ title = gr.Markdown("# Saliency evaluation - experiment 1")
45
+ user_state = gr.State(0)
46
+ print('user_state', user_state)
47
+ #user_counter = gr.Textbox(str(global_var), visible=False)
48
+ #img_counter = gr.Textbox(str(0), visible=False)
49
+
50
+ with gr.Row():
51
+ gr.Markdown("### Target image")
52
+ gr.Markdown("### Grad-cam")
53
+ gr.Markdown("### Lime")
54
+ gr.Markdown("### Sidu")
55
+ gr.Markdown("### Rise")
56
+
57
+ with gr.Row():
58
+ # generate random integer value
59
+ target_img = gr.Image(random_images[random.randint(0, 5)])
60
+ saliency_gradcam = gr.Image(random_images[random.randint(0, 5)])
61
+ saliency_lime = gr.Image(random_images[random.randint(0, 5)])
62
+ saliency_rise = gr.Image(random_images[random.randint(0, 5)])
63
+ saliency_sidu = gr.Image(random_images[random.randint(0, 5)])
64
 
65
+ with gr.Row():
66
+ dropdown1 = gr.Dropdown(choices=options, label="grad-cam")
67
+ dropdown2 = gr.Dropdown(choices=options, label="lime")
68
+ dropdown3 = gr.Dropdown(choices=options, label="sidu")
69
+ dropdown4 = gr.Dropdown(choices=options, label="rise")
70
+
71
+ gr.Markdown("### Image examples of the same class")
72
+ with gr.Row():
73
+ # generate random integer value
74
+ img1 = gr.Image(random_images[random.randint(0, 5)])
75
+ img2 = gr.Image(random_images[random.randint(0, 5)])
76
+ img3 = gr.Image(random_images[random.randint(0, 5)])
77
+ img4 = gr.Image(random_images[random.randint(0, 5)])
78
+ img5 = gr.Image(random_images[random.randint(0, 5)])
79
+ img6 = gr.Image(random_images[random.randint(0, 5)])
80
+ img7 = gr.Image(random_images[random.randint(0, 5)])
81
+ img8 = gr.Image(random_images[random.randint(0, 5)])
82
+ img9 = gr.Image(random_images[random.randint(0, 5)])
83
+ img10 = gr.Image(random_images[random.randint(0, 5)])
84
+ img11 = gr.Image(random_images[random.randint(0, 5)])
85
+ img12 = gr.Image(random_images[random.randint(0, 5)])
86
+ img13 = gr.Image(random_images[random.randint(0, 5)])
87
+ img14 = gr.Image(random_images[random.randint(0, 5)])
88
+ img15 = gr.Image(random_images[random.randint(0, 5)])
89
+ img16 = gr.Image(random_images[random.randint(0, 5)])
90
+ img17 = gr.Image(random_images[random.randint(0, 5)])
91
+ img18 = gr.Image(random_images[random.randint(0, 5)])
92
+
93
+
94
  submit_button = gr.Button("Submit")
95
+ finish_button = gr.Button("Finish", visible=False)
96
+
97
+ def update_images(dropdown1, dropdown2, dropdown3, dropdown4, user_state):
98
+
99
+ #print('dropdowns', dropdowns)
100
+ #str_dropdowns = str(dropdowns)
101
+ # remove the curly braces
102
+ #dropdowns = str_dropdowns[1:-1]
103
+ #dropdowns = [r.split(":")[1].strip().replace("'", "") for r in dropdowns.split(",")]
104
+
105
+ print('dropdowns', dropdown1, dropdown2, dropdown3, dropdown4)
106
+
107
+ rank = [dropdown1,dropdown2,dropdown3,dropdown4]
108
+ print('rank', rank)
109
+ # image target and saliency images
110
+ target_img = gr.Image(random_images[random.randint(0, 5)])
111
+ saliency_gradcam = gr.Image(random_images[random.randint(0, 5)])
112
+ saliency_lime = gr.Image(random_images[random.randint(0, 5)])
113
+ saliency_rise = gr.Image(random_images[random.randint(0, 5)])
114
+ saliency_sidu = gr.Image(random_images[random.randint(0, 5)])
115
+
116
+ # image examples
117
+ img1 = gr.Image(random_images[random.randint(0, 5)])
118
+ img2 = gr.Image(random_images[random.randint(0, 5)])
119
+ img3 = gr.Image(random_images[random.randint(0, 5)])
120
+ img4 = gr.Image(random_images[random.randint(0, 5)])
121
+ img5 = gr.Image(random_images[random.randint(0, 5)])
122
+ img6 = gr.Image(random_images[random.randint(0, 5)])
123
+ img7 = gr.Image(random_images[random.randint(0, 5)])
124
+ img8 = gr.Image(random_images[random.randint(0, 5)])
125
+ img9 = gr.Image(random_images[random.randint(0, 5)])
126
+ img10 = gr.Image(random_images[random.randint(0, 5)])
127
+ img11 = gr.Image(random_images[random.randint(0, 5)])
128
+ img12 = gr.Image(random_images[random.randint(0, 5)])
129
+ img13 = gr.Image(random_images[random.randint(0, 5)])
130
+ img14 = gr.Image(random_images[random.randint(0, 5)])
131
+ img15 = gr.Image(random_images[random.randint(0, 5)])
132
+ img16 = gr.Image(random_images[random.randint(0, 5)])
133
+ img17 = gr.Image(random_images[random.randint(0, 5)])
134
+ img18 = gr.Image(random_images[random.randint(0, 5)])
135
+
136
+
137
+ if not isinstance(user_state, int):
138
+ if user_state.value == 5:
139
+ finish_button.visible = True
140
+ submit_button.visible = False
141
+ else:
142
+ finish_button.visible = False
143
+ submit_button.visible = True
144
+
145
+
146
+ return target_img, saliency_gradcam, saliency_lime, saliency_rise, saliency_sidu, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18
147
 
148
+ def update_state(state):
149
+
150
+ count = state if isinstance(state, int) else state.value
151
+ print('\n\ncount', count)
152
+ return gr.State(count + 1)
153
 
154
+ def update_buttons(state):
155
+ count = state if isinstance(state, int) else state.value
156
+ finish_button, submit_button = None, None
157
+ if count == 5:
158
+ finish_button = gr.Button("Finish", visible=True)
159
+ submit_button = gr.Button("Submit", visible=False)
160
+ else:
161
+ finish_button = gr.Button("Finish", visible=False)
162
+ submit_button = gr.Button("Submit", visible=True)
163
+
164
+ return submit_button, finish_button
165
+
166
+
167
+ submit_button.click(
168
+ update_state,
169
+ inputs=user_state,
170
+ outputs=user_state
171
+ ).then(
172
+ update_buttons,
173
+ inputs=user_state,
174
+ outputs={submit_button, finish_button}
175
+ ).then(
176
+ update_images,
177
+ inputs=[dropdown1, dropdown2, dropdown3, dropdown4, user_state],
178
+ outputs={target_img, saliency_gradcam, saliency_lime, saliency_rise, saliency_sidu, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18},
179
+ )
180
+
181
+ def redirect():
182
+ pass
183
+
184
+ finish_button.click(redirect, js="window.location = 'https://marcoparola.github.io/saliency-evaluation-app/end'")
185
+
186
+ demo.load()
187
 
188
+ demo.launch()
189
 
190
  if __name__ == "__main__":
191
+ main()
src/__pycache__/utils.cpython-310.pyc DELETED
Binary file (1.96 kB)
 
src/style.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ css = """
2
+ #gallery {
3
+ height: 300px;
4
+ }
5
+
6
+ .gallery-textlabel > * {
7
+ h2 {
8
+ font-weight: medium;
9
+ text-align: center;
10
+ margin-top: 1px;
11
+ padding: 0px;
12
+ font-size: 1em;
13
+ }
14
+ .svelte-i3tvor {
15
+ display:none;
16
+ visibility: hidden;
17
+ font-size: 0.02em;
18
+ }
19
+ }
20
+ """
src/utils.py CHANGED
@@ -8,11 +8,18 @@ config = yaml.safe_load(open("./config/config.yaml"))
8
 
9
  # Function to load global variable from CSV
10
  def load_global_variable():
 
11
  if os.path.exists('global_variable.csv'):
12
  df = pd.read_csv('global_variable.csv')
13
- return df['value'][0]
14
- else:
15
- return 0
 
 
 
 
 
 
16
 
17
  def load_images(global_var):
18
  image_dir = os.path.join(config["data_dir"], config["image_dir"])
@@ -20,23 +27,39 @@ def load_images(global_var):
20
  images = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
21
  return images
22
 
 
 
 
 
 
 
 
 
23
  # Function to load words based on global variable
24
  def load_words(global_var):
25
  words = [f"word_{global_var}_{i}" for i in range(10)]
26
  return words
27
 
28
  # Function to save results and increment global variable
29
- def save_results(results):
30
 
31
  filename = "results.txt"
32
- results_str = "\n".join(results)
 
 
 
 
 
 
 
 
33
  with open(filename, 'w') as f:
34
- f.write(results_str)
35
 
36
  # Upload the file to Hugging Face Hub
37
  api = HfApi()
38
  token = os.getenv("HUGGINGFACE_TOKEN")
39
- #token = HfFolder.get_token()
40
 
41
  if not token:
42
  print("Token not found. Please login to Hugging Face.")
 
8
 
9
  # Function to load global variable from CSV
10
  def load_global_variable():
11
+ global global_counter
12
  if os.path.exists('global_variable.csv'):
13
  df = pd.read_csv('global_variable.csv')
14
+ global_counter = df['value'][0]
15
+
16
+ print('global_counter', global_counter)
17
+
18
+ global_counter += 1
19
+ df = pd.DataFrame({'value': [global_counter]})
20
+ df.to_csv('global_variable.csv', index=False)
21
+ return global_counter
22
+
23
 
24
  def load_images(global_var):
25
  image_dir = os.path.join(config["data_dir"], config["image_dir"])
 
27
  images = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
28
  return images
29
 
30
+ def load_saliencies(global_var):
31
+ image_dir = os.path.join(config["data_dir"], config["image_dir"])
32
+ #images = [f"image_{global_var}_{i}.jpg" for i in range(10)]
33
+ saliencies = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
34
+ # select first 5 saliencies
35
+ saliencies = saliencies[:5]
36
+ return saliencies
37
+
38
  # Function to load words based on global variable
39
  def load_words(global_var):
40
  words = [f"word_{global_var}_{i}" for i in range(10)]
41
  return words
42
 
43
  # Function to save results and increment global variable
44
+ def save_results(dropdowns):
45
 
46
  filename = "results.txt"
47
+ print('ooooooo', global_counter)
48
+ print(dropdowns)
49
+ str_dropdowns = str(dropdowns)
50
+ # remove the curly braces
51
+ dropdowns = str_dropdowns[1:-1]
52
+ # split by comma and select the number contained in the string
53
+ dropdowns = [r.split(":")[1].strip().replace("'", "") for r in dropdowns.split(",")]
54
+
55
+ str_dropdowns = "\n".join([str(r) for r in dropdowns])
56
  with open(filename, 'w') as f:
57
+ f.write(str_dropdowns)
58
 
59
  # Upload the file to Hugging Face Hub
60
  api = HfApi()
61
  token = os.getenv("HUGGINGFACE_TOKEN")
62
+ # token = HfFolder.get_token()
63
 
64
  if not token:
65
  print("Token not found. Please login to Hugging Face.")