<jupyter_start><jupyter_text>Generate data mapping<jupyter_code>aid_2_answer, answer_2_aid = {}, {}
aid_2_answer_vqa, answer_2_aid_vqa = {}, {}
qid_2_question, question_2_qid = {}, {}
for i in range(len(data)):
    question, answer, answer_vqa, img_num, map_name = data[i]
    
    if question not in question_2_qid:
        qid = len(question_2_qid)
        question_2_qid[question] = qid
        qid_2_question[qid] = question
    else:
        qid = question_2_qid[question]
    
    if answer not in answer_2_aid:
        aid = len(answer_2_aid)
        answer_2_aid[answer] = aid
        aid_2_answer[aid] = answer
    else:
        aid = answer_2_aid[answer]

    if answer_vqa not in answer_2_aid_vqa:
        aid_vqa = len(answer_2_aid_vqa)
        answer_2_aid_vqa[answer_vqa] = aid_vqa
        aid_2_answer_vqa[aid_vqa] = answer_vqa
    else:
        aid_vqa = answer_2_aid_vqa[answer_vqa]
        
    data[i] = [
        qid,
        aid,
        aid_vqa,
        img_num,
        map_name
    ]
vocab = {"word_2_wid": {}, "wid_2_word": {}}

for answer in answer_2_aid:
    for word in answer.split():
        if word not in vocab["word_2_wid"]:
            wid = len(vocab["word_2_wid"])
            vocab["word_2_wid"][word] = wid
            vocab["wid_2_word"][wid] = word
# Add eos
eos_id = len(vocab["word_2_wid"])
vocab["word_2_wid"]["<eos>"] = eos_id 
vocab["wid_2_word"][eos_id] = "<eos>"

# Add bos
bos_id = len(vocab["word_2_wid"])
vocab["word_2_wid"]["<bos>"] = bos_id 
vocab["wid_2_word"][bos_id] = "<bos>"

# Add pad
pad_id = len(vocab["word_2_wid"])
vocab["word_2_wid"]["<pad>"] = pad_id 
vocab["wid_2_word"][pad_id] = "<pad>"
vocab<jupyter_output><empty_output><jupyter_text>Save Mappings and Data<jupyter_code>mappings_path = "../../qa_data/mappings.json"
processed_data_path = "../../qa_data/processed_data.json"
json.dump({
    "aid_2_answer": aid_2_answer,
    "answer_2_aid": answer_2_aid, 
    "aid_2_answer_vqa": aid_2_answer_vqa,
    "answer_2_aid_vqa": answer_2_aid_vqa,
    "qid_2_question": qid_2_question,
    "question_2_qid": question_2_qid,
    "vocab": vocab
}, open(mappings_path, "w"), indent=4)
json.dump({
    "data": data
}, open(processed_data_path, "w"), indent=2)<jupyter_output><empty_output><jupyter_text>Test the Dataset class<jupyter_code>import sys
from importlib import reload
import json
sys.path.append("..")

from sklearn.model_selection import train_test_split 
from scene_description import model, data_utils

mappings_path = "../../qa_data/mappings.json"
processed_data_path = "../../qa_data/processed_data.json"
mappings = json.load(open(mappings_path))
qa_data = json.load(open(processed_data_path))["data"]<jupyter_output><empty_output><jupyter_text>----------<jupyter_code>reload(data_utils)
dataset = data_utils.SceneDescriptionDataset(qa_data, mappings, "../../qa_data/")
(question, img), (answer, answer_vqa) = dataset.__getitem__(0)
question, answer, answer_vqa
img.min(), img.max()<jupyter_output><empty_output><jupyter_text>Test the dataloader class<jupyter_code>import torch
dataloader = torch.utils.data.DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=data_utils.collate_fn)
for (question, img), (answer, answer_vqa) in dataloader:
    print(question)
    print(img.shape)
    print(answer)
    print(answer_vqa)
    break<jupyter_output>['how many torch are visible?', 'is a fire present near by a furniture?', 'number of door present?', 'how many switch are seen?']
torch.Size([4, 3, 480, 640])
tensor([[49,  8, 16,  3, 13, 48, 50, 50],
        [49,  3, 28, 31, 27, 17,  7, 48],
        [49,  3, 14, 11, 12, 48, 50, 50],
        [49,  8, 21, 43,  0, 48, 50, 50]])
tensor([2, 0, 2, 6])<jupyter_text>Test the model<jupyter_code>reload(model)
sd = model.SceneDescription(num_answers=len(mappings["aid_2_answer_vqa"]), vocab_size=len(mappings["vocab"]["wid_2_word"]))
sd
op = sd(question, img, answer)
op[0].shape
op[1].shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14746192893401014, 'fraction_numerical': 0.03197969543147208, 'mean_word_length': 4.325675675675676, 'pattern_counts': {'":': 10, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '191868', 'license': 'Apache-2.0', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1575, 'n_words': 279, 'path': 'notebooks/Data Processing.ipynb', 'repo_name': 'dataviral/scene-description', 'size': '3940'}