-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathextract_region_feature.py
269 lines (217 loc) · 10.8 KB
/
extract_region_feature.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
268
269
import argparse
import os
import torch
import tqdm
from fvcore.common.file_io import PathManager
# import pdb
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import get_cfg
from detectron2.engine import default_setup
from detectron2.evaluation import inference_context
from detectron2.modeling import build_model
import numpy as np
from grid_feats import (
add_attribute_config,
build_detection_test_loader_with_attributes,
)
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["HDF5_USE_FILE_LOCKING"] = 'FALSE'
# A simple mapper from object detection dataset to VQA dataset names
dataset_to_folder_mapper = {}
dataset_to_folder_mapper['coco_2014_train'] = 'train2014'
dataset_to_folder_mapper['coco_2014_val'] = 'val2014'
# One may need to change the Detectron2 code to support coco_2015_test
# insert "coco_2015_test": ("coco/test2015", "coco/annotations/image_info_test2015.json"),
# at: https://github.com/facebookresearch/detectron2/blob/master/detectron2/data/datasets/builtin.py#L36
dataset_to_folder_mapper['coco_2014_test'] = 'test2014'
def extract_grid_feature_argument_parser():
parser = argparse.ArgumentParser(description="Grid feature extraction")
parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file")
parser.add_argument("--dataset", help="name of the dataset", default="coco_2014_train",
choices=['coco_2014_train', 'coco_2014_val', 'coco_2015_test'])
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
return parser
def extract_grid_feature_on_dataset(model, data_loader, dump_folder):
for idx, inputs in enumerate(tqdm.tqdm(data_loader)):
with torch.no_grad():
image_id = inputs[0]['image_id']
file_name = '%d.pth' % image_id
# compute features
images = model.preprocess_image(inputs)
features = model.backbone(images.tensor)
outputs = model.roi_heads.get_conv5_features(features)
with PathManager.open(os.path.join(dump_folder, file_name), "wb") as f:
# save as CPU tensors
torch.save(outputs.cpu(), f)
def do_feature_extraction(cfg, model, dataset_name):
with inference_context(model):
dump_folder = os.path.join(cfg.OUTPUT_DIR, "features", dataset_to_folder_mapper[dataset_name])
PathManager.mkdirs(dump_folder)
data_loader = build_detection_test_loader_with_attributes(cfg, dataset_name)
extract_grid_feature_on_dataset(model, data_loader, dump_folder)
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
add_attribute_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
# force the final residual block to have dilations 1
# cfg.MODEL.RESNETS.RES5_DILATION = 1
cfg.MODEL.WEIGHTS = 'output_X101/X-101.pth'
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.05 # I do thresh filter in my code
cfg.freeze()
default_setup(cfg, args)
return cfg
def resetup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
add_attribute_config(cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
# force the final residual block to have dilations 1
# cfg.MODEL.RESNETS.RES5_DILATION = 1
cfg.MODEL.WEIGHTS = 'output_X101/X-101.pth'
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0 # I do thresh filter in my code
cfg.freeze()
default_setup(cfg, args)
return cfg
def main(args):
cfg = setup(args)
model = build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=True
)
do_feature_extraction(cfg, model, args.dataset)
args = extract_grid_feature_argument_parser().parse_args('--config-file configs/X-101-grid.yaml --dataset coco_2014_train'.split())
cfg = setup(args)
print(cfg)
model = build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=True
)
import h5py
import os
from detectron2.structures import Boxes
save_dir = '/home/luoyp/disk1/grid-feats-vqa/feats'
region_before = h5py.File(os.path.join(save_dir,'region_before_X101.hdf5'),'w')
# region_after = h5py.File(os.path.join(save_dir,'region_after.hdf5'),'w')
# grid7 = h5py.File(os.path.join(save_dir,'my_grid7.hdf5'),'w')
# original_grid = h5py.File(os.path.join(save_dir,'original_grid7.hdf5'),'w')
thresh = 0.2
max_regions = 100
pooling = torch.nn.AdaptiveAvgPool2d((7,7))
image_id_collector = []
for dataset_name in ['coco_2014_train','coco_2014_val']:
with inference_context(model):
dump_folder = os.path.join(cfg.OUTPUT_DIR, "features", dataset_to_folder_mapper[dataset_name])
PathManager.mkdirs(dump_folder)
data_loader = build_detection_test_loader_with_attributes(cfg, dataset_name)
for idx, inputs in enumerate(tqdm.tqdm(data_loader)):
with torch.no_grad():
image_id = inputs[0]['image_id']
file_name = '%d.pth' % image_id
images = model.preprocess_image(inputs)
features = model.backbone(images.tensor)
proposals, _ = model.proposal_generator(images, features)
proposal_boxes = [x.proposal_boxes for x in proposals]
features = [features[f] for f in model.roi_heads.in_features]
box_features1 = model.roi_heads.box_pooler(features, [x.proposal_boxes for x in proposals])
box_features = model.roi_heads.box_head(box_features1)
predictions = model.roi_heads.box_predictor(box_features)
pred_instances, index = model.roi_heads.box_predictor.inference(predictions, proposals)
topk = 10
scores = pred_instances[0].get_fields()['scores']
topk_index = index[0][:topk]
thresh_mask = scores > thresh
thresh_index = index[0][thresh_mask]
if len(thresh_index) < 10:
index = [topk_index]
elif len(thresh_index) > max_regions:
index = [thresh_index[:max_regions]]
else:
index = [thresh_index]
if len(topk_index) < 10:
print("{} has less than 10 regions!!!".format(image_id))
image_id_collector.append(image_id)
continue
# feature of proposal
proposal_box_features1 = box_features1[index].mean(dim=[2,3])
proposal_box_features = box_features[index]
# pdb.set_trace()
boxes = pred_instances[0].get_fields()['pred_boxes'].tensor[:len(index[0])]
image_size = pred_instances[0].image_size
assert boxes.shape[0] == proposal_box_features.shape[0]
region_before.create_dataset('{}_features'.format(image_id),data=proposal_box_features1.cpu().numpy())
region_before.create_dataset('{}_boxes'.format(image_id),data=boxes.cpu().numpy())
region_before.create_dataset('{}_size'.format(image_id),data=np.array([image_size]))
# region_after.create_dataset('{}_features'.format(image_id),data=proposal_box_features.cpu().numpy())
# region_after.create_dataset('{}_boxes'.format(image_id),data=boxes.cpu().numpy())
# region_after.create_dataset('{}_size'.format(image_id),data=np.array([image_size]))
del cfg
del model
cfg = resetup(args)
print(cfg)
model = build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=True
)
print('problem images:')
print(image_id_collector)
for dataset_name in ['coco_2014_train','coco_2014_val']:
with inference_context(model):
dump_folder = os.path.join(cfg.OUTPUT_DIR, "features", dataset_to_folder_mapper[dataset_name])
PathManager.mkdirs(dump_folder)
data_loader = build_detection_test_loader_with_attributes(cfg, dataset_name)
for idx, inputs in enumerate(tqdm.tqdm(data_loader)):
with torch.no_grad():
image_id = inputs[0]['image_id']
if image_id not in image_id_collector:
continue
print('append image:',image_id)
file_name = '%d.pth' % image_id
images = model.preprocess_image(inputs)
features = model.backbone(images.tensor)
proposals, _ = model.proposal_generator(images, features)
proposal_boxes = [x.proposal_boxes for x in proposals]
features = [features[f] for f in model.roi_heads.in_features]
box_features1 = model.roi_heads.box_pooler(features, [x.proposal_boxes for x in proposals])
box_features = model.roi_heads.box_head(box_features1)
predictions = model.roi_heads.box_predictor(box_features)
pred_instances, index = model.roi_heads.box_predictor.inference(predictions, proposals)
topk = 10
scores = pred_instances[0].get_fields()['scores']
topk_index = index[0][:topk]
thresh_mask = scores > thresh
thresh_index = index[0][thresh_mask]
# if len(thresh_index) < 10:
index = [topk_index]
if len(topk_index) > max_regions:
index = [topk_index[:max_regions]]
if len(topk_index) < 10:
print("{} has less than 10 regions!!!".format(image_id))
raise
# feature of proposal
proposal_box_features1 = box_features1[index].mean(dim=[2,3])
proposal_box_features = box_features[index]
boxes = pred_instances[0].get_fields()['pred_boxes'].tensor[:len(index[0])]
image_size = pred_instances[0].image_size
assert boxes.shape[0] == proposal_box_features.shape[0]
region_before.create_dataset('{}_features'.format(image_id),data=proposal_box_features1.cpu().numpy())
region_before.create_dataset('{}_boxes'.format(image_id),data=boxes.cpu().numpy())
region_before.create_dataset('{}_size'.format(image_id),data=np.array([image_size]))
# region_after.create_dataset('{}_features'.format(image_id),data=proposal_box_features.cpu().numpy())
# region_after.create_dataset('{}_boxes'.format(image_id),data=boxes.cpu().numpy())
# region_after.create_dataset('{}_size'.format(image_id),data=np.array([image_size]))
region_before.close()
# region_after.close()
# grid7.close()
# original_grid.close()