-
Notifications
You must be signed in to change notification settings - Fork 8
/
coco_vocab.py
109 lines (90 loc) · 3.43 KB
/
coco_vocab.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
# Create a vocabulary wrapper
import nltk
import pickle
from collections import Counter
import json
import argparse
import os
from random import shuffle, seed
class Vocabulary(object):
"""Simple vocabulary wrapper."""
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.idx = 0
def add_word(self, word):
if word not in self.word2idx:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
def __call__(self, word):
if word not in self.word2idx:
return self.word2idx['<unk>']
return self.word2idx[word]
def __len__(self):
return len(self.word2idx)
def build_vocab(imgs):
count_thr = 5
# count up the number of words
counts = {}
for img in imgs:
for sent in img['sentences']:
for w in sent['tokens']:
counts[w] = counts.get(w, 0) + 1
cw = sorted([(count, w) for w, count in counts.items()], reverse=True)
print('top words and their counts:')
print('\n'.join(map(str, cw[:20])))
# print some stats
total_words = sum(counts.values())
print('total words:', total_words)
bad_words = [w for w, n in counts.items() if n <= count_thr]
words = [w for w, n in counts.items() if n > count_thr]
bad_count = sum(counts[w] for w in bad_words)
print('number of bad words: %d/%d = %.2f%%' % (len(bad_words), len(counts), len(bad_words) * 100.0 / len(counts)))
print('number of words in vocab would be %d' % (len(words),))
print('number of UNKs: %d/%d = %.2f%%' % (bad_count, total_words, bad_count * 100.0 / total_words))
# lets look at the distribution of lengths as well
sent_lengths = {}
for img in imgs:
for sent in img['sentences']:
txt = sent['tokens']
nw = len(txt)
sent_lengths[nw] = sent_lengths.get(nw, 0) + 1
max_len = max(sent_lengths.keys())
print('max length sentence in raw data: ', max_len)
print('sentence length distribution (count, number of words):')
sum_len = sum(sent_lengths.values())
for i in range(max_len + 1):
print('%2d: %10d %f%%' % (i, sent_lengths.get(i, 0), sent_lengths.get(i, 0) * 100.0 / sum_len))
# lets now produce the final annotations
if bad_count > 0:
# additional special UNK token we will use below to map infrequent words to
print('inserting the special UNK token')
words.append('<unk>')
for img in imgs:
img['final_captions'] = []
for sent in img['sentences']:
txt = sent['tokens']
caption = [w if counts.get(w, 0) > count_thr else 'UNK' for w in txt]
img['final_captions'].append(caption)
# Create a vocab wrapper and add some special tokens.
vocab = Vocabulary()
vocab.add_word('<end>')
# Add words to the vocabulary.
for i, word in enumerate(words):
vocab.add_word(word)
print(vocab.idx2word)
return vocab
def main(data_path):
imgs = json.load(open(data_path, 'r'))
imgs = imgs['images']
seed(123) # make reproducible
vocab = build_vocab(imgs)
with open('./vocab/mscoco_vocab.pkl', 'wb') as f:
pickle.dump(vocab, f, pickle.HIGHEST_PROTOCOL)
print("Saved vocabulary file ")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', default='data/dataset_coco.json')
opt = parser.parse_args()
main(opt.data_path)