Xmaster6y commited on
Commit
813250f
1 Parent(s): d30c849

tie and unseen concepts

Browse files
Files changed (2) hide show
  1. src/label_interface.py +63 -15
  2. src/sample_interface.py +19 -9
src/label_interface.py CHANGED
@@ -10,18 +10,38 @@ import gradio as gr
10
  from src import global_variables
11
  from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def get_next_image(
14
  split: str,
15
  concepts: list,
 
16
  filtered_indices: dict,
17
  selected_concepts: list,
 
18
  profile: gr.OAuthProfile
19
  ):
20
  username = profile.username
21
- if concepts != selected_concepts:
22
  for key, values in global_variables.all_metadata.items():
23
- filtered_indices[key] = [i for i in range(len(values)) if all([values[i]["concepts"].get(c, False) for c in concepts])]
24
  selected_concepts = concepts
 
25
  try:
26
  sample_idx = random.choice(filtered_indices[split])
27
  sample = global_variables.all_metadata[split][sample_idx]
@@ -29,8 +49,11 @@ def get_next_image(
29
  try:
30
  username_votes = sample["votes"][username]
31
  voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
 
32
  except KeyError:
33
  voted_concepts = []
 
 
34
 
35
  return (
36
  image_path,
@@ -38,26 +61,31 @@ def get_next_image(
38
  f"{split}:{sample_idx}",
39
  sample["class"],
40
  sample["concepts"],
 
 
41
  filtered_indices,
42
  selected_concepts,
 
43
  )
44
  except IndexError:
45
  gr.Warning("No image found for the selected filter.")
46
- return None, None, None, None, None, filtered_indices, selected_concepts
47
 
48
  def submit_label(
49
  voted_concepts: list,
50
  current_image: Optional[str],
51
  split,
52
  concepts,
 
53
  filtered_indices,
54
  selected_concepts,
 
55
  profile: gr.OAuthProfile
56
  ):
57
  username = profile.username
58
  if current_image is None:
59
  gr.Warning("No image selected.")
60
- return None, None, None, None, None, filtered_indices, selected_concepts
61
  current_split, idx = current_image.split(":")
62
  idx = int(idx)
63
  global_variables.get_metadata(current_split)
@@ -65,21 +93,23 @@ def submit_label(
65
  global_variables.all_metadata[current_split][idx]["votes"] = {}
66
  global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
67
  vote_sum = {c: 0 for c in CONCEPTS}
68
- concepts = {}
69
  for c in CONCEPTS:
70
  for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
71
  if c not in vote:
72
  continue
73
  vote_sum[c] += 2 * vote[c] - 1
74
- concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
75
- global_variables.all_metadata[current_split][idx]["concepts"] = concepts
76
  global_variables.save_metadata(current_split)
77
  gr.Info("Submit success")
78
  return get_next_image(
79
  split,
80
  concepts,
 
81
  filtered_indices,
82
  selected_concepts,
 
83
  profile
84
  )
85
 
@@ -91,11 +121,17 @@ with gr.Blocks() as interface:
91
  gr.Markdown(
92
  "## # Image Selection",
93
  )
94
- split = gr.Radio(
95
- label="Split",
96
- choices=["train", "validation", "test"],
97
- value="train",
98
- )
 
 
 
 
 
 
99
  concepts = gr.Dropdown(
100
  label="Concepts",
101
  multiselect=True,
@@ -106,6 +142,14 @@ with gr.Blocks() as interface:
106
  label="Voted Concepts",
107
  choices=CONCEPTS,
108
  )
 
 
 
 
 
 
 
 
109
 
110
  with gr.Row():
111
  next_button = gr.Button(
@@ -135,22 +179,26 @@ with gr.Blocks() as interface:
135
  for split in global_variables.all_metadata
136
  })
137
  selected_concepts = gr.State([])
 
138
  common_output = [
139
  image,
140
  voted_concepts,
141
  current_image,
142
  im_class,
143
  im_concepts,
 
 
144
  filtered_indices,
145
- selected_concepts
 
146
  ]
147
  next_button.click(
148
  get_next_image,
149
- inputs=[split, concepts, filtered_indices, selected_concepts],
150
  outputs=common_output
151
  )
152
  submit_button.click(
153
  submit_label,
154
- inputs=[voted_concepts, current_image, split, concepts, filtered_indices, selected_concepts],
155
  outputs=common_output
156
  )
 
10
  from src import global_variables
11
  from src.constants import CONCEPTS, ASSETS_FOLDER, DATASET_NAME
12
 
13
+
14
+ def filter_sample(sample, concepts, username, sample_type):
15
+ has_concepts = all([sample["concepts"].get(c, False) for c in concepts])
16
+ if not has_concepts:
17
+ return False
18
+ if "votes" in sample and username in sample["votes"]:
19
+ is_labelled = all([c in sample["votes"][username] for c in CONCEPTS])
20
+ else:
21
+ is_labelled = False
22
+ if sample_type == "labelled":
23
+ return is_labelled
24
+ elif sample_type == "unlabelled":
25
+ return not is_labelled
26
+ else:
27
+ raise ValueError(f"Invalid sample type: {sample_type}")
28
+
29
+
30
  def get_next_image(
31
  split: str,
32
  concepts: list,
33
+ sample_type: str,
34
  filtered_indices: dict,
35
  selected_concepts: list,
36
+ selected_sample_type: str,
37
  profile: gr.OAuthProfile
38
  ):
39
  username = profile.username
40
+ if concepts != selected_concepts or sample_type != selected_sample_type:
41
  for key, values in global_variables.all_metadata.items():
42
+ filtered_indices[key] = [i for i in range(len(values)) if filter_sample(values[i], concepts, username, sample_type)]
43
  selected_concepts = concepts
44
+ selected_sample_type = sample_type
45
  try:
46
  sample_idx = random.choice(filtered_indices[split])
47
  sample = global_variables.all_metadata[split][sample_idx]
 
49
  try:
50
  username_votes = sample["votes"][username]
51
  voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
52
+ unseen_concepts = [c for c in CONCEPTS if c not in username_votes]
53
  except KeyError:
54
  voted_concepts = []
55
+ unseen_concepts = []
56
+ tie_concepts = [c for c in sample["concepts"] if sample["concepts"][c] is None]
57
 
58
  return (
59
  image_path,
 
61
  f"{split}:{sample_idx}",
62
  sample["class"],
63
  sample["concepts"],
64
+ unseen_concepts,
65
+ tie_concepts,
66
  filtered_indices,
67
  selected_concepts,
68
+ selected_sample_type,
69
  )
70
  except IndexError:
71
  gr.Warning("No image found for the selected filter.")
72
+ return None, None, None, None, None, None, None, None, filtered_indices, selected_concepts, selected_sample_type
73
 
74
  def submit_label(
75
  voted_concepts: list,
76
  current_image: Optional[str],
77
  split,
78
  concepts,
79
+ sample_type,
80
  filtered_indices,
81
  selected_concepts,
82
+ selected_sample_type,
83
  profile: gr.OAuthProfile
84
  ):
85
  username = profile.username
86
  if current_image is None:
87
  gr.Warning("No image selected.")
88
+ return None, None, None, None, None, None, None, None, filtered_indices, selected_concepts, selected_sample_type
89
  current_split, idx = current_image.split(":")
90
  idx = int(idx)
91
  global_variables.get_metadata(current_split)
 
93
  global_variables.all_metadata[current_split][idx]["votes"] = {}
94
  global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
95
  vote_sum = {c: 0 for c in CONCEPTS}
96
+ new_concepts = {}
97
  for c in CONCEPTS:
98
  for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
99
  if c not in vote:
100
  continue
101
  vote_sum[c] += 2 * vote[c] - 1
102
+ new_concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
103
+ global_variables.all_metadata[current_split][idx]["concepts"] = new_concepts
104
  global_variables.save_metadata(current_split)
105
  gr.Info("Submit success")
106
  return get_next_image(
107
  split,
108
  concepts,
109
+ sample_type,
110
  filtered_indices,
111
  selected_concepts,
112
+ selected_sample_type,
113
  profile
114
  )
115
 
 
121
  gr.Markdown(
122
  "## # Image Selection",
123
  )
124
+ with gr.Row():
125
+ split = gr.Radio(
126
+ label="Split",
127
+ choices=["train", "validation", "test"],
128
+ value="train",
129
+ )
130
+ sample_type = gr.Radio(
131
+ label="Sample Type",
132
+ choices=["labelled", "unlabelled"],
133
+ value="unlabelled",
134
+ )
135
  concepts = gr.Dropdown(
136
  label="Concepts",
137
  multiselect=True,
 
142
  label="Voted Concepts",
143
  choices=CONCEPTS,
144
  )
145
+ unseen_concepts = gr.CheckboxGroup(
146
+ label="Previously Unseen Concepts",
147
+ choices=CONCEPTS,
148
+ )
149
+ tie_concepts = gr.CheckboxGroup(
150
+ label="Tie Concepts",
151
+ choices=CONCEPTS,
152
+ )
153
 
154
  with gr.Row():
155
  next_button = gr.Button(
 
179
  for split in global_variables.all_metadata
180
  })
181
  selected_concepts = gr.State([])
182
+ selected_sample_type = gr.State(None)
183
  common_output = [
184
  image,
185
  voted_concepts,
186
  current_image,
187
  im_class,
188
  im_concepts,
189
+ unseen_concepts,
190
+ tie_concepts,
191
  filtered_indices,
192
+ selected_concepts,
193
+ selected_sample_type,
194
  ]
195
  next_button.click(
196
  get_next_image,
197
+ inputs=[split, concepts, sample_type, filtered_indices, selected_concepts, selected_sample_type],
198
  outputs=common_output
199
  )
200
  submit_button.click(
201
  submit_label,
202
+ inputs=[voted_concepts, current_image, split, concepts, sample_type, filtered_indices, selected_concepts, selected_sample_type],
203
  outputs=common_output
204
  )
src/sample_interface.py CHANGED
@@ -33,8 +33,11 @@ def get_image(
33
  try:
34
  username_votes = sample["votes"][username]
35
  voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
 
36
  except KeyError:
37
  voted_concepts = []
 
 
38
 
39
  return (
40
  image_path,
@@ -43,6 +46,8 @@ def get_image(
43
  sample["class"],
44
  sample["concepts"],
45
  str(sample_idx),
 
 
46
  filtered_indices,
47
  )
48
 
@@ -71,7 +76,7 @@ def submit_label(
71
  username = profile.username
72
  if current_image is None:
73
  gr.Warning("No image selected.")
74
- return None, None, None, None, None, index, filtered_indices
75
  current_split, idx = current_image.split(":")
76
  idx = int(idx)
77
  global_variables.get_metadata(current_split)
@@ -79,14 +84,14 @@ def submit_label(
79
  global_variables.all_metadata[current_split][idx]["votes"] = {}
80
  global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
81
  vote_sum = {c: 0 for c in CONCEPTS}
82
- concepts = {}
83
  for c in CONCEPTS:
84
  for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
85
  if c not in vote:
86
  continue
87
  vote_sum[c] += 2 * vote[c] - 1
88
- concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
89
- global_variables.all_metadata[current_split][idx]["concepts"] = concepts
90
  global_variables.save_metadata(current_split)
91
  gr.Info("Submit success")
92
  return get_next_image(
@@ -119,6 +124,14 @@ with gr.Blocks() as interface:
119
  label="Voted Concepts",
120
  choices=CONCEPTS,
121
  )
 
 
 
 
 
 
 
 
122
 
123
  with gr.Row():
124
  prev_button = gr.Button(
@@ -157,6 +170,8 @@ with gr.Blocks() as interface:
157
  im_class,
158
  im_concepts,
159
  index,
 
 
160
  filtered_indices,
161
  ]
162
  common_input = [split, index, filtered_indices]
@@ -180,8 +195,3 @@ with gr.Blocks() as interface:
180
  inputs=common_input,
181
  outputs=common_output,
182
  )
183
- interface.load(
184
- get_current_image,
185
- inputs=common_input,
186
- outputs=common_output,
187
- )
 
33
  try:
34
  username_votes = sample["votes"][username]
35
  voted_concepts = [c for c in CONCEPTS if username_votes.get(c, False)]
36
+ unseen_concepts = [c for c in CONCEPTS if c not in username_votes]
37
  except KeyError:
38
  voted_concepts = []
39
+ unseen_concepts = []
40
+ tie_concepts = [c for c in sample["concepts"] if sample["concepts"][c] is None]
41
 
42
  return (
43
  image_path,
 
46
  sample["class"],
47
  sample["concepts"],
48
  str(sample_idx),
49
+ unseen_concepts,
50
+ tie_concepts,
51
  filtered_indices,
52
  )
53
 
 
76
  username = profile.username
77
  if current_image is None:
78
  gr.Warning("No image selected.")
79
+ return None, None, None, None, None, None, None, index, filtered_indices
80
  current_split, idx = current_image.split(":")
81
  idx = int(idx)
82
  global_variables.get_metadata(current_split)
 
84
  global_variables.all_metadata[current_split][idx]["votes"] = {}
85
  global_variables.all_metadata[current_split][idx]["votes"][username] = {c: c in voted_concepts for c in CONCEPTS}
86
  vote_sum = {c: 0 for c in CONCEPTS}
87
+ new_concepts = {}
88
  for c in CONCEPTS:
89
  for vote in global_variables.all_metadata[current_split][idx]["votes"].values():
90
  if c not in vote:
91
  continue
92
  vote_sum[c] += 2 * vote[c] - 1
93
+ new_concepts[c] = vote_sum[c] > 0 if vote_sum[c] != 0 else None
94
+ global_variables.all_metadata[current_split][idx]["concepts"] = new_concepts
95
  global_variables.save_metadata(current_split)
96
  gr.Info("Submit success")
97
  return get_next_image(
 
124
  label="Voted Concepts",
125
  choices=CONCEPTS,
126
  )
127
+ unseen_concepts = gr.CheckboxGroup(
128
+ label="Previously Unseen Concepts",
129
+ choices=CONCEPTS,
130
+ )
131
+ tie_concepts = gr.CheckboxGroup(
132
+ label="Tie Concepts",
133
+ choices=CONCEPTS,
134
+ )
135
 
136
  with gr.Row():
137
  prev_button = gr.Button(
 
170
  im_class,
171
  im_concepts,
172
  index,
173
+ unseen_concepts,
174
+ tie_concepts,
175
  filtered_indices,
176
  ]
177
  common_input = [split, index, filtered_indices]
 
195
  inputs=common_input,
196
  outputs=common_output,
197
  )