File size: 23,593 Bytes
44ddffd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!user/bin/env python
# -*- coding:utf-8 -*-
import collections
import json


import string

import numpy as np
from model_LXM2T5 import T5tokenizer, LXMT52T5, LXMtokenizer
import pickle
import torch
from torch.utils.data import Dataset

from config4LXMT5_DDP import args
print('dataset4T5',args)
from random import sample


def normalize_wiki(s):
    stopwords=['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]


    def white_space_fix(text):
        return ' '.join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    def remove_stop_w(text):
        to_be_removed = set(stopwords)
        text_list = text.split(' ')
        text_list = [item for item in text_list if item not in to_be_removed]
        return ' '.join(text_list)

    return white_space_fix(remove_stop_w(remove_punc(lower(s))))



if args.dataset == 'okvqa':
    with open('../data/image_features/vqa_img_feature_train.pickle', 'rb') as f:
        pretrain_feature = pickle.load(f)
    if args.pretrain:
        with open('../data/pretrain/vqa_train_filter.json','r') as f:
            vqa2 = json.load(f)
        train_row = vqa2
    else:
        with open('../data/finetune/okvqa_train.json','r') as f:
            train_row = json.load(f)

    if args.pretrain:
        with open('../data/pretrain/caption_predict_vqav2train.json', 'r') as f:
            captions_train = json.load(f)
        with open('../data/pretrain/labeling_predict_vqav2train.json', 'r') as f:
            labelings_train = json.load(f)
        with open('../data/pretrain/ocr_predict_vqav2train.json', 'r') as f:
            ocrs_train = json.load(f)

        with open('../data/pretrain/wiki_100sim_train.json', 'r') as f:
            wikis_train = json.load(f)

    else:
        with open('../data/finetune/caption_predict_train.json', 'r') as f:
            captions_train = json.load(f)
        with open('../data/finetune/labeling_predict_train.json', 'r') as f:
            labelings_train = json.load(f)
        with open('../data/finetune/ocr_predict_train.json', 'r') as f:
            ocrs_train = json.load(f)
        if args.ofa=="normal":
            with open('../data/finetune/ofa_predictions/OFA_zerorate_predict_train.json', 'r') as f:
                ofas_train = json.load(f)#key为数字
            with open('../data/finetune/ofa_predictions/OFA_zerorate_evidence_train.json', 'r') as f:
                evid_train = json.load(f)#key为字符串
        elif args.ofa=="finetune":
            with open('../data/finetune/ofa_predictions/OFAvqa_zerorate_answer_train.json', 'r') as f:
                ofas_train = json.load(f)#key为字符串
            with open('../data/finetune/ofa_predictions/OFAvqa_zerorate_evidence_train.json', 'r') as f:
                evid_train = json.load(f)#key为字符串
        else:
            assert 0==1
        with open("../data/finetune/gpt3_okvqa_train2014_answers.pkl", 'rb') as f:
            gpt3_train = pickle.load(f)
        with open('../data/finetune/wiki_100sim_train.json', 'r') as f:
            wikis_train = json.load(f)

else:
    assert 0==1



def plural(word):
    if word.endswith('y'):
        return word[:-1] + 'ies'
    elif word[-1] in 'sxo' or word[-2:] in ['sh', 'ch']:
        return word + 'es'
    elif word.endswith('an'):
        return word[:-2] + 'en'
    else:
        return word + 's'

image_ids = []
qids = []
questions = []
answers = []
labels = []
objects = []
answer_ids = []
answers_lists = []
question_lengths = []
answers_most = []
neg_answer = []


train_captions = {}
for item in captions_train:
    if item['image_id'] in train_captions.keys():
        print("IMG caption REPEATED!")
        assert 0==1
    train_captions[item['image_id']] = item['caption']

train_labelings = {}
for item in labelings_train:
    if item['image_id'] in train_labelings.keys():
        print("IMG labelings REPEATED!")
        assert 0==1
    train_labelings[str(item['image_id'])] = item['labeling']
print("labeling number:", len(train_labelings.keys()))

train_ocrs = {}
for item in ocrs_train:
    if item['image_id'] in train_ocrs.keys():
        print("IMG ocrs REPEATED!")
        assert 0==1
    train_ocrs[str(item['image_id'])] = item['ocr']


if not args.pretrain:
    train_ofas = {}
    if args.ofa=="normal":
        for item in ofas_train:
            if item['question_id'] in train_ofas.keys():
                print("IMG ofas REPEATED!")
                assert 0==1
            train_ofas[str(item['question_id'])] = item['OFA_answer']+", "+evid_train[str(item['question_id'])]
    elif  args.ofa=="finetune":
        for k in evid_train.keys():
            train_ofas[k] = ofas_train[k]+", "+evid_train[k]
    else:
        assert 0==1

    train_gpt3 = {}
    for k in gpt3_train.keys():
        qid = k.split("#")[1]
    
        train_gpt3[str(qid)] = ", ".join(gpt3_train[k][0])#[(ans, evid)]


train_wikis = wikis_train


if args.pretrain: 
    if args.num_wiki > 51:
        for key in train_wikis.keys():
            for i in range(args.num_wiki):
                train_wikis[key][i]=normalize_wiki(train_wikis[key][i])




n = 0


for qid, item in train_row.items():
    img_id = str(item['image_id'])
    image_ids.append(img_id)
    qids.append(qid)
    question_clean = item['question']# + answer_sentence
    questions.append(question_clean)
 


    # multi-answer
    if args.dataset == 'okvqa':
        answers.append(item['multi_answers'])
        # m_ans_id = [a_dic.get(i, 0) for i in item['multi_answers']]
        # most_answer_ids.append(m_ans_id)


    #single answer
    else:
        answers.append(item['answer'])




def _create_gpt3_entry(imgage_ids, q_ids, questions, answer, captions,labelings, ocrs,ofas, gpt3, wikis,final_txt):

    if not args.pretrain:
        entry = {
            'img_id': imgage_ids,
            'qid': q_ids,
            'question': questions,
            'answer': answer,
            'caption': captions,
            'labeling':labelings,
            'ocr': ocrs,
            'ofa':ofas,
            'gpt3':gpt3,
            'wiki':wikis,
            'final_txt':final_txt}

    return entry



def _create_entry(imgage_ids, q_ids, questions, answer, captions,labelings, ocrs,ofas, wikis,final_txt):
    if not args.pretrain:
        entry = {
            'img_id': imgage_ids,
            'qid': q_ids,
            'question': questions,
            'answer': answer,
            'caption': captions,
            'labeling':labelings,
            'ocr': ocrs,
            'ofa':ofas,
            'wiki':wikis,
            'final_txt':final_txt}
    return entry


def _create_vqav2_entry(imgage_ids, q_ids, questions, answer, captions,labelings, ocrs,wikis,final_txt):
    if args.pretrain:
        entry = {
            'img_id': imgage_ids,
            'qid': q_ids,
            'question': questions,
            'answer': answer,
            'caption': captions,
            'labeling':labelings,
            'ocr': ocrs,
            'wiki':wikis,
            'final_txt':final_txt}
    # else:
    return entry


def _load_dataset(train_row):
    entries=[]
    for qid, item in train_row.items():
        qid = str(qid)
        img_id = str(item['image_id'])
        question = item['question']
 


        # multi-answer
        if args.dataset == 'okvqa':
            answers=item['multi_answers']
            

        #single answer
        else:
            answers=item['answer']
            
        caption=train_captions[img_id]
        labeling=train_labelings[img_id]
        ocr_list=train_ocrs[img_id]
        ocr = ", ".join(str(i) for i in ocr_list)
        if not args.pretrain:
            ofa=train_ofas[qid]
            gpt3=train_gpt3[qid]
        wiki=train_wikis[qid]
        
        if args.pretrain:
            if args.num_wiki > 51:
                final_txt = [question + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
            else:
                final_txt = [question + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
        else:
            if args.seed > 1000:
                print("seed > 1000 denotes that ablation study on 2 encoders")
                assert args.input_type==0
            if args.gpt3:
                if args.input_type==0:
                
                    if args.num_wiki > 51: 
			# When there are a large number of Wiki passages, to save on GPU memory usage, Wiki passages are processed.
                        final_txt = [question + " [SEP] " + ofa + " " + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + normalize_wiki(x) for x in wiki[:args.num_wiki]]
                    else:
                        final_txt = [question + " [SEP] " + ofa + " " + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
                elif args.input_type==1: 
                    final_txt = question + " [SEP] " + ofa + " " + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr
                elif args.input_type==2:
                    if args.num_wiki > 51:
                        final_txt = [question + " [SEP] "  + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + normalize_wiki(x) for x in wiki[:args.num_wiki]]
                    else:
                        final_txt = [question + " [SEP] "  + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
                elif args.input_type==3: 
                    final_txt = question + " [SEP] " + gpt3 + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr
                else:
                    print('choose input-type in [0,1,2,3]')
                    assert 0==1
                
                
            else:
                if args.input_type==0:
                
                    if args.num_wiki > 51:
                        final_txt = [question + " [SEP] " + ofa + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + normalize_wiki(x) for x in wiki[:args.num_wiki]]
                    else:
                        final_txt = [question + " [SEP] " + ofa + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
                elif args.input_type==1: 
                    final_txt = question + " [SEP] " + ofa + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr
                elif args.input_type==2: 
                    if args.num_wiki > 51:
                        final_txt = [question + " [SEP] "  + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + normalize_wiki(x) for x in wiki[:args.num_wiki]]
                    else:
                        final_txt = [question + " [SEP] "  + caption + " [SEP] " + labeling + " [SEP] " + ocr + " [SEP] " + x for x in wiki[:args.num_wiki]]
                elif args.input_type==3:
                    final_txt = question + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr
                else:
                    print('choose input-type in [0,1,2,3,4,5]')
                    assert 0==1
                
                
                
        
        if args.pretrain:
            entries.append(_create_vqav2_entry(img_id, qid, question, answers, caption,labeling, ocr, wiki, final_txt))
        else:
            if args.gpt3:
                entries.append(_create_gpt3_entry(img_id, qid, question, answers, caption,labeling, ocr,ofa,gpt3, wiki, final_txt))
            else:
                entries.append(_create_entry(img_id, qid, question, answers, caption,labeling, ocr,ofa, wiki, final_txt))

    return entries





def _create_pretrain_entry(imgage_ids, q_ids, questions, answer):#, captions,labelings, ocrs,ofas,final_txt):
    entry = {
        'img_id': imgage_ids,
        'qid': q_ids,
        'question': questions,
        'answer': answer}#,
    return entry

def _load_pretrain_dataset(train_row):
    entries=[]
    for qid, item in train_row.items():
        qid = str(qid)

        img_id = str(item['image_id'])
        question = item['question']


        # multi-answer
        if args.dataset == 'okvqa':
            answers=item['multi_answers']
            # answers.append(item['multi_answers'])
            # m_ans_id = [a_dic.get(i, 0) for i in item['multi_answers']]
            # most_answer_ids.append(m_ans_id)


        #single answer
        else:
            answers=item['answer']
        
        entries.append(_create_pretrain_entry(img_id, qid, question, answers))
    return entries



class KgDataset(Dataset):
    def __init__(self, val=False, val_test=False):
        self.entries = _load_dataset(train_row)
        self.tokenize()

    def __len__(self):
        return len(self.entries)
    def tokenize(self):
        if args.input_type==0:
            if args.num_wiki > 51:
                max_source_length=200
            else:
                max_source_length=250 #300
        else:
            max_source_length=128
        max_target_length=5
        max_que_length=16
        for entry in self.entries:
            T5_input_seq, T5_input_ids, T5_input_masks = self.tokenizer_func( T5tokenizer, entry['final_txt'], max_length=max_source_length)
            LXM_input_seq, LXM_input_ids, LXM_input_masks = self.tokenizer_func( LXMtokenizer, entry['question'], max_length=max_que_length)
            
            
            all_Ans_T5_target_seq = []
            all_Ans_T5_target_ids = []
            all_Ans_T5_target_masks = []
            if args.allAns:
                for i in range(10):  
                    if i%2==0:
                        T5_target_seq, T5_target_ids, T5_target_masks = self.tokenizer_func( T5tokenizer, entry['answer'][i], max_length=max_target_length)
                        all_Ans_T5_target_seq.append(T5_target_seq)
                        all_Ans_T5_target_ids.append(torch.from_numpy(np.array(T5_target_ids)))
                        all_Ans_T5_target_masks.append(torch.from_numpy(np.array(T5_target_masks)))
                        # print()
                all_Ans_T5_target_ids=torch.stack(all_Ans_T5_target_ids)
                all_Ans_T5_target_masks=torch.stack(all_Ans_T5_target_masks)
                
                entry['T5_target_seq']=all_Ans_T5_target_seq
                entry['T5_target_ids']=all_Ans_T5_target_ids
                entry['T5_target_masks']=all_Ans_T5_target_masks

            else:
                T5_target_seq, T5_target_ids, T5_target_masks = self.tokenizer_func( T5tokenizer, entry['answer'][0], max_length=max_target_length)
                entry['T5_target_seq']=T5_target_seq#torch.from_numpy(np.array(T5_target_seq))
                entry['T5_target_ids']=torch.from_numpy(np.array(T5_target_ids))
                entry['T5_target_masks']=torch.from_numpy(np.array(T5_target_masks))
            entry['T5_input_seq']=T5_input_seq#torch.from_numpy(np.array(T5_input_seq))
            entry['T5_input_ids']=torch.from_numpy(np.array(T5_input_ids))
            entry['T5_input_masks']=torch.from_numpy(np.array(T5_input_masks))
            entry['LXM_input_seq']=LXM_input_seq#torch.from_numpy(np.array(LXM_input_seq))
            entry['LXM_input_ids']=torch.from_numpy(np.array(LXM_input_ids))
            entry['LXM_input_masks']=torch.from_numpy(np.array(LXM_input_masks))
            


    def tokenizer_func(self, tokenizer, text, max_length=0):
        if max_length==0:
            print('plz set the max length of input sequence!')
            assert 1==2
       
        out_seq = tokenizer(
            text,
            # batch_data['final_txt'],
            padding='max_length',
            max_length=max_length,
            truncation=True,
            # return_tensors="pt",
            )
        
        tokens=out_seq.input_ids #['input_ids']
        masks=out_seq.attention_mask
        length = len(tokens)

        return out_seq, tokens, masks

    def __getitem__(self, index):

        entry = self.entries[index]
        qid=entry['qid']
        question=entry['question']
        answer=entry['answer']
        img_id=entry['img_id']
        image_feature = pretrain_feature[img_id]['feats']
        
        image_caption = entry['caption']
        image_labeling = entry['labeling']
        image_ocr_list = entry['ocr']
        image_ocr = ", ".join(str(i) for i in image_ocr_list)
        if not args.pretrain:
            ofa = entry['ofa'] 
            if args.gpt3:
                gpt3 = entry['gpt3'] 
        wiki = entry['wiki']
        final_txt = entry['final_txt']
        
        
        spatial_feature = pretrain_feature[img_id]['sp_feats']
        
        T5_input_seq, T5_input_ids, T5_input_masks = entry['T5_input_seq'], entry['T5_input_ids'], entry['T5_input_masks']#self.tokenizer_func( T5tokenizer, final_txt, max_length=max_source_length)
        
        LXM_input_seq, LXM_input_ids, LXM_input_masks = entry['LXM_input_seq'], entry['LXM_input_ids'], entry['LXM_input_masks']
        
        LXM_token_type_ids = torch.from_numpy(np.array(LXM_input_seq['token_type_ids']))#.to(device)
        
        T5_target_seq, T5_target_ids, T5_target_masks=entry['T5_target_seq'],entry['T5_target_ids'],entry['T5_target_masks']
        
        if not args.pretrain:
            if not args.gpt3:
                return qid, question, answer, image_feature, spatial_feature, image_caption, image_labeling, image_ocr, ofa, wiki, final_txt, T5_input_seq,T5_input_ids,T5_input_masks,LXM_input_ids,LXM_input_masks,LXM_token_type_ids,T5_target_seq,T5_target_ids,T5_target_masks  
            elif args.gpt3:
                return qid, question, answer, image_feature, spatial_feature, image_caption, image_labeling, image_ocr, ofa, gpt3, wiki, final_txt, T5_input_seq,T5_input_ids,T5_input_masks,LXM_input_ids,LXM_input_masks,LXM_token_type_ids,T5_target_seq,T5_target_ids,T5_target_masks  
        else:
            return qid, question, answer, image_feature, spatial_feature, image_caption, image_labeling, image_ocr, wiki, final_txt, T5_input_seq,T5_input_ids,T5_input_masks,LXM_input_ids,LXM_input_masks,LXM_token_type_ids,T5_target_seq,T5_target_ids,T5_target_masks  

def my_collate(batch):
    batch = list(zip(*batch))
    if not args.pretrain:
        if not args.gpt3:
            res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                    'img': batch[3], 'spatial': batch[4],
                    'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'ofa': batch[8], 'wiki': batch[9], 'final_txt': batch[10],
                    'T5_input_seq': batch[11], 'T5_input_ids': batch[12],'T5_input_masks': batch[13],'LXM_input_ids':batch[14], 'LXM_input_masks':batch[15], 'LXM_token_type_ids':batch[16], 'T5_target_seq':batch[17],'T5_target_ids':batch[18],'T5_target_masks':batch[19]}
        elif args.gpt3:
            res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                    'img': batch[3], 'spatial': batch[4],
                    'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'ofa': batch[8], 'gpt3': batch[9], 'wiki': batch[10], 'final_txt': batch[11],
                    'T5_input_seq': batch[12], 'T5_input_ids': batch[13],'T5_input_masks': batch[14],'LXM_input_ids':batch[15], 'LXM_input_masks':batch[16], 'LXM_token_type_ids':batch[17], 'T5_target_seq':batch[18],'T5_target_ids':batch[19],'T5_target_masks':batch[20]}


    else:
        res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                'img': batch[3], 'spatial': batch[4],
                'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'wiki': batch[8], 'final_txt': batch[9],
                'T5_input_seq': batch[10], 'T5_input_ids': batch[11],'T5_input_masks': batch[12],'LXM_input_ids':batch[13], 'LXM_input_masks':batch[14], 'LXM_token_type_ids':batch[15], 'T5_target_seq':batch[16],'T5_target_ids':batch[17],'T5_target_masks':batch[18]}

            
    del batch
    return res

def my_val_collate(batch):
    batch = list(zip(*batch))
    if 1:
        res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                'img': batch[3], 'spatial': batch[4],
                'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'ofa': batch[8], 'wiki': batch[9], 'final_txt': batch[10],
                'T5_input_seq': batch[11], 'T5_input_ids': batch[12],'T5_input_masks': batch[13],'LXM_input_ids':batch[14], 'LXM_input_masks':batch[15], 'LXM_token_type_ids':batch[16], 'T5_target_seq':batch[17],'T5_target_ids':batch[18],'T5_target_masks':batch[19]}
    del batch
    return res





def my_gpt3_collate(batch):
    batch = list(zip(*batch))
    if 1:
        res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                'img': batch[3], 'spatial': batch[4],
                'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'ofa': batch[8],'gpt3': batch[9], 'wiki': batch[10], 'final_txt': batch[11],
                'T5_input_seq': batch[12], 'T5_input_ids': batch[13],'T5_input_masks': batch[14],'LXM_input_ids':batch[15], 'LXM_input_masks':batch[16], 'LXM_token_type_ids':batch[17], 'T5_target_seq':batch[18],'T5_target_ids':batch[19],'T5_target_masks':batch[20]}
    del batch
    return res

def my_val_gpt3_collate(batch):
    batch = list(zip(*batch))
    if 1:
        res = {'id': batch[0], 'ques': batch[1], 'ans': batch[2],
                'img': batch[3], 'spatial': batch[4],
                'caption': batch[5], 'labeling': batch[6], 'ocr': batch[7], 'ofa': batch[8],'gpt3': batch[9], 'wiki': batch[10], 'final_txt': batch[11],
                'T5_input_seq': batch[12], 'T5_input_ids': batch[13],'T5_input_masks': batch[14],'LXM_input_ids':batch[15], 'LXM_input_masks':batch[16], 'LXM_token_type_ids':batch[17], 'T5_target_seq':batch[18],'T5_target_ids':batch[19],'T5_target_masks':batch[20]}
    del batch
    return res