-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader_test.py
171 lines (136 loc) · 5.02 KB
/
dataloader_test.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
from torch.utils.data import Dataset
import torch
from PIL import Image
import os
from torch.utils.data import DataLoader
from transformers import CamembertTokenizer, FlaubertTokenizer, XLMRobertaTokenizer, BertTokenizer
import json
import pickle
class InputFeature(object):
"""
A single set of features
"""
def __init__(self, image_id, input_ids, input_mask, input_type_ids, image):
self.data = []
self.data.append(input_ids)
self.data.append(input_mask)
self.data.append(image_id)
self.data.append(image)
self.data.append(input_type_ids)
def __iter__(self):
return iter(self.data)
class TestLoader(DataLoader):
def __init__(self, data_set, shuffle=False, device="cuda", batch_size=16):
super(TestLoader, self).__init__(
dataset=data_set,
collate_fn=self.collate_fn,
shuffle=shuffle,
batch_size=batch_size
)
self.device = device
def collate_fn(self, data):
res = []
token_ml = max(map(lambda x_: sum(x_.data[1]), data))
for sample in data:
example = []
for x in sample:
if isinstance(x, list):
x = x[:token_ml]
example.append(x)
res.append(example)
res_ = []
for idx, x in enumerate(zip(*res)):
if isinstance(x[0], list):
res_.append(torch.LongTensor(x))
elif isinstance(x[0], str):
res_.append(torch.LongTensor([int(values) for values in x]))
else:
res_.append(torch.stack([value for value in x]))
# return [t.to(self.device) for t in res_]
return {'image':res_[3], 'input_id':res_[0], 'input_mask': res_[1], 'image_id':res_[2] ,'input_type_ids':res_[4]}
class VQADatasetTest(Dataset):
"""VQA Dataset"""
def __init__(self, data, img_dir, transform,tokenizer,config):
"""
:param data:
:param img_dir:
:param transform:
:param tokenizer:
:param config:
"""
self.data = data
self.images_dir = img_dir
# Image transforms
self.transform = transform
self.config = config
self.tokenizer = tokenizer
@classmethod
def create(cls, data_file,
image_dir,
transform,
pad_idx =0,
tokenizer = None,
model_type = None,
min_char_len = 1,
max_seq_length=510,
model_name = "camembert-base",
clear_cache = False,
is_cls = True):
if tokenizer is None:
if 'camem' in model_type:
tokenizer = CamembertTokenizer.from_pretrained(model_name)
elif 'flaubert' in model_type:
tokenizer = FlaubertTokenizer.from_pretrained(model_name)
elif 'XLMRoberta' in model_type:
tokenizer = XLMRobertaTokenizer.from_pretrained(model_name)
elif 'M-Bert' in model_type:
tokenizer = BertTokenizer.from_pretrained(model_name)
with open(data_file, 'rb') as f:
data = pickle.load(f)
# idx2labels, labels2idx = cls.create_labels(labels_path)
config = {
"min_char_len": min_char_len,
"model_name": model_name,
"max_sequence_length": max_seq_length,
"clear_cache": clear_cache,
"pad_idx": pad_idx,
"is_cls": is_cls,
}
self = cls(data,image_dir,transform,tokenizer,config)
return self
@classmethod
def create_labels(cls,labels_path):
with open(labels_path,'r') as f:
idx2labels = json.load(f)
labels2idx = dict((values,keys) for (keys,values) in idx2labels.items())
return idx2labels, labels2idx
def create_features(self,item):
# bert_tokens = []
# tok_map = []
img_path = os.path.join(self.images_dir, item[1])
cur_tokens = self.tokenizer.tokenize(item[2][:self.config["max_sequence_length"]])
image_id = item[0]
# cur_label = self.config['labels2idx'][cur_label]
orig_tokens = cur_tokens
input_ids = self.tokenizer.encode(orig_tokens)
input_mask = [1] * len(input_ids)
while len(input_ids) < self.config["max_sequence_length"]:
input_ids.append(self.config["pad_idx"])
input_mask.append(0)
input_type_ids = [0] * len(input_ids)
# img_path = os.path.join(self.images_dir, item[1])
image = Image.open(img_path).convert('RGB')
image = self.transform(image)
return InputFeature(
input_ids=input_ids,
input_mask=input_mask,
image_id=image_id,
image=image,
input_type_ids=input_type_ids
)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
# Parse the text file line
data_item = self.data[idx]
return self.create_features(data_item)