sindhuhegde commited on
Commit
ce2945b
·
1 Parent(s): d5b2219

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -77
app.py CHANGED
@@ -163,87 +163,99 @@ def plot_query(query_idx, data, key_list, val_list, data_path="videos/", fps=15,
163
 
164
  return plot
165
 
166
- def retrieve(file_choice, target_word, query_idx, rows=5, cols=4):
167
-
168
- # file_choice = fileobj.name
169
- print(file_choice)
170
-
171
- if file_choice=="big_little":
172
- file="features/gestsync_feats_biglittle_train.pkl"
173
- word_map={"big":0, "little":1}
174
- print("Target word: ", target_word)
175
- target_label=word_map[target_word]
176
-
177
- print("Target label: ", target_label)
178
- key_list = list(word_map.keys())
179
- val_list = list(word_map.values())
180
-
181
- x_train, y_train_actual, files_train, data_rows = load_feats(file)
182
- print("X train: {} | Y train: {} | Files train: {} | Data rows test: {}".format(x_train.shape, y_train_actual.shape, files_train.shape, len(data_rows)))
183
-
184
- print("Query idx orig: ", query_idx)
185
- if query_idx=="Random":
186
- pos_indices = find_ind(target_word, y_train_actual, word_map)
187
- query_idx = random.choice(pos_indices)
188
- else:
189
- query_idx = int(query_idx)
190
-
191
- print("Query idx: ", query_idx)
192
-
193
- y_train = get_binary_labels_exemplar(y_train_actual, target_label)
194
-
195
- svm_x_train = []
196
- svm_y_train = []
197
- svm_data_rows = []
198
-
199
- for i in range(len(x_train)):
200
- if y_train[i]==-1:
201
- svm_x_train.append(x_train[i])
202
- svm_y_train.append(y_train[i])
203
- svm_data_rows.append(data_rows[i])
204
- if i==query_idx:
205
- print(query_idx, y_train[i])
206
- svm_x_train.append(x_train[i])
207
- svm_y_train.append(y_train[i])
208
- svm_data_rows.append(data_rows[i])
209
-
210
- svm_x_train = np.array(svm_x_train).astype(float)
211
- svm_y_train = np.array(svm_y_train).astype(float)
212
- print("SVM dataset - X: {} | Y: {}".format(svm_x_train.shape,svm_y_train.shape))
213
-
214
- clf = svm.SVC(C=1, kernel="linear")
215
- clf.fit(svm_x_train, svm_y_train)
216
-
217
- scores = get_scores_sklearn(x_train, clf)
218
- # print(scores)
219
-
220
- query_plot = plot_query(query_idx, data_rows, key_list, val_list, target_label=target_label)
221
- retrieved_plots = plot_ranked_videos(y_train_actual, torch.tensor(scores), data_rows, key_list, val_list, row=rows, col=cols, target_label=target_label)
222
-
223
- return query_plot, retrieved_plots
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
 
226
  if __name__ == "__main__":
227
 
228
- query_choices = ["Random"]+list(range(0, 268))
229
  with gr.Blocks() as demo:
230
- with gr.Row():
231
- gr.HTML("<b> <font size=5> <center> Welcome to Exemplar-SVM Training and Visualization of top-k videos </center> </font> </b>")
232
-
233
- with gr.Row():
234
- features = gr.Dropdown(["big_little"], label="Word classes", value="big_little")
235
- word = gr.Radio(["big", "little"], label="Target word", value="big")
236
- query_idx = gr.Dropdown(query_choices, value="Random", label="Positive sample index to train Exemplar SVM")
237
-
238
- with gr.Row():
239
- submit = gr.Button("Retrieve")
240
-
241
- with gr.Row():
242
- query = gr.HTML()
243
-
244
- with gr.Row():
245
- ret_videos = gr.HTML()
246
-
247
- submit.click(retrieve, inputs=[features, word, query_idx], outputs=[query, ret_videos])
248
 
249
  demo.launch(allowed_paths=["."])
 
163
 
164
  return plot
165
 
166
+ def retrieve(file_choice, target_word, query_idx, rows=6, cols=4):
167
+
168
+ print(file_choice)
169
+
170
+ if file_choice=="big_little":
171
+ file="features/gestsync_feats_biglittle_train.pkl"
172
+ word_map={"big":0, "little":1}
173
+ print("Target word: ", target_word)
174
+ if target_word not in list(word_map.keys()):
175
+ msg="The selected target word is not present in the word classes. For '{}' word classes, please select the target word from the following list: {}".format(file_choice, list(word_map.keys()))
176
+ return msg, ""
177
+ target_label=word_map[target_word]
178
+ elif file_choice=="5_words":
179
+ file="features/gestsync_feats_5words_balanced_train.pkl"
180
+ word_map = {'big': 0, 'little': 1, 'next': 2, 'i': 3, 'you': 4}
181
+ print("Target word: ", target_word)
182
+ if target_word not in list(word_map.keys()):
183
+ msg="The selected target word is not present in the word classes. For '{}' word classes, please select the target word from the following list: {}".format(file_choice, list(word_map.keys()))
184
+ return msg, ""
185
+ target_label=word_map[target_word]
186
+
187
+ print("Target label: ", target_label)
188
+ key_list = list(word_map.keys())
189
+ val_list = list(word_map.values())
190
+
191
+ x_train, y_train_actual, files_train, data_rows = load_feats(file)
192
+ print("X train: {} | Y train: {} | Files train: {} | Data rows test: {}".format(x_train.shape, y_train_actual.shape, files_train.shape, len(data_rows)))
193
+
194
+ print("Query idx orig: ", query_idx)
195
+ pos_indices = find_ind(target_word, y_train_actual, word_map)
196
+ if query_idx=="Random":
197
+ query_idx = random.choice(pos_indices)
198
+ else:
199
+ query_idx = int(query_idx)
200
+ if query_idx not in pos_indices:
201
+ msg = "The input query index selected ({}) is not the right index! Please select an index of the positive sample to include in Exemplar-SVM training. <br> For the selected target word '{}', the following are the positive indices in the dataset that can be selected: <br> {}".format(query_idx, target_word, pos_indices)
202
+ return msg, ""
203
+
204
+ print("Query idx: ", query_idx)
205
+
206
+ y_train = get_binary_labels_exemplar(y_train_actual, target_label)
207
+
208
+ svm_x_train = []
209
+ svm_y_train = []
210
+ svm_data_rows = []
211
+
212
+ for i in range(len(x_train)):
213
+ if y_train[i]==-1:
214
+ svm_x_train.append(x_train[i])
215
+ svm_y_train.append(y_train[i])
216
+ svm_data_rows.append(data_rows[i])
217
+ if i==query_idx:
218
+ print(query_idx, y_train[i])
219
+ svm_x_train.append(x_train[i])
220
+ svm_y_train.append(y_train[i])
221
+ svm_data_rows.append(data_rows[i])
222
+
223
+ svm_x_train = np.array(svm_x_train).astype(float)
224
+ svm_y_train = np.array(svm_y_train).astype(float)
225
+ print("SVM dataset - X: {} | Y: {}".format(svm_x_train.shape,svm_y_train.shape))
226
+
227
+ clf = svm.SVC(C=1, kernel="linear")
228
+ clf.fit(svm_x_train, svm_y_train)
229
+
230
+ scores = get_scores_sklearn(x_train, clf)
231
+ # print(scores)
232
+
233
+ query_plot = plot_query(query_idx, data_rows, key_list, val_list, target_label=target_label)
234
+ retrieved_plots = plot_ranked_videos(y_train_actual, torch.tensor(scores), data_rows, key_list, val_list, row=rows, col=cols, target_label=target_label)
235
+
236
+ return query_plot, retrieved_plots
237
 
238
 
239
  if __name__ == "__main__":
240
 
 
241
  with gr.Blocks() as demo:
242
+ with gr.Row():
243
+ gr.HTML("<b> <font size=5> <center> Welcome to Exemplar-SVM Training and Visualization of top-k videos </center> </font> </b>")
244
+
245
+ with gr.Row():
246
+ features = gr.Dropdown(["big_little", "5_words"], label="Word classes", value="big_little")
247
+ word = gr.Radio(["big", "little", "i", "you", "next"], label="Target word", value="big")
248
+ query_idx = gr.Textbox(value="Random", label="Positive sample index to train Exemplar-SVM (Type 'Random' to randomly select the index)")
249
+
250
+ with gr.Row():
251
+ submit = gr.Button("Retrieve")
252
+
253
+ with gr.Row():
254
+ query = gr.HTML()
255
+
256
+ with gr.Row():
257
+ ret_videos = gr.HTML()
258
+
259
+ submit.click(retrieve, inputs=[features, word, query_idx], outputs=[query, ret_videos])
260
 
261
  demo.launch(allowed_paths=["."])