<jupyter_start><jupyter_text>A notebook to query the trained embedding with the test set to qualitatively evaluate the text-video retrieval<jupyter_code>import torch as th
from torch.utils.data import DataLoader
import numpy as np
import torch.optim as optim
from args import get_args
import random
import os
from model import Net
from metrics import compute_metrics, print_computed_metrics
from loss import MaxMarginRankingLoss
from gensim.models.keyedvectors import KeyedVectors
import pickle
from m2e2_dataloader import M2E2DataLoader
import pandas as pd 
import json
word2vec_path='data/GoogleNews-vectors-negative300.bin'

print('Loading word vectors: {}'.format(word2vec_path))
we = KeyedVectors.load_word2vec_format(word2vec_path, binary=True)
print('done')
sentences_path = "/kiwi-data/users/shoya/AIDA/event_occurences_video_and_text_pairs.json"
max_words = 20
we_dim = 300 
batch_size=256
batch_size_val=3500
num_workers = 4

dataset_val_m2e2 = M2E2DataLoader(
    csv="/home/shoya/howto100m/data_paths_test.csv",
    sentences=sentences_path,
    we=we,
    max_words=max_words,
    we_dim=we_dim,
)
dataloader_val_m2e2 = DataLoader(
    dataset_val_m2e2,
    batch_size=batch_size_val,
    num_workers=num_workers,
    shuffle=False,
)
net = Net(
    video_dim=4096,
    embd_dim=6144,
    we_dim=300,
    n_pair=1,
    max_words=max_words,
    sentence_dim=-1,
)

net.load_checkpoint('e31.pth')
net.eval() 
net.cuda()
batch = next(iter(dataloader_val_m2e2))
text = batch['text'].cuda() 
video = batch['video'].cuda() 
output = net(video, text)
output = output.cpu().detach().numpy()
eval_paths = pd.read_csv("/home/shoya/howto100m/data_paths_test.csv")
eval_file_names = eval_paths['video_id'].values
video2sentence = json.load(open(sentences_path))
for correct_idx, prob in enumerate(output[:30]):
    corresponding_video = eval_file_names[correct_idx]
    corresponding_sentences = video2sentence[corresponding_video]
    
    correct_idx_prob = prob[correct_idx]
    sorted_x = np.sort(-prob)
    correct_guessed_at = np.where(sorted_x+correct_idx_prob == 0)[0][0]
    
    print('============= Test Sample {} ============='.format(correct_idx+1))
    print("Query Sentence: {}".format(corresponding_sentences))
    print("Corresponding Video: {}".format(corresponding_video))
    print("Correctly Guessed on Index {}".format(correct_guessed_at))
    print()
    
    
    top_5_index = prob.argsort()[-5:][::-1]
    for i,pred_idx in enumerate(top_5_index):
        print("Guess {}: {}".format(i+1,eval_file_names[pred_idx]))
    print()<jupyter_output>============= Test Sample 1 =============
Query Sentence: ['US President <NAME> has told his citizens they should brace for " painful " weeks ahead .']
Corresponding Video: Coronavirus Trump warns of very painful weeks ahead - BBC News clipped_23.557_86.748.mp4
Correctly Guessed on Index 1

Guess 1: Coronavirus President Trump Cuts US Funding to WHO clipped_0_38.157.mp4
Guess 2: Coronavirus Trump warns of very painful weeks ahead - BBC News clipped_23.557_86.748.mp4
Guess 3: Blasts Heard at Ashraf Ghani Inauguration as Afghan President clipped_38.802_46.198.mp4
Guess 4: Senate Committee Holds Hearing into Fort Hood Attack clipped_116.027_155.277.mp4
Guess 5: <NAME> attends Manchester attack remembrance service in UK clipped_19.004_30.04.mp4

============= Test Sample 2 =============
Query Sentence: ["Police officers and army personnel in Mosul , Kirkuk and Ramadi queued to cast their votes two days before the rest of the nation ' s voters can go to the polls to elect a ne[...]
{'fraction_non_alphanumeric': 0.11401291771974165, 'fraction_numerical': 0.03903397921932041, 'mean_word_length': 5.27112676056338, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '571879', 'license': 'Apache-2.0', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1184, 'n_words': 304, 'path': 'visualize_output.ipynb', 'repo_name': 'shoyasaxa/howto100m', 'size': '3575'}