-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare_input_classifier.py
More file actions
145 lines (119 loc) · 5.32 KB
/
Copy pathprepare_input_classifier.py
File metadata and controls
145 lines (119 loc) · 5.32 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
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
import json
import os
from loguru import logger
from tqdm import tqdm
data_dir = 'data/'
classifier_dir = os.path.join(data_dir, 'classifier')
dataset = 'coco'
coco_dir = os.path.join(data_dir, 'annotations_trainval2014/annotations')
coco_train_filename = 'instances_train2014.json'
coco_val_filename = 'instances_val2014.json'
split_path = os.path.join(data_dir, 'caption_datasets', 'dataset_coco.json')
feature_map_path = os.path.join(data_dir, 'feature_map.json')
def prepare_input_classifier():
assert dataset in {'coco'}
with open(os.path.join(coco_dir, coco_train_filename)) as fp:
detection_data_train = json.load(fp)
with open(os.path.join(coco_dir, coco_val_filename)) as fp:
detection_data_val = json.load(fp)
with open(split_path) as fp:
split_data = json.load(fp)
with open(feature_map_path, 'r') as fp:
feature_map = json.load(fp)
categories = []
category_indices = {}
for c in detection_data_train['categories']:
category_indices[c['id']] = len(categories)
categories.append(c['name'])
assert len(categories) == 80
if not os.path.exists(classifier_dir):
os.mkdir(classifier_dir)
with open(os.path.join(classifier_dir, 'category_names.json'), 'w') as fp:
json.dump(categories, fp)
label_detection = {}
category_count_detection = {}
for category in categories:
category_count_detection[category] = 0
for annotation in tqdm(detection_data_train['annotations'] + \
detection_data_val['annotations']):
image_id = str(annotation['image_id'])
if not image_id in feature_map:
logger.warning('Not found in features! id: {0}'.format(image_id))
else:
if image_id in label_detection:
label_detection[image_id].append(annotation['category_id'])
else:
label_detection[image_id] = [annotation['category_id']]
for image in label_detection:
label = label_detection[image]
label_detection[image] = [0 for _ in range(len(categories))]
for label in label:
label_detection[image][category_indices[label]] = 1
category_count_detection[categories[category_indices[label]]] += 1
label_detection_splited = {'train': [], 'val': [], 'test': []}
label_caption_splited = {'train': [], 'val': [], 'test': []}
filemap = {}
category_count_caption = {}
for category in categories:
category_count_caption[category] = 0
for item in tqdm(split_data['images']):
image_id = str(item['cocoid'])
if not image_id in feature_map:
logger.warning('Not found in features! id: {0}'.format(image_id))
else:
split = item['split']
if split == 'restval':
split = 'train'
assert split in ['train', 'val', 'test']
label = [0 for _ in range(len(categories))]
for sentence in item['sentences']:
for word in sentence['tokens']:
if word in categories:
label[categories.index(word)] = 1
category_count_caption[word] += 1
bigrams = list(zip(sentence['tokens'], sentence['tokens'][1:]))
for bigram in bigrams:
word = bigram[0] + ' ' + bigram[1]
if word in categories:
label[categories.index(word)] = 1
category_count_caption[word] += 1
label_caption_splited[split].append({
'image_id': image_id,
'label': label,
})
filemap[image_id] = {
'file_path': item['filepath'],
'file_name': item['filename']
}
if image_id in label_detection:
label_detection_splited[split].append({
'image_id':
image_id,
'label':
label_detection[image_id],
})
else:
logger.warning(
"Not found in coco detection! id: {0}".format(image_id))
logger.info('detection labels:')
logger.info('train: {0}'.format(len(label_detection_splited['train'])))
logger.info('val: {0}'.format(len(label_detection_splited['val'])))
logger.info('test: {0}'.format(len(label_detection_splited['test'])))
logger.info('caption labels:')
logger.info('train: {0}'.format(len(label_caption_splited['train'])))
logger.info('val: {0}'.format(len(label_caption_splited['val'])))
logger.info('test: {0}'.format(len(label_caption_splited['test'])))
with open(os.path.join(classifier_dir, 'label_caption.json'), 'w') as fp:
json.dump(label_caption_splited, fp)
with open(os.path.join(classifier_dir, 'label_detection.json'), 'w') as fp:
json.dump(label_detection_splited, fp)
with open(os.path.join(classifier_dir, 'category_count_detection.json'),
'w') as fp:
json.dump(category_count_detection, fp)
with open(os.path.join(classifier_dir, 'category_count_caption.json'),
'w') as fp:
json.dump(category_count_caption, fp)
with open(os.path.join(data_dir, 'image_file_map.json'), 'w') as fp:
json.dump(filemap, fp)
if __name__ == '__main__':
prepare_input_classifier()