forked from monologg/NER-Multimodal-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
303 lines (241 loc) · 11.1 KB
/
data_loader.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
import os
import copy
import json
import logging
import numpy as np
import torch
from torch.utils.data import TensorDataset
from utils import load_vocab, preprocess_word
logger = logging.getLogger(__name__)
class InputExample(object):
def __init__(self, guid, img_id, words, labels):
self.guid = guid # int
self.img_id = img_id # int
self.words = words # list
self.labels = labels # list
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, word_ids, char_ids, img_feature, mask, label_ids):
self.word_ids = word_ids
self.char_ids = char_ids
self.img_feature = img_feature
self.mask = mask
self.label_ids = label_ids
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
class TweetProcessor(object):
"""Processor for the Tweet data set """
def __init__(self, args):
self.args = args
@classmethod
def get_labels(cls):
return ["[pad]", "[unk]", "O", "B-PER", "I-PER", "B-LOC", "I-LOC", "B-ORG", "I-ORG", "B-OTHER", "I-OTHER"]
@classmethod
def get_label_vocab(cls):
label_vocab = dict()
for idx, label in enumerate(cls.get_labels()):
label_vocab[label] = idx
return label_vocab
def load_img_features(self):
return torch.load(os.path.join(self.args.data_dir, self.args.img_feature_file))
@classmethod
def _read_file(cls, input_file):
"""Read tsv file, and return words and label as list"""
with open(input_file, "r", encoding="utf-8") as f:
sentences = []
sentence = [[], []] # [[words], [tags], img_id]
for line in f:
if line.strip() == "":
continue
if line.startswith("IMGID:"):
if sentence[0]:
sentences.append(sentence)
sentence = [[], []] # Flush
# Add img_id at last
img_id = int(line.replace("IMGID:", "").strip())
sentence.append(img_id)
else:
try:
word, tag = line.strip().split("\t")
word = preprocess_word(word)
sentence[0].append(word)
sentence[1].append(tag)
except:
logger.info("\"{}\" cannot be splitted".format(line.rstrip()))
# Flush the last one
if sentence[0]:
sentences.append(sentence)
return sentences
def _create_examples(self, sentences, set_type):
"""Creates examples for the training dev, and test sets."""
examples = []
for (i, sentence) in enumerate(sentences):
words, labels, img_id = sentence[0], sentence[1], sentence[2]
assert len(words) == len(labels)
guid = "%s-%s" % (set_type, i)
if i % 10000 == 0:
logger.info(sentence)
examples.append(InputExample(guid=guid, img_id=img_id, words=words, labels=labels))
return examples
def get_examples(self, mode):
"""
:param mode: train, dev, test
"""
file_to_read = None
if mode == 'train':
file_to_read = self.args.train_file
elif mode == 'dev':
file_to_read = self.args.dev_file
elif mode == 'test':
file_to_read = self.args.test_file
logger.info("LOOKING AT {}".format(os.path.join(self.args.data_dir, file_to_read)))
return self._create_examples(self._read_file(os.path.join(self.args.data_dir, file_to_read)), mode)
def load_word_matrix(args, word_vocab):
if not os.path.exists(args.wordvec_dir):
os.mkdir(args.wordvec_dir)
# Making new word vector
logger.info("Building word matrix...")
embedding_index = dict()
with open(os.path.join(args.wordvec_dir, args.w2v_file), 'r', encoding='utf-8') as f:
for line in f:
line = line.rstrip()
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embedding_index[word] = coefs
word_matrix = np.zeros((args.word_vocab_size, args.word_emb_dim), dtype='float32')
cnt = 0
for word, i in word_vocab.items():
embedding_vector = embedding_index.get(word)
if embedding_vector is not None:
word_matrix[i] = embedding_vector
else:
word_matrix[i] = np.random.uniform(-0.25, 0.25, args.word_emb_dim)
cnt += 1
logger.info('{} words not in pretrained matrix'.format(cnt))
word_matrix = torch.from_numpy(word_matrix)
return word_matrix
def convert_examples_to_features(examples,
img_features,
max_seq_len,
max_word_len,
word_vocab,
char_vocab,
label_vocab,
pad_token="[pad]",
unk_token="[unk]"):
features = []
for (ex_index, example) in enumerate(examples):
if ex_index % 5000 == 0:
logger.info("Writing example %d of %d" % (ex_index, len(examples)))
# 1. Load img feature
try:
img_feature = img_features[example.img_id]
except:
logger.warning("Cannot load image feature! (IMGID: {})".format(example.img_id))
continue
# 2. Convert tokens to idx & Padding
word_pad_idx, char_pad_idx, label_pad_idx = word_vocab[pad_token], char_vocab[pad_token], label_vocab[pad_token]
word_unk_idx, char_unk_idx, label_unk_idx = word_vocab[unk_token], char_vocab[unk_token], label_vocab[unk_token]
word_ids = []
char_ids = []
label_ids = []
for word in example.words:
word_ids.append(word_vocab.get(word, word_unk_idx))
ch_in_word = []
for char in word:
ch_in_word.append(char_vocab.get(char, char_unk_idx))
# Padding for char
char_padding_length = max_word_len - len(ch_in_word)
ch_in_word = ch_in_word + ([char_pad_idx] * char_padding_length)
ch_in_word = ch_in_word[:max_word_len]
char_ids.append(ch_in_word)
for label in example.labels:
label_ids.append(label_vocab.get(label, label_unk_idx))
mask = [1] * len(word_ids)
# Padding for word and label
word_padding_length = max_seq_len - len(word_ids)
word_ids = word_ids + ([word_pad_idx] * word_padding_length)
label_ids = label_ids + ([label_pad_idx] * word_padding_length)
mask = mask + ([0] * word_padding_length)
word_ids = word_ids[:max_seq_len]
label_ids = label_ids[:max_seq_len]
char_ids = char_ids[:max_seq_len]
mask = mask[:max_seq_len]
# Additional padding for char if word_padding_length > 0
if word_padding_length > 0:
for i in range(word_padding_length):
char_ids.append([char_pad_idx] * max_word_len)
# 3. Verify
assert len(word_ids) == max_seq_len, "Error with word_ids length {} vs {}".format(len(word_ids), max_seq_len)
assert len(char_ids) == max_seq_len, "Error with char_ids length {} vs {}".format(len(char_ids), max_seq_len)
assert len(label_ids) == max_seq_len, "Error with label_ids length {} vs {}".format(len(label_ids), max_seq_len)
assert len(mask) == max_seq_len, "Error with mask length {} vs {}".format(len(mask), max_seq_len)
if ex_index < 5:
logger.info("*** Example ***")
logger.info("guid: %s" % example.guid)
logger.info("img_id: %s" % example.img_id)
logger.info("words: %s" % " ".join([str(x) for x in example.words]))
logger.info("word_ids: %s" % " ".join([str(x) for x in word_ids]))
logger.info("char_ids[0]: %s" % " ".join([str(x) for x in char_ids[0]]))
logger.info("mask: %s" % " ".join([str(x) for x in mask]))
logger.info("label_ids: %s" % " ".join([str(x) for x in label_ids]))
features.append(
InputFeatures(word_ids=word_ids,
char_ids=char_ids,
img_feature=img_feature,
mask=mask,
label_ids=label_ids
))
return features
def load_data(args, mode):
processor = TweetProcessor(args)
# Load data features from dataset file
logger.info("Creating features from dataset file at %s", args.data_dir)
if mode == "train":
examples = processor.get_examples("train")
elif mode == "dev":
examples = processor.get_examples("dev")
elif mode == "test":
examples = processor.get_examples("test")
else:
raise Exception("For mode, Only train, dev, test is available")
word_vocab, char_vocab, _, _ = load_vocab(args)
label_vocab = processor.get_label_vocab()
# Use cross entropy ignore index as padding label id so that only real label ids contribute to the loss later
features = convert_examples_to_features(examples,
processor.load_img_features(),
args.max_seq_len,
args.max_word_len,
word_vocab,
char_vocab,
label_vocab)
# Convert to Tensors and build dataset
all_word_ids = torch.tensor([f.word_ids for f in features], dtype=torch.long)
all_char_ids = torch.tensor([f.char_ids for f in features], dtype=torch.long)
all_img_feature = torch.stack([f.img_feature for f in features])
all_mask = torch.tensor([f.mask for f in features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_ids for f in features], dtype=torch.long)
logger.info("all_word_ids.size(): {}".format(all_word_ids.size()))
logger.info("all_char_ids.size(): {}".format(all_char_ids.size()))
logger.info("all_img_feature.size(): {}".format(all_img_feature.size()))
logger.info("all_mask.size(): {}".format(all_mask.size()))
logger.info("all_label_ids.size(): {}".format(all_label_ids.size()))
dataset = TensorDataset(all_word_ids, all_char_ids, all_img_feature, all_mask, all_label_ids)
return dataset