forked from bupt-mmai/CNN-Caption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_prepare.py
executable file
·187 lines (150 loc) · 6.94 KB
/
data_prepare.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
# -*- coding: utf-8 -*-
import os
import pickle
import numpy as np
import json
data_dir = 'data'
origin_path = os.path.join(data_dir, 'paragraphs_v1.json')
img2sents_path = os.path.join(data_dir, 'img2sents.pkl')
img2dense_path = os.path.join(data_dir, 'img2dense.json')
img2onehot_path = os.path.join(data_dir, 'img2onehot.pkl')
# img2densevec_path = os.path.join(data_dir, 'img2dense_vec.pkl')
vocab_path = os.path.join(data_dir, 'vocab.pkl')
def get_img2sents(update_flag=False):
if os.path.exists(img2sents_path) and not update_flag:
return
origin_data = json.load(open(origin_path, 'r'))
img2paragraph = {}
for each_data in origin_data:
image_id = each_data['image_id']
paragraph = each_data['paragraph']
paragraph = paragraph.replace('t.v.', 'tv').replace('U.S.', 'US').replace('T.C.', 'TC').replace(
'C.E.T.', 'CET')
paragraph = paragraph.replace(' st.', ' st').replace(' ST.', ' ST').replace(' Mt. ', ' Mt ').replace(
' St.', ' St').replace(' Dept. ', ' Dept ')
paragraph = paragraph.replace(' S. ', ' st ').replace('welcomebackveterans.org.',
'welcomebackveterans')
paragraph = paragraph.replace('$1.00', '$1').replace('3.20', '320').replace('us.open.org',
'usopenorg')
paragraph = paragraph.replace('evil. ECC. IV.23', 'evilECCIV23').replace('neweracap.com',
'neweracapcom')
paragraph = paragraph.replace(' Baby toys. And boxes.', ' ').replace('$1.25', '$1').replace(
'.UMBRELLA.', 'UMBRELLA.')
paragraph = paragraph.replace('www.kiwirail.co.nz', 'wwwkiwirailconz').replace('28.41',
'2841').replace(
'http://www.tmz.com/', '')
paragraph = paragraph.replace(' Handle. ', ' Handle ').replace(' oz. ', ' oz ')
paragraph = paragraph.replace('CapeTreasures.com', 'CapeTreasurescom').replace('NW Meadow... DR.',
'DR')
paragraph = paragraph.replace('$.20', '$1').replace('www.theimpusilvebuy.com',
'wwwtheimpusilvebuycom')
paragraph = paragraph.replace('transavia.com', 'transaviacom').replace('XL.com', 'XLcom')
paragraph.replace(' .', '.')
paragraph.replace('. ', '.')
sentences = paragraph.split('.')
sentences = map(lambda sent: sent.strip(), sentences)
sentences = filter(lambda sent: len(sent) >= 2, sentences)
# if sentences!=[]:
# print sentences
# if '.cn' in paragraph:
# print paragraph
img2paragraph[image_id] = sentences
pickle.dump(img2paragraph, open(img2sents_path, 'wb'))
def get_vocab(word_count_threshold=5, update_flag=False):
if os.path.exists(vocab_path) and not update_flag:
return
img2para = pickle.load(open(img2sents_path, 'rb'))
all_sents = []
for key, para in img2para.items():
for each_sent in para:
each_sent = each_sent.replace(',', ' , ')
all_sents.append(each_sent)
print('preprocessing word counts and creating vocab based on word count threshold %d' % (word_count_threshold,))
word_counts = {}
nsents = 0
for sent in all_sents:
nsents += 1
tmp_sent = sent.lower().split(' ')
for w in tmp_sent:
if w != '' and w != ' ':
word_counts[w] = word_counts.get(w, 0) + 1
vocab = [w for w in word_counts if word_counts[w] >= word_count_threshold]
print('filtered words from %d to %d' % (len(word_counts), len(vocab)))
idx2word = {}
idx2word[0] = '<bos>'
idx2word[1] = '<eos>'
idx2word[2] = '<pad>'
idx2word[3] = '<unk>'
word2idx = {}
word2idx['<bos>'] = 0
word2idx['<eos>'] = 1
word2idx['<pad>'] = 2
word2idx['<unk>'] = 3
for idx, w in enumerate(vocab):
word2idx[w] = idx + 4
idx2word[idx + 4] = w
word_counts['<eos>'] = nsents
word_counts['<bos>'] = nsents
word_counts['<pad>'] = nsents
word_counts['<unk>'] = nsents
# bias_init_vector = np.array([1.0 * word_counts[ ixtoword[i] ] for i in ixtoword])
# bias_init_vector /= np.sum(bias_init_vector) # normalize to frequencies
# bias_init_vector = np.log(bias_init_vector)
# bias_init_vector -= np.max(bias_init_vector) # shift to nice numeric range
pickle.dump([word2idx, idx2word], open(vocab_path, 'wb'))
def get_one_hot(s_max, n_max, update_flag):
if os.path.exists(img2onehot_path) or not update_flag:
return
word2idx = pickle.load(open(vocab_path, 'rb'))[0]
img2para = pickle.load(open(img2sents_path, 'rb'))
img2para_vec = {}
for img, para in img2para.items():
num_sents = min(len(para), s_max)
sent_stop = np.zeros(s_max, dtype=int)
sent_stop[num_sents - 1:] = 1
paras_idx = np.ones([s_max, n_max + 1], dtype=int) * 2
for sent_id, sent in enumerate(para):
if sent_id == num_sents:
break
sent = sent.replace(',', ' , ')
sent = '<bos> ' + sent + ' <eos>'
word_count = 0
tmp_sent = sent.lower().split(' ')
tmp_sent = filter(lambda x: x != '' and x != ' ', tmp_sent)
for word_id, word in enumerate(tmp_sent):
if word_id == n_max + 1:
break
word_count += 1
if word in word2idx:
paras_idx[sent_id, word_id] = word2idx[word]
else:
paras_idx[sent_id, word_id] = word2idx['<unk>']
img2para_vec[str(img)] = [paras_idx, sent_stop]
pickle.dump(img2para_vec, open(img2onehot_path, 'wb'))
# def getDenseVec():
# img2dense = json.load(open(img2dense_path, 'r'))
# img2dense_vec = {}
# for img, captions in img2dense.items():
# dense_vec = np.ones((50, 6), dtype=int) * 2
# for i, caption in enumerate(captions):
# if i >= 50:
# break
# words = caption.split(' ')
# for j, word in enumerate(words):
# if j >= 6:
# break
# if word in word2idx:
# dense_vec[i, j] = word2idx[word]
# else:
# dense_vec[i, j] = word2idx['<unk>']
# img2dense_vec[img] = dense_vec
# with open(img2densevec_path, 'wb') as f:
# pickle.dump(img2dense_vec, f)
def run(update_flag):
get_img2sents(update_flag)
get_vocab(update_flag=update_flag)
get_one_hot(6, 30, update_flag=update_flag)
# getDenseVec()
print('Data preprocess done')
if __name__ == '__main__':
run(True)