-
Notifications
You must be signed in to change notification settings - Fork 17
/
create_sequencelabel.py
136 lines (104 loc) · 4.37 KB
/
create_sequencelabel.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
"""
Read caption from a json file
Save as h5 format
"""
import os
import json
import argparse
import h5py
import numpy as np
import string
from random import shuffle, seed
import logging
from datetime import datetime
from build_vocab import __PAD_TOKEN, __UNK_TOKEN, __BOS_TOKEN, __EOS_TOKEN
logger = logging.getLogger(__name__)
def encode_captions(videos, max_length, wtoi):
"""
encode all captions into one large array
"""
N = len(videos)
M = sum(len(v['final_captions'])
for v in videos) # total number of captions
label_arrays = []
# note: these will be one-indexed
label_start_ix = np.zeros(N, dtype=int)
label_end_ix = np.zeros(N, dtype=int)
label_length = np.zeros(M, dtype=int)
label_to_video = np.zeros(M, dtype=int)
counter = 0
for i, v in enumerate(videos):
n = len(v['final_captions'])
assert n > 0, 'error: some image has no captions'
# 0 is __PAD_TOKEN, implicitly
Li = np.zeros((n, max_length), dtype=int)
for j, s in enumerate(v['final_captions']):
label_length[counter + j] = min(max_length, len(s))
label_to_video[counter + j] = i
# truncated at max_length, thus the last token might be not the <eos>.
# any problem with this?
for k, w in enumerate(s):
if k < max_length:
Li[j, k] = wtoi[w]
label_arrays.append(Li)
label_start_ix[i] = counter
label_end_ix[i] = counter + n
counter += n
L = np.concatenate(label_arrays, axis=0) # put all the labels together
assert L.shape[0] == M, 'lengths don\'t match? that\'s weird'
# assert np.all(label_length > 0), 'error: some caption had no words?'
logger.info('encoded captions to array of size %s', repr(L.shape))
return L, label_start_ix, label_end_ix, label_length, label_to_video
def main(vocab_json, captions_json, output_h5, max_length):
# create the vocab
vocab = json.load(open(vocab_json))
# inverse table
wtoi = {w: i for i, w in enumerate(vocab)}
videos = json.load(open(captions_json))
logger.info('Select tokens in the vocab only')
for v in videos:
v['final_captions'] = []
for txt in v['processed_tokens']:
caption = [__BOS_TOKEN]
caption += [w if w in wtoi else __UNK_TOKEN for w in txt]
caption += [__EOS_TOKEN]
v['final_captions'].append(caption)
with h5py.File(output_h5, 'w') as of:
if len(videos[0]['captions']) > 0:
logger.info('Encoding captions...')
L, label_start_ix, label_end_ix, label_length, label_to_video = encode_captions(
videos, max_length, wtoi)
of.create_dataset('labels', dtype=int, data=L)
of.create_dataset('label_start_ix', dtype=int, data=label_start_ix)
of.create_dataset('label_end_ix', dtype=int, data=label_end_ix)
of.create_dataset('label_length', dtype=int, data=label_length)
of.create_dataset('label_to_video', dtype=int, data=label_to_video)
else:
logger.info('Caption not found! Skipped encoding captions.')
video_ids = [v['video_id'] for v in videos]
of['videos'] = np.array(video_ids, dtype=np.string_)
of['vocab'] = np.array(vocab, dtype=np.string_)
logger.info('Wrote to %s', output_h5)
######################################################################
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s:%(levelname)s: %(message)s')
parser = argparse.ArgumentParser()
parser.add_argument('vocab_json', default='_vocab.json',
help='vocab json file')
parser.add_argument('captions_json', default='_proprocessedtokens',
help='_proprocessedtokens json file')
parser.add_argument(
'output_h5',
default='_sequencelabel.h5',
help='output h5 file')
parser.add_argument(
'--max_length',
default=30,
type=int,
help='max length of a caption, in number of words. captions longer than this get clipped.')
args = parser.parse_args()
logger.info('Input parameters: %s', args)
start = datetime.now()
main(args.vocab_json, args.captions_json, args.output_h5, args.max_length)
logger.info('Time: %s', datetime.now() - start)