-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_annotations.py
233 lines (203 loc) · 8.46 KB
/
process_annotations.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
import json, string, os, sys
from utils_ import init_config
# function that replaces tokens with replacements, so as to match tokens in embeddings
def replace_problematic_words(toklist, replacements):
for w in replacements:
for i,t in enumerate(toklist):
if t == w:
toklist = [*toklist[:i] , * replacements[w].split(), *toklist[i+1 :] ]
# print("Replaced %s with %s" % (w,toklist[i : i + len(replacements[w].split())]))
return toklist
# read caption data from file
def read_file(filename, format):
print("Reading file ",filename)
img_captions = None
print("File format is %s." % format)
# read Coco caption data
if format == "coco":
with open(filename,'r') as f:
print("Loading json data.")
data = json.load(f)
print("Reading data.")
img_captions = {}
img_filenames = {}
# read image ids and associate them with their captions
for annot_item in data['annotations']:
image_id = annot_item['image_id']
caption = annot_item['caption']
if not image_id in img_captions:
img_captions[image_id] = []
img_captions[image_id].append(caption)
# also read the image file name
for image_dict in data['images']:
image_file = image_dict['file_name']
image_id = image_dict['id']
img_filenames[image_id] = image_file
# read flickr30 caption data
elif format == "flickr":
lines = []
with open(filename,"r") as f:
for line in f:
lines.append(line.strip())
print("Reading data.")
img_captions = {}
img_filenames = {}
for line in lines:
img,caption = line.split("\t")
name, numcaption = img.split("#")
if name not in img_captions:
img_captions[name] = []
img_captions[name].append(caption)
img_filenames[name] = name
# combine captions per image
print("Generating json object.")
image_jsons = []
for id in img_captions:
obj = {}
obj["id"] = id
obj["filename"] = img_filenames[id]
obj["raw_captions"] = []
for cap in img_captions[id]:
obj["raw_captions"].append(cap)
image_jsons.append(obj)
with open(filename + ".per_image.json", "w") as fp:
json.dump(image_jsons, fp)
return image_jsons
# preprocess the captions, removing punctuation and applying token replacement if needed
def prepro_captions(imgs_json, vocab_replacement_file):
translator = str.maketrans('', '', string.punctuation)
for i, img in enumerate(imgs_json):
img['processed_tokens'] = []
for j, s in enumerate(img['raw_captions']):
txt = str(s).lower().translate(translator).strip().split()
img['processed_tokens'].append(txt)
if vocab_replacement_file is not None:
# read replacements file
replacements = {}
with open(vocab_replacement_file, "r") as f:
for line in f:
tokens = line.strip().split("\t")
tokens = [ tok.strip() for tok in tokens if len(tok.strip()) > 0]
word = tokens[0]
replacements[word] = " ".join(tokens[1:])
for i, img in enumerate(imgs_json):
for t, txt in enumerate(img['processed_tokens']):
txt = replace_problematic_words(txt, replacements)
imgs_json[i]['processed_tokens'][t] = txt
# construct the vocabulary from the input captions
def build_vocab(imgs, word_count_thresh):
if word_count_thresh is not None:
vocab = apply_frequency_filtering(imgs, word_count_thresh)
else:
vocab = set()
for img in imgs:
for txt in img['processed_tokens']:
for w in txt:
vocab.add(w)
vocab = list(vocab)
return vocab
# produce a frequenccy-filtered vocabulary
def apply_frequency_filtering(imgs, count_threshold ):
print("Counting tokens...")
counts = {}
for img in imgs:
for txt in img['processed_tokens']:
for w in txt:
if w not in counts:
counts[w] = 1
else:
counts[w] = counts[w] + 1
cw = sorted([(count, w) for w, count in counts.items()], reverse=True)
# print top words
num_top = 10
print('\ntop %d words and their counts:' % num_top)
print('\n'.join(map(str, cw[:num_top])))
print()
vocab = [w for w, n in counts.items() if n > count_threshold]
# print some stats
total_words = len(counts.items())
bad_words = [w for w, n in counts.items() if n <= count_threshold]
print('total words:', total_words)
print('number of non-frequent words to-be-mapped to UNK: %d/%d = %.2f%%' % (
len(bad_words), len(counts), len(bad_words) * 100.0 / len(counts)))
print('number of words in vocab: %d' % (len(vocab)))
return vocab
# map captions to vocabulary tokens
def finalize_captions(img_list, captions_field, vocab, caption_max_length):
for img in img_list:
img['final_captions'] = []
# map to vocabulary and potentially truncate
for raw_word_list in img[captions_field]:
vocab_words = [w if w in vocab else 'UNK' for w in raw_word_list]
if caption_max_length is not None and len(vocab_words) > caption_max_length:
trunc_vocab_words= vocab_words[0:caption_max_length]
print("Limiting caption of size %d :" % len(vocab_words), str(vocab_words), " to ", str(trunc_vocab_words))
vocab_words = trunc_vocab_words
img['final_captions'].append(vocab_words)
# read the vocab
def read_vocabulary(vocab_file):
# read vocabulary
print("Reading vocabulary from ",vocab_file)
vocab = {}
count = 0
with open(vocab_file, "r") as f:
for line in f:
if not line or len(line) == 0:
continue
vocab[line.strip()] = count
count = count + 1
print("Read a %d-word vocabulary." % len(vocab))
return vocab
def main():
keyvals = init_config(init_file, "captions")
for key in keyvals:
exec("%s=%s" % (key, keyvals[key]), )
print("Successfully initialized from file %s" % init_file)
# read caption files
print (caption_file_formats)
image_jsons = []
for i,c in enumerate(caption_files):
image_jsons.append(read_file(c, caption_file_formats[i]))
for i,c in enumerate(image_jsons):
print('Processing tokens of %s.' % (caption_files[i]))
prepro_captions(c, vocab_replacement_file)
# if no vocabulary specified, build it
if vocabulary_file is None:
img_json = []
for obj in image_jsons:
img_json.extend(obj)
vocab = build_vocab(img_json, word_count_thresh)
# add EOS, BOS
vocab.extend(["UNK","EOS","BOS"])
# write to the folder of the first caption file
filename = os.path.join(os.path.dirname(caption_files[0]),
"_".join([ os.path.basename(capfile) for capfile in caption_files]))
filename = filename + ".vocab"
print("Produced vocabulary of", len(vocab)," words, including the UNK, EOS, BOS symbols.")
print("Writing vocabulary to",filename )
with open(filename, "w") as f:
for w in vocab:
f.write(w + "\n")
else:
# encode captions to an existing vocabulary
vocab = read_vocabulary(vocabulary_file)
for i in range(len(caption_files)):
filename = caption_files[i]
imgjson = image_jsons[i]
print("Mapping captions of ", filename, " to the vocabulary")
finalize_captions(imgjson,"processed_tokens",vocab, caption_max_length)
# write
with open(filename + ".paths.txt","w") as f:
for image_obj in imgjson:
imgname = image_obj["filename"]
for cap in image_obj['final_captions']:
labels = []
for word in cap:
if word not in vocab:
print("Word",word,"not found in vocabulary.")
exit(1)
labels.append(str(vocab[word]))
f.write("%s %s\n" % (imgname, " ".join(labels)))
print("Wrote file ",filename + ".paths.txt")
if __name__ == "__main__":
main()