-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_vcr_tfrecord.py
More file actions
341 lines (268 loc) · 11.8 KB
/
create_vcr_tfrecord.py
File metadata and controls
341 lines (268 loc) · 11.8 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import json
from absl import app
from absl import flags
from absl import logging
import hashlib
import io
import zipfile
import numpy as np
import PIL.Image
import tensorflow as tf
from bert import tokenization
flags.DEFINE_string('bert_vocab_file',
'data/bert/tf1.x/cased_L-12_H-768_A-12/vocab.txt',
'Path to the Bert vocabulary file.')
flags.DEFINE_boolean('do_lower_case', False,
'To be passed to the bert tokenizer.')
flags.DEFINE_string('annotations_jsonl_file', 'data/vcr1annots/val.jsonl',
'Path to the annotations file in jsonl format.')
flags.DEFINE_integer('num_shards', 10,
'Number of shards of the output tfrecord files.')
flags.DEFINE_integer('shard_id', 0, 'Shard id of the current process.')
flags.DEFINE_string('output_tfrecord_path', 'output/val.record',
'Path to the output tfrecord files.')
flags.DEFINE_string('image_zip_file', 'data/vcr1images.zip',
'Path to the zip file of images.')
flags.DEFINE_boolean('only_use_relevant_dets', False,
'If true, only use relevant detections.')
FLAGS = flags.FLAGS
# GENDER_NEUTRAL_NAMES = [
# 'Casey', 'Riley', 'Jessie', 'Jackie', 'Avery', 'Jaime', 'Peyton', 'Kerry',
# 'Jody', 'Kendall', 'Peyton', 'Skyler', 'Frankie', 'Pat', 'Quinn'
# ]
NUM_CHOICES = 4
GENDER_NEUTRAL_NAMES = [
'Casey', 'Riley', 'Jessie', 'Jackie', 'Avery', 'Jaime', 'Peyton', 'Kerry',
'Kendall', 'Frankie', 'Pat', 'Quinn'
]
MASKING_OFFSET = 10000
_NUM_PARTITIONS = 100
def get_partition_id(annot_id, num_partitions=_NUM_PARTITIONS):
split, number = annot_id.split('-')
return int(number) % num_partitions
def _load_annotations(filename):
"""Loads annotations from file.
Args:
filename: Path to the jsonl annotations file.
Returns:
A list of python dictionary, each is parsed from a json object.
"""
with tf.io.gfile.GFile(filename, 'r') as f:
return [json.loads(x.strip('\n')) for x in f]
def _fix_tokenization(tokenized_sent, obj_to_type, old_det_to_new_ind,
bert_tokenizer, do_lower_case):
"""Converts tokenized annotations into tokenized sentence.
Args:
tokenized_sent: Tokenized sentence with detections collapsed to a list.
obj_to_type: [person, person, pottedplant] indexed by the labels.
old_det_to_new_ind: A mapping from the old indices to the new indices.
Returns:
tokenized_sent: A list of string tokens.
"""
case_fn = lambda x: x.lower() if do_lower_case else x
new_tokenization_with_tags = []
for tok in tokenized_sent:
if isinstance(tok, list):
for idx in tok:
mask_flag = False
if idx >= MASKING_OFFSET:
mask_flag = True
idx -= MASKING_OFFSET
if old_det_to_new_ind is not None:
idx = old_det_to_new_ind[idx]
obj_type = obj_to_type[idx]
text_to_use = GENDER_NEUTRAL_NAMES[
idx %
len(GENDER_NEUTRAL_NAMES)] if obj_type == 'person' else obj_type
if mask_flag:
new_tokenization_with_tags.append(('[MASK]', idx))
else:
new_tokenization_with_tags.append((case_fn(text_to_use), idx))
else:
# new_tokenization_with_tags.append((case_fn(tok), -1))
# yek@: disable wordpiece tokenizer
if tok == '[MASK]':
new_tokenization_with_tags.append((tok, -1))
else:
for sub_tok in bert_tokenizer.wordpiece_tokenizer.tokenize(case_fn(tok)):
new_tokenization_with_tags.append((sub_tok, -1))
tokenized_sent, tags = zip(*new_tokenization_with_tags)
return list(tokenized_sent), list(tags)
def get_detections_to_use(obj_to_type, tokens_mixed_with_tags):
"""Gets the detections to use, filtering out objects that are not mentioned.
Args:
obj_to_type: A list of object names.
tokens_mixed_with_tags: Sentences that refer to the object list.
Returns:
indices: Indices in the obj_to_type, denoting the mentioned objects.
"""
detections_to_use = np.zeros(len(obj_to_type), dtype=bool)
people = np.array([x == 'person' for x in obj_to_type], dtype=bool)
for sentence in tokens_mixed_with_tags:
for possibly_det_list in sentence:
if isinstance(possibly_det_list, list):
for tag in possibly_det_list:
assert 0 <= tag < len(obj_to_type)
detections_to_use[tag] = True
elif possibly_det_list.lower() in ['everyone', 'everyones']:
detections_to_use |= people
if not detections_to_use.any():
detections_to_use |= people
detections_to_use = np.where(detections_to_use)[0]
old_det_to_new_ind = np.zeros(len(obj_to_type), dtype=np.int32) - 1
old_det_to_new_ind[detections_to_use] = np.arange(detections_to_use.shape[0],
dtype=np.int32)
return detections_to_use, old_det_to_new_ind
def _create_tf_example(encoded_jpeg, annot, meta, bert_tokenizer, do_lower_case,
only_use_relevant_dets, desired_size=400):
"""Creates an example from the annotation.
Args:
encoded_jpeg: A python string, the encoded jpeg data.
annot: A python dictionary parsed from the json object.
meta: A python dictionary containing object information.
bert_tokenizer: A tokenization.FullTokenizer object.
do_lower_case: If true, convert text to lower case.
Returns:
tf_example: A tf.train.Example proto.
"""
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(
value=[value.encode('utf8')]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _bytes_feature_list(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(
value=[x.encode('utf8') for x in value]))
def _int64_feature_list(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def _float_feature_list(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
# Encode objects and boxes.
image_height, image_width = meta['height'], meta['width']
assert (meta['names'] == annot['objects']
), 'Meta data does not match the annotation.'
obj_to_type = annot['objects']
question = annot['question']
answer_choices = annot['answer_choices']
rationale_choices = annot['rationale_choices']
assert NUM_CHOICES == len(answer_choices) == len(rationale_choices)
obj_to_type = np.array(obj_to_type)
boxes = np.array(meta['boxes'])
old_det_to_new_ind = None
if only_use_relevant_dets:
detections_to_use, old_det_to_new_ind = get_detections_to_use(
obj_to_type, [question] + answer_choices + rationale_choices)
old_det_to_new_ind = [-1 if x < 0 else x + 1 for x in old_det_to_new_ind]
# Gather elements using the new indices.
obj_to_type = obj_to_type[detections_to_use]
boxes = boxes[detections_to_use]
else:
old_det_to_new_ind = np.arange(len(obj_to_type), dtype=np.int32) + 1
# Add [0, 0, height, width] as for the full image.
obj_to_type = np.concatenate([['[unused400]'], obj_to_type], 0)
boxes = np.concatenate([[[0, 0, image_width, image_height, 1]], boxes], 0)
xmin, ymin, xmax, ymax, score = [boxes[:, i] for i in range(5)]
xmin /= image_width
ymin /= image_height
xmax /= image_width
ymax /= image_height
feature = {}
for key, value in annot.items():
if isinstance(value, int):
feature[key] = _int64_feature(value)
elif isinstance(value, str):
feature[key] = _bytes_feature(value)
feature['image/object/bbox/xmin'] = _float_feature_list(xmin.tolist())
feature['image/object/bbox/ymin'] = _float_feature_list(ymin.tolist())
feature['image/object/bbox/xmax'] = _float_feature_list(xmax.tolist())
feature['image/object/bbox/ymax'] = _float_feature_list(ymax.tolist())
feature['image/object/bbox/score'] = _float_feature_list(score.tolist())
feature['image/object/bbox/label'] = _bytes_feature_list(obj_to_type.tolist())
# Encode jpeg data, resize image if specified.
image = PIL.Image.open(io.BytesIO(encoded_jpeg))
assert image.format == 'JPEG'
min_size = min(image.width, image.height)
scale = 1.0 * desired_size / min_size
if scale < 1.0:
new_height, new_width = (int(image.height * scale),
int(image.width * scale))
image = image.resize((new_width, new_height))
with io.BytesIO() as f:
image.save(f, format="JPEG")
encoded_jpeg = f.getvalue()
feature['image/format'] = _bytes_feature('jpeg')
feature['image/encoded'] = tf.train.Feature(bytes_list=tf.train.BytesList(
value=[encoded_jpeg]))
# Encode question.
question_tokens, question_tags = _fix_tokenization(question, obj_to_type,
old_det_to_new_ind,
bert_tokenizer,
do_lower_case)
feature['question'] = _bytes_feature_list(question_tokens)
feature['question_tag'] = _int64_feature_list(question_tags)
# Encode answer choices.
for idx, tokenized_sent in enumerate(answer_choices):
tokens, tags = _fix_tokenization(tokenized_sent, obj_to_type,
old_det_to_new_ind, bert_tokenizer,
do_lower_case)
feature['answer_choice_%i' % (idx + 1)] = _bytes_feature_list(tokens)
feature['answer_choice_tag_%i' % (idx + 1)] = _int64_feature_list(tags)
# Encode ratinale choices.
for idx, tokenized_sent in enumerate(rationale_choices):
tokens, tags = _fix_tokenization(tokenized_sent, obj_to_type,
old_det_to_new_ind, bert_tokenizer,
do_lower_case)
feature['rationale_choice_%i' % (idx + 1)] = _bytes_feature_list(tokens)
feature['rationale_choice_tag_%i' % (idx + 1)] = _int64_feature_list(tags)
tf_example = tf.train.Example(features=tf.train.Features(feature=feature))
return tf_example
def main(_):
logging.set_verbosity(logging.INFO)
# Create Bert model.
bert_tokenizer = tokenization.FullTokenizer(vocab_file=FLAGS.bert_vocab_file,
do_lower_case=FLAGS.do_lower_case)
# Load annotations.
annots = _load_annotations(FLAGS.annotations_jsonl_file)
logging.info('Loaded %i annotations.', len(annots))
shard_id, num_shards = FLAGS.shard_id, FLAGS.num_shards
assert 0 <= shard_id < num_shards
writer = tf.io.TFRecordWriter(FLAGS.output_tfrecord_path + '-%05d-of-%05d' %
(shard_id, num_shards))
with zipfile.ZipFile(FLAGS.image_zip_file) as image_zip:
for idx, annot in enumerate(annots):
if (idx + 1) % 1000 == 0:
logging.info('On example %i/%i.', idx + 1, len(annots))
annot_id = int(annot['annot_id'].split('-')[-1])
if annot_id % num_shards != shard_id:
continue
# Read meta data.
meta_fn = os.path.join('vcr1images', annot['metadata_fn'])
try:
with image_zip.open(meta_fn, 'r') as f:
meta = json.load(f)
except Exception as ex:
logging.warn('Skip %s.', meta_fn)
continue
# Read image data.
img_fn = os.path.join('vcr1images', annot['img_fn'])
try:
with image_zip.open(img_fn, 'r') as f:
encoded_jpeg = f.read()
except Exception as ex:
logging.warn('Skip %s.', img_fn)
continue
# Create TF example.
tf_example = _create_tf_example(encoded_jpeg, annot, meta, bert_tokenizer,
FLAGS.do_lower_case,
FLAGS.only_use_relevant_dets)
writer.write(tf_example.SerializeToString())
writer.close()
logging.info('Done')
if __name__ == '__main__':
app.run(main)