-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare_features.py
More file actions
65 lines (57 loc) · 2.03 KB
/
Copy pathprepare_features.py
File metadata and controls
65 lines (57 loc) · 2.03 KB
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
"""
Reads in a tsv file with pre-trained bottom up attention features and
stores it in HDF5 format.
Hierarchy of HDF5 file:
[
image_id: {
'features': num_boxes x 2048 array of features
'bboxes': num_boxes x 4 array of bounding boxes
}
]
"""
import base64
import csv
import json
import os
import h5py
import numpy as np
from tqdm import tqdm
csv.field_size_limit(2**31 - 1)
FIELDNAMES = [
'image_id', 'image_w', 'image_h', 'num_boxes', 'boxes', 'features'
]
data_dir = 'data/'
trainval_dir = os.path.join(data_dir, 'trainval_36')
infile = os.path.join(trainval_dir,
'trainval_resnet101_faster_rcnn_genome_36.tsv')
feature_file = os.path.join(data_dir, 'image_features.h5')
indexmap = {}
feature_length = 2048
num_fixed_boxes = 36
num_images = 123287
if __name__ == '__main__':
h_feature = h5py.File(feature_file, "w")
features = h_feature.create_dataset('image_feature',
shape=(num_images, num_fixed_boxes,
feature_length),
dtype='<f4')
print("reading tsv...")
with open(infile, newline='') as tsv_in_file:
reader = csv.DictReader(tsv_in_file,
delimiter='\t',
fieldnames=FIELDNAMES)
image_count = 0
for item in tqdm(reader, total=123287):
num_boxes = int(item['num_boxes'])
image_id = str(item['image_id'])
image_feature = np.frombuffer(base64.b64decode(item['features']),
dtype=np.float32).reshape(
num_boxes, -1)
assert image_feature.shape == (num_fixed_boxes, feature_length)
indexmap[image_id] = image_count
features[image_count, :, :] = image_feature
image_count += 1
h_feature.close()
with open(os.path.join(data_dir, 'feature_map.json'), 'w') as fp:
json.dump(indexmap, fp)
print("done!")