-
Notifications
You must be signed in to change notification settings - Fork 3
/
vqa_model.py
394 lines (313 loc) · 16.7 KB
/
vqa_model.py
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torch
import torch.nn as nn
import clip
import requests
from sklearn.metrics import average_precision_score
class VQAModel(nn.Module):
def __init__(self, num_classes, hidden_size, model_name = "ViT-L/14@336px", device = torch.device("cpu")):
super(VQAModel, self).__init__()
self.training_losses = []
self.validation_losses = []
self.training_accuracies = []
self.validation_accuracies = []
self.vizwiz_training_accuracies = []
self.vizwiz_validation_accuracies = []
self.training_answerability = []
self.validation_answerability = []
self.device = device
self.model_name = model_name
# Initializing Binary Cross Entropy Loss which will be used to train the model on answerability
self.answerability_loss_fn = nn.BCELoss()
# Loading the CLIP model
self.clip_model, self.preprocess = clip.load(model_name, device = device)
# Freezing the CLIP model
for param in self.clip_model.parameters():
param.requires_grad = False
# First linear layer
self.linear_layer1 = nn.Sequential(
nn.LayerNorm(self.clip_model.visual.output_dim + self.clip_model.text_projection.shape[1]),
nn.Dropout(p=0.5),
nn.Linear(self.clip_model.visual.output_dim + self.clip_model.text_projection.shape[1], hidden_size)
)
# Second linear layer
self.linear_layer2 = nn.Sequential(
nn.LayerNorm(hidden_size),
nn.Dropout(p=0.5),
nn.Linear(hidden_size, num_classes)
)
self.answer_type_layer = nn.Linear(hidden_size, 4)
self.answer_mask_layer = nn.Linear(4, num_classes)
self.sigmoid = nn.Sigmoid()
# Answerability Linear Layer (We removed drop out layer because training answerability was very bad)
self.answerability_linear_layer = nn.Sequential(
nn.LayerNorm(self.clip_model.visual.output_dim + self.clip_model.text_projection.shape[1]),
nn.Linear(self.clip_model.visual.output_dim + self.clip_model.text_projection.shape[1], hidden_size)
)
# Answerability Sigmoid Layer
self.answerability_final_layer = nn.Linear(hidden_size, 1)
# Sigmoid Layer for Answerability
self.answerability_sigmoid = nn.Sigmoid()
def forward(self, image, question):
# Flattening and concatenating the image and question features
image = torch.flatten(image, start_dim=1)
question = torch.flatten(question, start_dim=1)
features = torch.cat((image, question), dim=1)
# Calculating the answerability score
answerability_score = self.answerability_linear_layer(features)
answerability_score = self.answerability_final_layer(answerability_score)
answerability_score = self.answerability_sigmoid(answerability_score)
answerability_score = answerability_score.squeeze()
# Passing the features through the first linear layer
features = self.linear_layer1(features)
# Passing the features to get 4 answer types
answer_type = self.answer_type_layer(features)
# Expanding answer make to the same size as the number of classes (vocab size)
answer_mask = self.answer_mask_layer(answer_type)
# Applying sigmoid to get the answer mask
answer_mask = self.sigmoid(answer_mask)
# Passing the features through the second linear layer
output = self.linear_layer2(features)
# Applying the answer mask to the output
output = output * answer_mask
return output, answer_type, answerability_score
def train_model(self, training_dataloader, validation_dataloader, test_dataloader, criterion, optimizer, epochs = 10, save_path = None, save_every = 1):
for epoch in range(1,epochs+1):
training_loss, training_accuracy, training_vizwiz_accuracy, train_answerability_score = self.training_step(training_dataloader, criterion, optimizer, self.device)
validation_loss, validation_accuracy, validation_vizwiz_accuracy, validation_answerability_score = self.validation_step(validation_dataloader, criterion, self.device)
test_accuracy, test_vizwiz_accuracy, test_answerability_score = self.test_step(test_dataloader)
self.training_losses.append(training_loss)
self.validation_losses.append(validation_loss)
self.training_accuracies.append(training_accuracy)
self.validation_accuracies.append(validation_accuracy)
self.vizwiz_training_accuracies.append(training_vizwiz_accuracy)
self.vizwiz_validation_accuracies.append(validation_vizwiz_accuracy)
self.training_answerability.append(train_answerability_score)
self.validation_answerability.append(validation_answerability_score)
print("Epoch: {} | Training Loss: {:.3f} | Validation Loss: {:.3f}".format(epoch, training_loss, validation_loss))
print("Epoch: {} | Training Accuracy: {:.3f} | Validation Accuracy: {:.3f} | Test Accuracy: {:.3f}".format(epoch, training_accuracy, validation_accuracy, test_accuracy))
print("Epoch: {} | Training VizWiz Accuracy: {:.3f} | Validation VizWiz Accuracy: {:.3f} | Test VizWiz Accuracy: {:.3f}".format(epoch, training_vizwiz_accuracy, validation_vizwiz_accuracy, test_vizwiz_accuracy))
print("Epoch: {} | Training Answerability Score: {:.3f} | Validation Answerability Score: {:.3f} | Test Answerability Score: {:.3f}\n".format(epoch, train_answerability_score, validation_answerability_score, test_answerability_score))
if save_path != None and epoch % save_every == 0:
self.save_model(save_path + "epoch_{}.pth".format(epoch))
return
def training_step(self, dataloader, criterion, optimizer, device):
training_loss, training_accuracy, vizwiz_accuracy, total_sum = 0.0, 0.0, 0.0, 0
answerable_true = []
answerable_predicted = []
self.train()
for _, batch in enumerate(dataloader):
image, question, answer, answer_type, answers_for_questions, answerable = batch
image, question, answer, answer_type, answers_for_questions, answerable = image.to(device), question.to(device), answer.to(device), answer_type.to(device), answers_for_questions.to(device), answerable.to(device)
optimizer.zero_grad()
output, answer_type_predicted, answerable_predict = self.forward(image, question)
answerable = 1 - answerable
answerable_predict = 1.0 - answerable_predict
loss = criterion(output, answer) + criterion(answer_type_predicted, answer_type) + self.answerability_loss_fn(answerable_predict, answerable)
loss.backward()
optimizer.step()
training_loss += loss.item()
predicted_answer = torch.argmax(output, dim = 1)
actual_answer = torch.argmax(answer, dim = 1)
for i in range(len(answer)):
if actual_answer[i] == predicted_answer[i]:
training_accuracy +=1
total_sum +=1
vizwiz_accuracy += min(1, torch.sum(torch.eq(predicted_answer[i], answers_for_questions[i])).item()/3)
answerable_true.append(answerable[i].item())
answerable_predicted.append(answerable_predict[i].item())
answerable_true = np.array(answerable_true)
answerable_predicted = np.array(answerable_predicted)
training_loss /= len(dataloader)
training_accuracy /= total_sum
vizwiz_accuracy /= total_sum
return training_loss, training_accuracy, vizwiz_accuracy, average_precision_score(answerable_true, answerable_predicted, average = 'weighted')
def validation_step(self, dataloader, criterion, device):
validation_loss, validation_accuracy, vizwiz_accuracy, total_sum = 0.0, 0.0, 0.0, 0
answerable_true = []
answerable_predicted = []
self.eval()
with torch.no_grad():
for _, batch in enumerate(dataloader):
image, question, answer, answer_type, answers_for_questions, answerable = batch
image, question, answer, answer_type, answers_for_questions, answerable = image.to(device), question.to(device), answer.to(device), answer_type.to(device), answers_for_questions.to(device), answerable.to(device)
output, answer_type_predicted, answerable_predict = self.forward(image, question)
# Answerablity is the confidence that quesion is not answerable, so we have to subtract from 1
answerable = 1 - answerable
answerable_predict = 1.0 - answerable_predict
loss = criterion(output, answer) + criterion(answer_type_predicted, answer_type) + self.answerability_loss_fn(answerable_predict, answerable)
validation_loss += loss.item()
predicted_answer = torch.argmax(output, dim = 1)
actual_answer = torch.argmax(answer, dim = 1)
for i in range(len(answer)):
if torch.sum(answer[i]) == 0:
continue
if actual_answer[i] == predicted_answer[i]:
validation_accuracy += 1
total_sum +=1
vizwiz_accuracy += min(1, torch.sum(torch.eq(predicted_answer[i], answers_for_questions[i])).item()/3)
answerable_true.append(answerable[i].item())
answerable_predicted.append(answerable_predict[i].item())
answerable_true = np.array(answerable_true)
answerable_predicted = np.array(answerable_predicted)
validation_loss /= len(dataloader)
validation_accuracy /= total_sum
vizwiz_accuracy /= total_sum
# We will use weighted average since that there is imbalance in answerability in the dataset as displayed in EDA section
return validation_loss, validation_accuracy, vizwiz_accuracy, average_precision_score(answerable_true, answerable_predicted, average = 'weighted')
def test_step(self, dataloader):
self.eval()
accuracy, total_sum, vizwiz_accuracy = 0.0, 0, 0.0
answerable_true = []
answerable_predicted = []
with torch.no_grad():
for _, batch in enumerate(dataloader):
image, question, answer, answer_type, answers_for_questions, answerable = batch
image, question, answer, answer_type, answers_for_questions, answerable = image.to(self.device), question.to(self.device), answer.to(self.device), answer_type.to(self.device), answers_for_questions.to(self.device), answerable.to(self.device)
output, _, answerable_predict = self.forward(image, question)
answerable = 1 - answerable
answerable_predict = 1.0 - answerable_predict
predicted_answer = torch.argmax(output, dim = 1)
actual_answer = torch.argmax(answer, dim = 1)
for i in range(len(answer)):
if torch.sum(answer[i]) == 0:
continue
if predicted_answer[i] == actual_answer[i]:
accuracy += 1
vizwiz_accuracy += min(1, torch.sum(torch.eq(predicted_answer[i], answers_for_questions[i])).item()/3)
total_sum +=1
answerable_true.append(answerable[i].item())
answerable_predicted.append(answerable_predict[i].item())
answerable_true = np.array(answerable_true)
answerable_predicted = np.array(answerable_predicted)
accuracy /= total_sum
vizwiz_accuracy /= total_sum
return accuracy, vizwiz_accuracy, average_precision_score(answerable_true, answerable_predicted, average = 'weighted')
def save_model(self, path):
"""
Saves the model state dictionary to the given path.
Args:
- self: the model object
- path (str): the path to save the model state dictionary
Returns:
- None
"""
torch.save(self.state_dict(), path)
def load_model(self, path):
"""
Loads the model state dictionary from the given path.
Args:
- self: the model object
- path (str): the path to load the model state dictionary
Returns:
- self: the loaded model object
"""
self.load_state_dict(torch.load(path, map_location=self.device))
self.eval()
return self
def predict(self, image, question):
"""
Predicts the output and answer type for the given image and question.
Args:
- self: the model object
- image (tensor): the image tensor
- question (tensor): the question tensor
Returns:
- output (tensor): the predicted output tensor
- answer_type (str): the predicted answer type
"""
output, answer_type, answerability = self.forward(image, question)
answerability = 1.0 - answerability
return output, answer_type, answerability
def plot_loss(self):
"""
Plots the training and validation losses.
Args:
- self: the model object
Returns:
- None
"""
plt.plot(self.training_losses, label = "Training Loss")
plt.plot(self.validation_losses, label = "Validation Loss")
plt.legend()
plt.show()
def plot_accuracy(self):
"""
Plots the training and validation accuracies.
Args:
- self: the model object
Returns:
- None
"""
plt.plot(self.training_accuracies, label = "Training Accuracy")
plt.plot(self.validation_accuracies, label = "Validation Accuracy")
plt.legend()
plt.show()
def plot_vizwiz_accuracy(self):
"""
Plots the VizWiz training and validation accuracies.
Args:
- self: the model object
Returns:
- None
"""
plt.plot(self.vizwiz_training_accuracies, label = "VizWiz Training Accuracy")
plt.plot(self.vizwiz_validation_accuracies, label = "VizWiz Validation Accuracy")
plt.legend()
plt.show()
def plot_answerability(self):
"""
Plots the training and validation answerabilities.
Args:
- self: the model object
Returns:
- None
"""
plt.plot(self.training_answerability, label = "Training Answerability")
plt.plot(self.validation_answerability, label = "Validation Answerability")
plt.legend()
plt.show()
def test_model(self, image_path, question):
"""
Tests the model by predicting the answer and answer type for the given image and question.
Args:
- self: the model object
- image_path (str): the path to the image file or URL
- question (str): the question to be asked
Returns:
- predicted_answer (tensor): the predicted answer tensor
- predicted_answer_type (str): the predicted answer type
"""
self.eval()
if image_path.startswith("http"):
image = Image.open(requests.get(image_path, stream = True).raw)
else:
image = Image.open(image_path)
image = self.preprocess(image).unsqueeze(0).to(self.device) # type: ignore
image_features = self.clip_model.encode_image(image)
image_features = torch.flatten(image_features, start_dim=1)
question = clip.tokenize(question).to(self.device)
text_features = self.clip_model.encode_text(question).float()
text_features = torch.flatten(text_features, start_dim=1)
predicted_answer, predicted_answer_type, answerability = self.predict(image_features, text_features)
return predicted_answer, predicted_answer_type, answerability
def print_CLIP_model(self):
"""
Prints the details of the selected CLIP model.
Args:
- self: the model object
Returns:
- None
"""
input_resolution = self.clip_model.visual.input_resolution
context_length = self.clip_model.context_length
vocab_size = self.clip_model.vocab_size
print("Selected model:", self.model_name)
print("Model parameters:", f"{np.sum([int(np.prod(p.shape)) for p in self.clip_model.parameters()]):,}")
print("Input resolution:", input_resolution)
print("Context length:", context_length)
print("Vocab size:", vocab_size)
print("")
__all__ = ['VQAModel']