forked from MILVLG/bottom-up-attention.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_features.py
238 lines (207 loc) · 8.7 KB
/
extract_features.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# pylint: disable=no-member
"""
TridentNet Training Script.
This script is a simplified version of the training script in detectron2/tools.
"""
import argparse
import os
import sys
import torch
# import tqdm
import cv2
import numpy as np
sys.path.append('detectron2')
import detectron2.utils.comm as comm
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.data import build_detection_test_loader, build_detection_train_loader
from detectron2.config import get_cfg
from detectron2.engine import DefaultTrainer, default_setup, launch
from detectron2.evaluation import COCOEvaluator, verify_results
from detectron2.structures import Instances
from utils.utils import mkdir, save_features
from utils.extract_utils import get_image_blob, save_bbox, save_roi_features_by_bbox, save_roi_features
from utils.progress_bar import ProgressBar
from models import add_config
from models.bua.box_regression import BUABoxes
import ray
from ray.actor import ActorHandle
def switch_extract_mode(mode):
if mode == 'roi_feats':
switch_cmd = ['MODEL.BUA.EXTRACTOR.MODE', 1]
elif mode == 'bboxes':
switch_cmd = ['MODEL.BUA.EXTRACTOR.MODE', 2]
elif mode == 'bbox_feats':
switch_cmd = ['MODEL.BUA.EXTRACTOR.MODE', 3, 'MODEL.PROPOSAL_GENERATOR.NAME', 'PrecomputedProposals']
else:
print('Wrong extract mode! ')
exit()
return switch_cmd
def set_min_max_boxes(min_max_boxes):
if min_max_boxes == 'min_max_default':
return []
try:
min_boxes = int(min_max_boxes.split(',')[0])
max_boxes = int(min_max_boxes.split(',')[1])
except:
print('Illegal min-max boxes setting, using config default. ')
return []
cmd = ['MODEL.BUA.EXTRACTOR.MIN_BOXES', min_boxes,
'MODEL.BUA.EXTRACTOR.MAX_BOXES', max_boxes]
return cmd
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
add_config(args, cfg)
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.merge_from_list(switch_extract_mode(args.extract_mode))
cfg.merge_from_list(set_min_max_boxes(args.min_max_boxes))
cfg.freeze()
default_setup(cfg, args)
return cfg
def generate_npz(extract_mode, *args):
if extract_mode == 1:
save_roi_features(*args)
elif extract_mode == 2:
save_bbox(*args)
elif extract_mode == 3:
save_roi_features_by_bbox(*args)
else:
print('Invalid Extract Mode! ')
@ray.remote(num_gpus=1)
def extract_feat(split_idx, img_list, cfg, args, actor: ActorHandle):
num_images = len(img_list)
print('Number of images on split{}: {}.'.format(split_idx, num_images))
model = DefaultTrainer.build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
model.eval()
for im_file in (img_list):
if os.path.exists(os.path.join(args.output_dir, im_file.split('.')[0]+'.npz')):
actor.update.remote(1)
continue
im = cv2.imread(os.path.join(args.image_dir, im_file))
if im is None:
print(os.path.join(args.image_dir, im_file), "is illegal!")
actor.update.remote(1)
continue
dataset_dict = get_image_blob(im, cfg.MODEL.PIXEL_MEAN)
# extract roi features
if cfg.MODEL.BUA.EXTRACTOR.MODE == 1:
attr_scores = None
with torch.set_grad_enabled(False):
if cfg.MODEL.BUA.ATTRIBUTE_ON:
boxes, scores, features_pooled, attr_scores = model([dataset_dict])
else:
boxes, scores, features_pooled = model([dataset_dict])
boxes = [box.tensor.cpu() for box in boxes]
scores = [score.cpu() for score in scores]
features_pooled = [feat.cpu() for feat in features_pooled]
if not attr_scores is None:
attr_scores = [attr_score.cpu() for attr_score in attr_scores]
generate_npz(1,
args, cfg, im_file, im, dataset_dict,
boxes, scores, features_pooled, attr_scores)
# extract bbox only
elif cfg.MODEL.BUA.EXTRACTOR.MODE == 2:
with torch.set_grad_enabled(False):
boxes, scores = model([dataset_dict])
boxes = [box.cpu() for box in boxes]
scores = [score.cpu() for score in scores]
generate_npz(2,
args, cfg, im_file, im, dataset_dict,
boxes, scores)
# extract roi features by bbox
elif cfg.MODEL.BUA.EXTRACTOR.MODE == 3:
if not os.path.exists(os.path.join(args.bbox_dir, im_file.split('.')[0]+'.npz')):
actor.update.remote(1)
continue
bbox = torch.from_numpy(np.load(os.path.join(args.bbox_dir, im_file.split('.')[0]+'.npz'))['bbox']) * dataset_dict['im_scale']
proposals = Instances(dataset_dict['image'].shape[-2:])
proposals.proposal_boxes = BUABoxes(bbox)
dataset_dict['proposals'] = proposals
attr_scores = None
with torch.set_grad_enabled(False):
if cfg.MODEL.BUA.ATTRIBUTE_ON:
boxes, scores, features_pooled, attr_scores = model([dataset_dict])
else:
boxes, scores, features_pooled = model([dataset_dict])
boxes = [box.tensor.cpu() for box in boxes]
scores = [score.cpu() for score in scores]
features_pooled = [feat.cpu() for feat in features_pooled]
if not attr_scores is None:
attr_scores = [attr_score.data.cpu() for attr_score in attr_scores]
generate_npz(3,
args, cfg, im_file, im, dataset_dict,
boxes, scores, features_pooled, attr_scores)
actor.update.remote(1)
def main():
parser = argparse.ArgumentParser(description="PyTorch Object Detection2 Inference")
parser.add_argument(
"--config-file",
default="configs/bua-caffe/extract-bua-caffe-r101.yaml",
metavar="FILE",
help="path to config file",
)
parser.add_argument('--num-cpus', default=1, type=int,
help='number of cpus to use for ray, 0 means no limit')
parser.add_argument('--gpus', dest='gpu_id', help='GPU id(s) to use',
default='0', type=str)
parser.add_argument("--mode", default="caffe", type=str, help="bua_caffe, ...")
parser.add_argument('--extract-mode', default='roi_feats', type=str,
help="'roi_feats', 'bboxes' and 'bbox_feats' indicates \
'extract roi features directly', 'extract bboxes only' and \
'extract roi features with pre-computed bboxes' respectively")
parser.add_argument('--min-max-boxes', default='min_max_default', type=str,
help='the number of min-max boxes of extractor')
parser.add_argument('--out-dir', dest='output_dir',
help='output directory for features',
default="features")
parser.add_argument('--image-dir', dest='image_dir',
help='directory with images',
default="image")
parser.add_argument('--bbox-dir', dest='bbox_dir',
help='directory with bbox',
default="bbox")
parser.add_argument(
"--resume",
action="store_true",
help="whether to attempt to resume from the checkpoint directory",
)
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
cfg = setup(args)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
num_gpus = len(args.gpu_id.split(','))
MIN_BOXES = cfg.MODEL.BUA.EXTRACTOR.MIN_BOXES
MAX_BOXES = cfg.MODEL.BUA.EXTRACTOR.MAX_BOXES
CONF_THRESH = cfg.MODEL.BUA.EXTRACTOR.CONF_THRESH
# Extract features.
imglist = os.listdir(args.image_dir)
num_images = len(imglist)
print('Number of images: {}.'.format(num_images))
if args.num_cpus != 0:
ray.init(num_cpus=args.num_cpus)
else:
ray.init()
img_lists = [imglist[i::num_gpus] for i in range(num_gpus)]
pb = ProgressBar(len(imglist))
actor = pb.actor
print('Number of GPUs: {}.'.format(num_gpus))
extract_feat_list = []
for i in range(num_gpus):
extract_feat_list.append(extract_feat.remote(i, img_lists[i], cfg, args, actor))
pb.print_until_done()
ray.get(extract_feat_list)
ray.get(actor.get_counter.remote())
if __name__ == "__main__":
main()