File size: 5,253 Bytes
1e5f3d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
from datasets import load_dataset
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader, random_split
from PIL import Image
from collections import Counter
import pickle
import re
from tqdm import tqdm
import os

# ========================
# CONFIG
# ========================
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
EPOCHS = 50
BATCH_SIZE = 32
LR = 5e-4
MAX_LEN = 20

# ========================
# LOAD DATASET
# ========================
dataset = load_dataset("flaviagiammarino/vqa-rad")
df = pd.DataFrame(dataset["train"])
df = df[["image", "question", "answer"]]

# ========================
# CLEAN TEXT
# ========================
def clean_text(text):
    text = text.lower()
    return re.sub(r"[^a-z0-9 ]", "", text)

df["question"] = df["question"].apply(clean_text)
df["answer"] = df["answer"].apply(clean_text)

# ========================
# FILTER TOP ANSWERS
# ========================
top_answers = df["answer"].value_counts().nlargest(50).index
df = df[df["answer"].isin(top_answers)]

answer_to_idx = {a:i for i,a in enumerate(top_answers)}
idx_to_answer = {i:a for a,i in answer_to_idx.items()}
df["answer_encoded"] = df["answer"].apply(lambda x: answer_to_idx[x])

# ========================
# VOCAB
# ========================
vocab = {"<PAD>":0, "<UNK>":1}
counter = Counter()

for q in df["question"]:
    for w in q.split():
        counter[w] += 1

idx = 2
for word, count in counter.items():
    if count > 2:
        vocab[word] = idx
        idx += 1

def encode_question(q):
    tokens = q.split()
    enc = [vocab.get(w, vocab["<UNK>"]) for w in tokens]
    enc = enc[:MAX_LEN] + [vocab["<PAD>"]] * (MAX_LEN - len(enc))
    return enc

df["question_encoded"] = df["question"].apply(encode_question)

# ========================
# DATASET CLASS
# ========================
transform = transforms.Compose([
    transforms.Resize((224,224)),
    transforms.ToTensor()
])

class VQADataset(Dataset):
    def __init__(self, df):
        self.df = df

    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        row = self.df.iloc[idx]

        image = row["image"].convert("RGB")
        image = transform(image)

        question = torch.tensor(row["question_encoded"])
        answer = torch.tensor(row["answer_encoded"])

        return image, question, answer

# ========================
# SPLIT DATA
# ========================
dataset_full = VQADataset(df)
train_size = int(0.8 * len(dataset_full))
val_size = len(dataset_full) - train_size

train_dataset, val_dataset = random_split(dataset_full, [train_size, val_size])

train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE)

# ========================
# MODEL
# ========================
import torchvision.models as models

class VQAModel(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim, num_answers):
        super().__init__()

        self.cnn = models.resnet18(weights="DEFAULT")
        self.cnn.fc = nn.Identity()

        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True)

        self.fc1 = nn.Linear(512 + hidden_dim, 256)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(256, num_answers)

    def forward(self, image, question):
        img_feat = self.cnn(image)

        q_embed = self.embedding(question)
        _, (h, _) = self.lstm(q_embed)
        q_feat = h.squeeze(0)

        x = self.relu(self.fc1(torch.cat((img_feat, q_feat), dim=1)))
        return self.fc2(x)

model = VQAModel(len(vocab), 300, 256, len(answer_to_idx)).to(DEVICE)

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=LR)

# ========================
# TRAIN LOOP
# ========================
for epoch in range(EPOCHS):
    model.train()
    total_loss = 0

    for images, questions, answers in tqdm(train_loader):
        images, questions, answers = images.to(DEVICE), questions.to(DEVICE), answers.to(DEVICE)

        outputs = model(images, questions)
        loss = criterion(outputs, answers)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        total_loss += loss.item()

    # VALIDATION
    model.eval()
    val_loss = 0

    with torch.no_grad():
        for images, questions, answers in val_loader:
            images, questions, answers = images.to(DEVICE), questions.to(DEVICE), answers.to(DEVICE)

            outputs = model(images, questions)
            loss = criterion(outputs, answers)
            val_loss += loss.item()

    print(f"\nEpoch {epoch+1}")
    print(f"Train Loss: {total_loss/len(train_loader):.4f}")
    print(f"Val Loss: {val_loss/len(val_loader):.4f}")

# ========================
# SAVE MODEL
# ========================
os.makedirs("weights", exist_ok=True)

torch.save(model.state_dict(), "weights/vqa_model.pth")

with open("weights/vocab.pkl", "wb") as f:
    pickle.dump(vocab, f)

with open("weights/answers.pkl", "wb") as f:
    pickle.dump(idx_to_answer, f)

print("\n✅ Training Complete & Model Saved!")