forked from ryankiros/visual-semantic-embedding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_abstract.py
267 lines (227 loc) · 10.5 KB
/
parse_abstract.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
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
# Code to prepare data for visual semantic embeddings
# Ramakrishna Vedantam
# *****************************************************************************
# Notes:
# *****************************************************************************
# MNLM requires:
# > Text file with a caption on each line
# > npy file with row i containing image features
# Experiment with different kinds of image and features
# Image features include:
# 1. Detection features: Only object occurrence and position
# 2. Pose + Detection features: Object pose and occurence/location
# 3. Pose + Detection + Semantic: Object expression etc.
# 4. Relative Location: Most likely a spatial pyramid of occurences?
# 5. FC7 VGG 19: FC7 features from the VGG 19 model
import json
# argument parser
import os
import sys
import argparse
# python debugger
import pdb
import numpy as np
import random
import h5py
# Python Image Library
from PIL import Image
try:
import cPickle as pickle
except:
import pickle
from collections import defaultdict
from nltk.tokenize import word_tokenize
from tqdm import *
import demo
# for clipart feature extraction
import abstract_features as af # TODO: change, not good for debugging
def imname(prefix, index):
return prefix + "%012d" % (index) + '.png'
def jsonname(prefix, index):
return prefix + "%012d" % (index) + '.json'
def compute_mean(image_folder, orig_split, indices):
prefix = 'abstract_v002_%s2015_' % (orig_split)
folder = os.path.join(image_folder, 'scene_img', 'img_%s2015' % (orig_split))
mean_image = np.zeros((400,700,3), dtype=np.float32)
# compute image mean
for index, item in enumerate(indices):
image_name = os.path.join(folder, imname(prefix, item))
# remove alpha channel
image = np.array(Image.open(image_name), dtype=np.float32)[:,:,:-1]
mean_image += image
mean_image /= float(len(indices))
mean = np.mean(np.mean(mean_image, axis=0), axis=0)
return mean
def parse_captions(json_file):
caps = defaultdict(list)
with open(json_file, 'r') as annfile:
anns = json.load(annfile)['annotations']
for item in anns:
caps[item['image_id']].append(' '.join(word_tokenize(item['caption'])))
return caps
def get_image_feat(feat_type, image_folder, orig_split, indices, real_split):
feats = defaultdict(int)
prefix = 'abstract_v002_%s2015_' % (orig_split)
if 'fc7' in feat_type:
# set some parameters
folder = os.path.join(image_folder, 'scene_img', 'img_%s2015' % (orig_split)) + '/'
print "Preparing the VGG 19 Net"
net = demo.build_convnet()
print "Extracting Features"
with open('temp_{}.txt'.format(orig_split), 'w') as image_file:
for item in tqdm(indices):
image_file.write(imname(prefix, item) + '\n')
image_file.close()
feats = demo.compute_fromfile(net, 'temp_{}.txt'.format(orig_split),
base_path=folder)
elif 'hdf5' in feat_type:
try:
folder = os.path.join(image_folder, 'scene_img', 'img_%s2015' % (orig_split)) + '/'
images = np.zeros((len(indices), 3, 224, 224)) # TODO: Low Priority, make general
for index, item in tqdm(enumerate(indices)):
images[index] = demo.load_abstract_image(folder + imname(prefix, item))
with h5py.File('/ssd_local/rama/datasets/abstract-hdf5/{}.h5'.format(real_split), 'w') as outfile:
outfile['images'] = images
return True
except:
print "problem"
return False
else:
folder = os.path.join(image_folder, 'scene_json', 'scene_%s2015_indv' % (orig_split))
# create the abstract feature instance
AF = pickle.load(open('extract_features/af_dump.p', 'r'))
# TODO: Figure out a better place to initialize all this
out_dir = '/srv/share/vqa/release_data/abstract_v002/scene_json/features_v2/'
keep_or_remove = 'keep'
get_names = False
tags = feat_type
# path to metafeature directory
metafeat_dir = af.dir_path(os.path.join(out_dir, 'metafeatures'))
for item in tqdm(indices):
metafeat_fn = '{}_instances-{}.cpickle'.format(item,
AF.instance_ordering)
cur_metafeat_fn = os.path.join(metafeat_dir,
metafeat_fn)
with open(cur_metafeat_fn, 'rb') as fp:
cur_metafeats = pickle.load(fp)
cur_feats, _ = AF.scene_metafeatures_to_features(cur_metafeats,
tags,
keep_or_remove,
get_names)
feats[item] = cur_feats
return feats
def write_vse_input(outfile, captions, feats, sp):
"""
outfile: str (Path to directory where data is to be written, in datasets/)
captions: dict of dict of list (captions for train, dev and test splits)
feats: dict of dict of np.array (features for train, dev and test images)
"""
caption_fname = open(os.path.join('/ssd_local/rama/datasets', outfile, outfile + '_' + sp + '_caps.txt'), 'w')
im_fname = os.path.join('/ssd_local/rama/datasets', outfile, outfile + '_' + sp + '_ims.npy')
cap_key = sp + '_captions'
feat_key = sp + '_feats'
# image list file
image_features = np.float32(np.zeros([len(feats[feat_key].keys())*len(captions[cap_key].values()[0])
, len(feats[feat_key].values()[0])]))
fptr = 0
for key in captions[cap_key]:
for item in captions[cap_key][key]:
asc_item = item.encode('ascii', 'replace')
if asc_item != item:
asc_item = asc_item.replace('?', ' ')
caption_fname.write(asc_item + '\n')
image_features[fptr, :] = np.float32(feats[feat_key][key])
fptr += 1
assert (fptr == image_features.shape[0])
np.save(im_fname, image_features)
del(image_features)
caption_fname.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create dataset to give as inpu\
t the Visual Semantic Embedding (VSE)')
parser.add_argument('--imfeat', default='fc7', help='What image features do \
you want to use?')
parser.add_argument('--imdir',
default='/srv/share/vqa/release_data/abstract_v002/',
help='Name of the directory which contains the \
img_train2015 and img_val2015 folders')
parser.add_argument('--splits', default='train-dev-test', help="What splits \
do you want to run feature extraction on?")
parser.add_argument('--caption_dir', default='../datasets', help='Name of \
the directory which has the caption files in VQA format')
parser.add_argument('--seed', default=123, help='Select random seed for \
splitting val into dev and test')
args = parser.parse_args()
out_dir = 'abstract-' + args.imfeat
chk_dir= os.path.join('/ssd_local/rama/datasets', out_dir)
# Check if output directory needs to be created
if not os.path.exists(chk_dir):
os.mkdir(chk_dir)
#
if args.imfeat != 'all':
imfeat = set(args.imfeat.split('-'))
else:
imfeat = ('instance-level', 'category-general')
# setup
random.seed(args.seed)
feats = {}
captions = {}
# parse the sentences in train and val sets of abstract scenes respectively
train_path = os.path.join(args.caption_dir,
'captions_abstract_v002_train2015.json')
val_path = os.path.join(args.caption_dir,
'captions_abstract_v002_val2015.json')
captions['train_captions'] = parse_captions(train_path)
val_captions = parse_captions(val_path)
all_splits = {}
# create the train, val and dev splits
all_splits['train_split'] = set(captions['train_captions'].keys())
val_keys = val_captions.keys()
# shuffle entries randomly
random.shuffle(val_keys)
# slit half into test and half into dev
all_splits['dev_split'] = set(val_keys[0:len(val_keys)/2])
all_splits['test_split'] = set(val_keys[len(val_keys)/2:-1])
assert(len(all_splits['dev_split'].intersection(
all_splits['test_split'])) == 0)
splits = args.splits.split('-')
# for testing - use just one element of splits
#all_splits['train_split'] = set([list(all_splits['train_split'])[0]])
#all_splits['dev_split'] = set([list(all_splits['dev_split'])[0]])
#all_splits['test_split'] = set([list(all_splits['test_split'])[0]])
if 'hdf5' in imfeat:
for sp in splits:
print "Writing {} HDF5 Files".format(sp)
if 'dev' == sp or 'test' == sp:
orig_split = 'val'
else:
orig_split = 'train'
if get_image_feat(imfeat, args.imdir, orig_split, all_splits['{}_split'.format(sp)], real_split=sp):
print "Successfully created HDF5"
else:
print "Failed to create HDF5"
sys.exit()
captions['dev_captions'] = {k: v for k, v in val_captions.iteritems()\
if k in all_splits['dev_split']}
captions['test_captions'] = {k: v for k, v in val_captions.iteritems()
if k in all_splits['test_split']}
captions['train_captions'] = {k: v for k, v in
captions['train_captions'].iteritems()
if k in all_splits['train_split']}
# feats.p stores the vgg 19 features w/o mean correction
# with open('feats.p', 'r') as readfile:
# feats = pickle.load(readfile)
# decide what image features to extract
# extract image features for dev_split
for sp in splits:
print "Extracting {} Set Features".format(sp)
if 'dev' == sp or 'test' == sp:
orig_split = 'val'
else:
orig_split = 'train'
feats['{}_feats'.format(sp)] = get_image_feat(imfeat, args.imdir,
orig_split,
all_splits['{}_split'.format(sp)], sp)
print "Writing the caption and image files"
# put the captions and images in to text files and npy files respectively
write_vse_input(out_dir, captions, feats, sp)