-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvilt_vanilla_finetuning.py
520 lines (308 loc) · 15 KB
/
vilt_vanilla_finetuning.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import json
import os
import torch
import random
import sys
import datasets
import numpy as np
from PIL import Image
from tqdm import tqdm
from datasets import Dataset
from datasets import load_dataset
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
path = './vilt_vanilla_finetuning.txt'
sys.stdout = open(path, 'w')
def collate_fn(batch):
input_ids = [item['input_ids'] for item in batch]
pixel_values = [item['pixel_values'] for item in batch]
attention_mask = [item['attention_mask'] for item in batch]
token_type_ids = [item['token_type_ids'] for item in batch]
labels = [item['labels'] for item in batch]
# create padded pixel values and corresponding pixel mask
encoding = processor.image_processor.pad(pixel_values, return_tensors="pt")
# create new batch
batch = {}
batch['input_ids'] = torch.stack(input_ids)
batch['attention_mask'] = torch.stack(attention_mask)
batch['token_type_ids'] = torch.stack(token_type_ids)
batch['pixel_values'] = encoding['pixel_values']
batch['pixel_mask'] = encoding['pixel_mask']
batch['labels'] = torch.stack(labels, dim = 0 )
return batch
"""
# This function returns the Validation Loss and accuracy on the Validation Set
def calculateAccuracyVal():
matchScore, loopCounter = 0,0
for index in range(0,10000):
loopCounter += 1
val_example = val_dataset_object[index]
val_example = {k: v.unsqueeze(0).to(device) for k,v in val_example.items()}
val_outputs = model(**val_example)
validationLoss = val_outputs.loss
val_logits = val_outputs.logits
val_predicted_classes = torch.sigmoid(val_logits)
val_ans = reverse_mapping[torch.argmax(val_predicted_classes).item()]
#print(f'T: {answerList_val[index]} <-> P: {val_ans}' )
# accuracy score
if answerList_val[index] == val_ans:
matchScore += 1
#print(matchScore, loopCounter)
accuracyVal = (matchScore/loopCounter)*100
return ( accuracyVal,validationLoss.item() )
# This function returns accuracy on the Test Set
def calculateAccuracyTest():
matchScore, loopCounter = 0,0
model.eval()
for index in range(0, 10000):
loopCounter += 1
test_example = test_dataset_object[index]
test_example = {k: v.unsqueeze(0).to(device) for k,v in test_example.items()}
test_outputs = model(**test_example)
test_logits = test_outputs.logits
test_predicted_classes = torch.sigmoid(test_logits)
test_ans = reverse_mapping[torch.argmax(test_predicted_classes).item()]
# print(f'T: {answerList_val[index]} <-> P: {test_ans}' )
# accuracy score
if answerList_test[index] == test_ans:
matchScore += 1
print(f'\nTotal Questions {loopCounter}')
print(f'\nCorrectly classified {matchScore}')
return ((matchScore/loopCounter)*100)
"""
# This function creates a uniques list of all the labels(answers) which was using a randomised
# set for this purpose.
def findUnique(targetList):
uniqueList = []
for word in targetList:
if word not in uniqueList:
uniqueList.append(word)
return uniqueList
#---------------------------------------------------------------------------------------------------------------------------------------------
print('Loading JSON Data')
train_file_path = '/home/aritra/cric/train_questions.json'
val_file_path = '/home/aritra/cric/val_questions.json'
test_file_path = '/home/aritra/cric/test_v1_questions.json'
# Training Set
with open(train_file_path, "r") as file:
train_json = json.load(file)
# Validation Set
with open(val_file_path, "r") as file:
val_json = json.load(file)
# Test Set
with open(test_file_path, "r") as file:
test_json = json.load(file)
#---------------------------------------------------------------------------------------------------------------------------------------------
print('\nExtracting Data of Training Set')
questionList, answerList, imgList = [], [],[]
print('\nExcluding Erroneous Indices')
# verifying
indexToExclude = []
with open('../text_files/error1.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExclude.append(number)
with open('../text_files/error2.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExclude.append(number)
with open('../text_files/error3.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExclude.append(number)
for i in tqdm(range(len(train_json))):
if i in indexToExclude:
continue
pointer = train_json[i]
questionList.append(pointer['question'])
answerList.append(pointer['answer'])
imgList.append(pointer['image_id'])
print('\nSize of the training set is: ', len(questionList))
#---------------------------------------------------------------------------------------------------------------------------------------------
print('\nCreating word to number mapping')
mapping = {}
counter = 0
uniqueAnsList = findUnique(answerList)
for word in uniqueAnsList:
if word not in mapping:
mapping[word] = counter
counter += 1
numOfClasses = max(mapping.values())
print('\nCreating number to word mapping')
print('\nLength of the unique labels is: ',len(uniqueAnsList))
print('\nLength of the mapping created is: ',len(mapping))
reverse_mapping = dict([(value, key) for key, value in mapping.items()])
#---------------------------------------------------------------------------------------------------------------------------------------------
print('\nProcessing Training Set')
labels = []
for i in range(len(answerList)):
labels.append( mapping[ answerList[i] ] )
scores = []
for i in tqdm(range(len(answerList))):
s = [0] * (numOfClasses+1)
s[ mapping[ answerList[i]] ] = 1
scores.append(s)
imgPathList = []
filepath = '/home/aritra/cric/images/img/'
for i in tqdm(range(len(imgList))):
imgName = str(imgList[i]) + '.jpg'
concatedPath = os.path.join(filepath,imgName)
imgPathList.append(concatedPath)
listToDictionary = {'questions':questionList, 'labels': labels, 'scores': scores, 'images':imgPathList}
modified_train_set = Dataset.from_dict(listToDictionary)
# mapping each filepath to images in the directory
modified_train_set = modified_train_set.cast_column("images", datasets.Image())
#-----------------------------------------------------------------------------------------------------------------------------------------
print('\nExtracting Validation Set')
questionList_val, answerList_val, imgList_val = [],[],[]
print('\nExcluding Erroneous Indices')
# collecting the index containing errorneous images
indexToExcludeVal = []
with open('../text_files/error_validation.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExcludeVal.append(number)
with open('../text_files/error_validation2.txt', 'r') as file:
for line in file:
number = int(line.strip()) # Convert the read line to an integer
indexToExcludeVal.append(number)
# excluding the index containing errorneous images
for i in tqdm(range(len(val_json))):
if (i in indexToExcludeVal):
continue
pointer = val_json[i]
questionList_val.append(pointer['question'])
answerList_val.append(pointer['answer'])
imgList_val.append(pointer['image_id'])
print('\nThe size of the val Set is: ',len(questionList_val))
uniqueAnswerListVal = list(set(answerList_val))
#-----------------------------------------------------------------------------------------------------------------------------------------
print('\nProcessing Validation Data')
labels_val = []
for i in range(len(answerList_val)):
labels_val.append( mapping[ answerList_val[i] ] )
scores_val = []
for i in tqdm(range(len(answerList_val))):
s = [0] * (numOfClasses+1)
s[ mapping[ answerList_val[i]] ] = 1
scores_val.append(s)
imgPathList_val = []
filepath = '/home/aritra/cric/images/img/'
for i in tqdm(range(len(imgList_val))):
imgName = str(imgList_val[i]) + '.jpg'
concatedPath = os.path.join(filepath,imgName)
imgPathList_val.append(concatedPath)
# creating HF dataset to map images fast of Val_set
listToDictionary = {'questions':questionList_val, 'labels':labels_val, 'scores':scores_val, 'images':imgPathList_val}
modified_val_set = Dataset.from_dict(listToDictionary)
# mapping each filepath of Val Set to images in the directory
modified_val_set = modified_val_set.cast_column("images", datasets.Image())
#---------------------------------------------------------------------------------------------------------------------------------------
print('\nExtracting Test Data Set')
questionList_test, answerList_test, imgList_test = [],[],[]
print('\nExcluding Erroneous Indices')
# collecting the index containing errorneous images
indexToExcludeTest = []
with open('../text_files/error_testSet1.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExcludeTest.append(number)
with open('../text_files/errorTestSet2.txt', 'r') as file:
for line in file:
number = int(line.strip())
indexToExcludeTest.append(number)
# excluding the index containing errorneous images
for i in tqdm(range(len(test_json))):
if i in indexToExcludeTest:
continue
pointer = test_json[i]
questionList_test.append(pointer['question'])
answerList_test.append(pointer['answer'])
imgList_test.append(pointer['image_id'])
print('\nSize of the Test set is ', len(questionList_test))
#---------------------------------------------------------------------------------------------------------------------------------------
print('\nProcessing Test Data Set')
labels_test = []
for i in range(len(answerList_test)):
labels_test.append( mapping[ answerList_test[i] ] )
scores_test = []
for i in tqdm(range(len(answerList_test))):
s = [0] * (numOfClasses+1)
s[ mapping[ answerList_test[i]] ] = 1
scores_test.append(s)
imgPathList_test = []
filepath = '/home/aritra/cric/images/img/'
for i in tqdm(range(len(imgList_test))):
imgName = str(imgList_test[i]) + '.jpg'
concatedPath = os.path.join(filepath,imgName)
imgPathList_test.append(concatedPath)
# creating HF dataset to map images fast of test_set
listToDictionary = {'questions':questionList_test, 'labels':labels_test, 'scores':scores_test, 'images':imgPathList_test}
modified_test_set = Dataset.from_dict(listToDictionary)
# mapping each filepath of test Set to images in the directory
modified_test_set = modified_test_set.cast_column("images", datasets.Image())
print('\nEnd of all pre Processing')
#-----------------------------------------------------------------------------------------------------------------------------------------
print('\nImporting Transformer')
from transformers import ViltProcessor, ViltForQuestionAnswering
from transformers import ViltConfig
config = ViltConfig.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
device = "cuda" if torch.cuda.is_available() else "cpu"
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-mlm")
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-mlm", id2label = reverse_mapping, label2id = mapping).to(device)
#-----------------------------------------------------------------------------------------------------------------------------------------
print('\nClass Declaration')
class cric_dataset(Dataset):
def __init__(self, dataset, processor):
self.processor = processor
self.dataset = dataset
def __len__(self):
return len(self.dataset)
def __getitem__(self,idx):
#print(idx)
item = self.dataset[idx]
#print(item)
encodings = self.processor(images = item["images"], text = item["questions"], padding="max_length", truncation=True, return_tensors = "pt")
encodings = {k:v.squeeze() for k,v in encodings.items()}
encodings['labels'] = torch.tensor(item['scores'], dtype = torch.float32)
return encodings
#-----------------------------------------------------------------------------------------------------------------------------------------
train_dataset_object = cric_dataset(modified_train_set, processor)
val_dataset_object = cric_dataset(modified_val_set, processor)
test_dataset_object = cric_dataset(modified_test_set, processor)
train_dataloader = DataLoader(train_dataset_object, collate_fn = collate_fn, shuffle = True, batch_size = 32)
tot_number_of_steps = len(train_dataloader)
print('\nTotal number of steps: ',tot_number_of_steps)
#----------------------------------------------------------------------------------------------------------------------------------------
print('\nFinetuning Process Begins')
# writer = SummaryWriter()
optimizer = torch.optim.AdamW(model.parameters(), lr = 5e-5)
model.train()
for epoch in tqdm(range(8)):
print(f"Epoch: {epoch}")
for idx, batch in enumerate(train_dataloader):
batch = {k:v.to(device) for k,v in batch.items()}
optimizer.zero_grad()
outputs = model(**batch)
loss = outputs.loss
print(idx,"-> Loss:", loss.item())
loss.backward()
optimizer.step()
"""
# Plots in tensorboard
if (idx != 0) and (idx % 1500 == 0):
model.eval()
acc_score_val, validationLoss = calculateAccuracyVal()
acc_score_test = calculateAccuracyTest()
print(f'\nValidation Accuracy: {acc_score_val}\n')
writer.add_scalar('Training Loss', loss.item(), epoch * tot_number_of_steps + idx)
writer.add_scalar('Validation Loss', validationLoss, epoch * tot_number_of_steps + idx)
writer.add_scalar('Accuracy Score On Val Set', acc_score_val, epoch * tot_number_of_steps + idx)
writer.add_scalar('Accuracy Score On Test Set', acc_score_test, epoch * tot_number_of_steps + idx)
model.train()
"""
save_path = os.path.join('../model_chkpts/vilt_vanilla_finetuning/', 'vilt_vanilla_' + str(epoch+1))
model.save_pretrained(save_path)
print('\n\nTuned Model Saved at: ', epoch+1)
#writer.close()
print('\nFinetuning Ends')