Spaces:
Runtime error
Runtime error
Commit
·
ce2945b
1
Parent(s):
d5b2219
Update app.py
Browse files
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=
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
|
| 228 |
-
query_choices = ["Random"]+list(range(0, 268))
|
| 229 |
with gr.Blocks() as demo:
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 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=["."])
|