MarcoParola commited on
Commit
cf46cef
·
1 Parent(s): 2f8f934

add random component that randomly shows concepts

Browse files
Files changed (3) hide show
  1. app.py +59 -9
  2. data/intel_image/concepts_by_class.csv +7 -0
  3. src/utils.py +16 -0
app.py CHANGED
@@ -4,10 +4,11 @@ import random
4
  import os
5
  import json
6
  import time
 
7
  from pathlib import Path
8
  from huggingface_hub import CommitScheduler, HfApi
9
 
10
- from src.utils import load_words, load_example_images
11
  from src.style import css
12
  from src.user import UserID
13
 
@@ -27,13 +28,17 @@ def main():
27
  title = gr.Markdown("# Saliency evaluation - experiment 2")
28
  user_state = gr.State(0)
29
  answers = gr.State([])
 
30
 
31
  target_img_label = gr.Markdown(f"Target class: **{class_names[user_state.value]}**")
32
  question = gr.Markdown()
33
 
 
 
34
  concept_checkboxes = gr.CheckboxGroup(
35
  ['c1, c2, c3', 'c4, c5, c6', 'c7, c8, c9'],
36
  label=f"Choose the concept set that better describes the target class",
 
37
  )
38
 
39
  gr.Markdown("### Image examples of the same class")
@@ -57,7 +62,8 @@ def main():
57
  img15 = gr.Image(images[14])
58
  img16 = gr.Image(images[15])
59
 
60
- submit_button = gr.Button("Submit")
 
61
  finish_button = gr.Button("Finish", visible=False)
62
 
63
  def update_label(concept_checkboxes, user_state):
@@ -99,14 +105,45 @@ def main():
99
  count = state if isinstance(state, int) else state.value
100
  max_images = config['dataset'][config['dataset']['name']]['n_classes']
101
  finish_button = gr.Button("Finish", visible=(count == max_images-1))
102
- submit_button = gr.Button("Submit", visible=(count != max_images-1))
103
- return submit_button, finish_button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- def update_checkbox():
106
  concept_checkboxes = gr.CheckboxGroup(
107
- choices = ['c1, c2, c3','c4, c5, c6','c7, c8, c9'],
108
  label=f"Choose the concept set that better describes the target class",
109
- value=None)
 
 
110
  return concept_checkboxes
111
 
112
  def redirect():
@@ -154,7 +191,7 @@ def main():
154
 
155
  def add_answer(concept_checkboxes, answers):
156
  answers.append(concept_checkboxes)
157
- print('ANSWERS:', answers)
158
  return answers
159
 
160
  submit_button.click(
@@ -175,13 +212,26 @@ def main():
175
  ).then(
176
  update_buttons,
177
  inputs=user_state,
178
- outputs={submit_button, finish_button}
 
 
 
179
  ).then(
180
  update_label,
181
  inputs=[concept_checkboxes, user_state],
182
  outputs={img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16},
 
 
 
 
 
 
 
 
 
183
  ).then(
184
  update_checkbox,
 
185
  outputs=concept_checkboxes
186
  )
187
 
 
4
  import os
5
  import json
6
  import time
7
+ import numpy as np
8
  from pathlib import Path
9
  from huggingface_hub import CommitScheduler, HfApi
10
 
11
+ from src.utils import load_words, load_example_images, load_csv_concepts, generate_random_ids
12
  from src.style import css
13
  from src.user import UserID
14
 
 
28
  title = gr.Markdown("# Saliency evaluation - experiment 2")
29
  user_state = gr.State(0)
30
  answers = gr.State([])
31
+ random_answer_order = gr.State([])
32
 
33
  target_img_label = gr.Markdown(f"Target class: **{class_names[user_state.value]}**")
34
  question = gr.Markdown()
35
 
36
+ concepts = load_csv_concepts(data_dir)
37
+
38
  concept_checkboxes = gr.CheckboxGroup(
39
  ['c1, c2, c3', 'c4, c5, c6', 'c7, c8, c9'],
40
  label=f"Choose the concept set that better describes the target class",
41
+ visible=False
42
  )
43
 
44
  gr.Markdown("### Image examples of the same class")
 
62
  img15 = gr.Image(images[14])
63
  img16 = gr.Image(images[15])
64
 
65
+ continue_button = gr.Button("Continue")
66
+ submit_button = gr.Button("Submit", visible=False)
67
  finish_button = gr.Button("Finish", visible=False)
68
 
69
  def update_label(concept_checkboxes, user_state):
 
105
  count = state if isinstance(state, int) else state.value
106
  max_images = config['dataset'][config['dataset']['name']]['n_classes']
107
  finish_button = gr.Button("Finish", visible=(count == max_images-1))
108
+ submit_button = gr.Button("Submit", visible=False)
109
+ continue_button = gr.Button("Continue", visible=(count != max_images-1))
110
+ return continue_button, submit_button, finish_button
111
+
112
+ def update_continue_button():
113
+ continue_button = gr.Button("Continue", visible=False)
114
+ submit_button = gr.Button("Submit", visible=True)
115
+ return continue_button, submit_button
116
+
117
+
118
+ def update_checkbox(user_state):
119
+ count = user_state if isinstance(user_state, int) else user_state.value
120
+ # get row count from csv
121
+ row = concepts.iloc[count]
122
+ keys = concepts.keys()
123
+ random_ids = generate_random_ids()
124
+ print('random_ids:', random_ids)
125
+ # generate a random order for the random_id sets between 0 and 2
126
+ random_order = np.random.permutation(3)
127
+ print('random_order:', random_order)
128
+ concept_checkboxes = gr.CheckboxGroup(
129
+ choices = [
130
+ (f'{row[keys[random_ids[random_order[0]][0]]]}, {row[keys[random_ids[random_order[0]][1]]]}, {row[keys[random_ids[random_order[0]][2]]]}', int(random_order[0])),
131
+ (f'{row[keys[random_ids[random_order[1]][0]]]}, {row[keys[random_ids[random_order[1]][1]]]}, {row[keys[random_ids[random_order[1]][2]]]}', int(random_order[1])),
132
+ (f'{row[keys[random_ids[random_order[2]][0]]]}, {row[keys[random_ids[random_order[2]][1]]]}, {row[keys[random_ids[random_order[2]][2]]]}', int(random_order[2]))
133
+ ],
134
+ label=f"Choose the concept set that better describes the target class",
135
+ value=None,
136
+ visible=True
137
+ )
138
+ return concept_checkboxes
139
 
140
+ def hide_checkbox():
141
  concept_checkboxes = gr.CheckboxGroup(
142
+ choices = ['c10, c2, c3','c4, c5, c6','c7, c8, c9'],
143
  label=f"Choose the concept set that better describes the target class",
144
+ value=None,
145
+ visible=False
146
+ )
147
  return concept_checkboxes
148
 
149
  def redirect():
 
191
 
192
  def add_answer(concept_checkboxes, answers):
193
  answers.append(concept_checkboxes)
194
+ print('ANSWERS:', answers, concept_checkboxes)
195
  return answers
196
 
197
  submit_button.click(
 
212
  ).then(
213
  update_buttons,
214
  inputs=user_state,
215
+ outputs={continue_button, submit_button, finish_button}
216
+ ).then(
217
+ hide_checkbox,
218
+ outputs=concept_checkboxes
219
  ).then(
220
  update_label,
221
  inputs=[concept_checkboxes, user_state],
222
  outputs={img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16},
223
+ )
224
+ #.then(
225
+ # update_checkbox,
226
+ # outputs=concept_checkboxes
227
+ #)
228
+
229
+ continue_button.click(
230
+ update_continue_button,
231
+ outputs={continue_button, submit_button}
232
  ).then(
233
  update_checkbox,
234
+ inputs=user_state,
235
  outputs=concept_checkboxes
236
  )
237
 
data/intel_image/concepts_by_class.csv ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ class, concept1, concept2, concept3, concept4, concept5, concept6, concept7, concept8, concept9, concept10, concept11, concept12, concept13, concept14, concept15, concept16
2
+ buildings, Roof, Window, Facade, Wall, Boat, Tree, Sky, Car, Streetlights, Sidewalk, Beach, Vegetation, Water, Mountain Peak, Rock, Ice
3
+ forest, Vegetation, Tree, Beach, Sidewalk, Facade, Sky, Water, Wall, Rock, Window, Ice, Roof, Streetlights, Car, Mountain Peak, Boat
4
+ glacier, Ice, Rock, Mountain Peak, Water, Wall, Beach, Sky, Vegetation, Sidewalk, Facade, Roof, Tree, Window, Boat, Streetlights, Car
5
+ mountain, Mountain Peak, Rock, Vegetation, Sky, Tree, Ice, Water, Beach, Wall, Facade, Roof, Boat, Sidewalk, Window, Streetlights, Car
6
+ sea, Water, Boat, Beach, Sky, Rock, Sidewalk, Wall, Ice, Roof, Vegetation, Facade, Mountain Peak, Tree, Streetlights, Window, Car
7
+ street, Car, Streetlights, Sidewalk, Boat, Wall, Facade, Tree, Roof, Beach, Sky, Window, Vegetation, Water, Rock, Mountain Peak, Ice
src/utils.py CHANGED
@@ -20,6 +20,22 @@ def load_words(idx):
20
  words = [f"word_{idx}_{i}" for i in range(20)]
21
  return words
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
 
25
 
 
20
  words = [f"word_{idx}_{i}" for i in range(20)]
21
  return words
22
 
23
+ def load_csv_concepts(data_dir):
24
+ # Load data from csv
25
+ data = pd.read_csv(os.path.join(data_dir, 'concepts_by_class.csv'))
26
+ return data
27
+
28
+ def generate_random_ids():
29
+ # generate three sets of random ids (three among 1-4, 1-8, 1-12)
30
+ # get time as seed
31
+ seed = int(time.time())
32
+ np.random.seed(seed)
33
+ ids1 = np.random.choice(np.arange(1, 5), 3, replace=False)
34
+ ids2 = np.random.choice(np.arange(2, 10), 3, replace=False)
35
+ ids3 = np.random.choice(np.arange(3, 15), 3, replace=False)
36
+
37
+ return [ids1, ids2, ids3]
38
+
39
 
40
 
41