File size: 14,751 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
#!user/bin/env python
# -*- coding:utf-8 -*-
import collections
import pickle
from model_LXM2T5 import T5tokenizer, LXMT52T5, LXMtokenizer

from torch.utils.data import Dataset
import json
import pickle
import numpy as np
import torch
import string


from config4LXMT5_DDP import args
print('dataset_val4T5',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 remove_articles(text):
    #     return regex.sub(r'\b(a|an|the)\b', ' ', text)

    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/validate/okvqa_val.json','r') as f:
        val_row = json.load(f)
    with open('../data/image_features/vqa_img_feature_val.pickle', 'rb') as f:
        pretrain_feature = pickle.load(f)
    with open('../data/validate/caption_predict_val.json', 'r') as f:
        captions_val = json.load(f)
    with open('../data/validate/labeling_predict_val.json', 'r') as f:
        labelings_val = json.load(f)
    with open('../data/validate/ocr_predict_val.json', 'r') as f:
        ocrs_val = json.load(f)
    
    if args.ofa=="normal":
        with open('../data/validate/ofa_predictions/OFA_zerorate_predict_val.json', 'r') as f:
            ofas_val = json.load(f)
        with open('../data/validate/ofa_predictions/OFA_zerorate_evidence_val.json', 'r') as f:
            evid_val = json.load(f)
    elif args.ofa=="finetune":
        with open('../data/validate/ofa_predictions/OFAvqa_zerorate_answer_val.json', 'r') as f:
            ofas_val = json.load(f)
        with open('../data/validate/ofa_predictions/OFAvqa_zerorate_evidence_val.json', 'r') as f:
            evid_val = json.load(f)
    else:
        assert 0==1
    with open("../data/validate/gpt3_okvqa_val2014_answers.pkl", 'rb') as f:
        gpt3_val = pickle.load(f)
    with open('../data/validate/wiki_100sim_val.json', 'r') as f:
        wikis_val = json.load(f)
    

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 = []
most_answer = []
neg_answer = []

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

val_labelings = {}
for item in labelings_val:
    if item['image_id'] in val_labelings.keys():
        print("IMG labelings REPEATED!")
        assert 0==1
    val_labelings[str(item['image_id'])] = item['labeling']

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

val_ofas = {}

if args.ofa=="normal":
    for item in ofas_val:
        if item['question_id'] in val_ofas.keys():
            print("IMG ofas REPEATED!")
            assert 0==1
        val_ofas[str(item['question_id'])] = item['OFA_answer']+", "+evid_val[str(item['question_id'])]
elif  args.ofa=="finetune":
    for k in evid_val.keys():
        val_ofas[k] = ofas_val[k]+", "+evid_val[k]
else:
    assert 0==1



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


val_wikis = wikis_val


for qid, item in val_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)
    if args.dataset == 'okvqa' or args.dataset == 'vqav2':
        answers.append(item['multi_answers'])
        if args.dataset == 'okvqa':
            objects.append(item['label'])
    else:
        answers.append(item['answer'])



def _create_gpt3_entry(imgage_ids, q_ids, questions, answer, captions,labelings, ocrs,ofas,gpt3, wikis, final_txt):
    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):
    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 _load_dataset(val_row):
    entries=[]
    for qid, item in val_row.items():
        qid = str(qid)
        img_id = str(item['image_id'])
        question = item['question']# + answer_sentence

        if args.dataset == 'okvqa':
            answers=item['multi_answers']


        else:
            answers=item['answer']
        caption=val_captions[img_id]
        labeling=val_labelings[img_id]
        ocr_list=val_ocrs[img_id]
        ocr = ", ".join(str(i) for i in ocr_list)
        ofa=val_ofas[qid]
        gpt3=val_gpt3[qid]
        wiki=val_wikis[qid]

        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:
                    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   

            entries.append(_create_gpt3_entry(img_id, qid, question, answers, caption,labeling, ocr,ofa,gpt3, wiki, final_txt))
            
        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: #什么知识都不加。知识单独的性能4(不要预训练):什么知识都不加,只有视觉属性。
                final_txt = question + " [SEP] " + ofa + " [SEP] " + caption + " [SEP] " + labeling + " [SEP] " + ocr
            else:
                print('choose input-type in [1,2,3,4,5]')
                assert 0==1              

            entries.append(_create_entry(img_id, qid, question, answers, caption,labeling, ocr,ofa, wiki, final_txt))
    return entries






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


    def __len__(self):
        return len(self.entries)
    def tokenize(self):
        if args.input_type%2==0 : #当input_type=0或者2的时候,有wiki在,所以句子长度要长
            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)
            T5_target_seq, T5_target_ids, T5_target_masks = self.tokenizer_func( T5tokenizer, entry['answer'][0], max_length=max_target_length)
            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))
            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))

    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,
            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)
        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 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  
        elif 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