-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_utils.py
229 lines (201 loc) · 9.27 KB
/
extract_utils.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
import torch
import numpy as np
import cv2
import os
from models.bua.layers.nms import nms
from models.bua.box_regression import BUABoxes
PIXEL_MEANS = np.array([[[102.9801, 115.9465, 122.7717]]])
TEST_SCALES = (600,)
TEST_MAX_SIZE = 1000
def get_norm_bb(bboxes, image_w, image_h):
'''
normalized 6-dim vector.
return (num_bbx, 6)
'''
box_width = bboxes[:, 2] - bboxes[:, 0]
box_height = bboxes[:, 3] - bboxes[:, 1]
scaled_width = box_width / image_w
scaled_height = box_height / image_h
scaled_x = bboxes[:, 0] / image_w
scaled_y = bboxes[:, 1] / image_h
box_width = box_width[..., np.newaxis]
box_height = box_height[..., np.newaxis]
scaled_width = scaled_width[..., np.newaxis]
scaled_height = scaled_height[..., np.newaxis]
scaled_x = scaled_x[..., np.newaxis]
scaled_y = scaled_y[..., np.newaxis]
normalized_bbox = np.concatenate((scaled_x, scaled_y,
scaled_x + scaled_width,
scaled_y + scaled_height,
scaled_width, scaled_height), axis=1)
return normalized_bbox
def im_list_to_blob(ims):
"""Convert a list of images into a network input.
Assumes images are already prepared (means subtracted, BGR order, ...).
"""
max_shape = np.array([im.shape for im in ims]).max(axis=0)
num_images = len(ims)
blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),
dtype=np.float32)
for i in range(num_images):
im = ims[i]
blob[i, 0:im.shape[0], 0:im.shape[1], :] = im
return blob
def get_image_blob(im, pixel_means):
"""Converts an image into a network input.
Arguments:
im (ndarray): a color image
Returns:
blob (ndarray): a data blob holding an image pyramid
im_scale_factors (list): list of image scales (relative to im) used
in the image pyramid
"""
pixel_means = np.array([[pixel_means]])
dataset_dict = {}
im_orig = im.astype(np.float32, copy=True)
im_orig -= pixel_means
im_shape = im_orig.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
for target_size in TEST_SCALES:
im_scale = float(target_size) / float(im_size_min)
# Prevent the biggest axis from being more than MAX_SIZE
if np.round(im_scale * im_size_max) > TEST_MAX_SIZE:
im_scale = float(TEST_MAX_SIZE) / float(im_size_max)
im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
interpolation=cv2.INTER_LINEAR)
dataset_dict["image"] = torch.from_numpy(im).permute(2, 0, 1)
dataset_dict["im_scale"] = im_scale
return dataset_dict
def save_roi_features(args, cfg, im_file, im, dataset_dict, boxes, scores, features_pooled, attr_scores=None):
MIN_BOXES = cfg.MODEL.BUA.EXTRACTOR.MIN_BOXES
MAX_BOXES = cfg.MODEL.BUA.EXTRACTOR.MAX_BOXES
CONF_THRESH = cfg.MODEL.BUA.EXTRACTOR.CONF_THRESH
dets = boxes[0] / dataset_dict['im_scale']
scores = scores[0]
feats = features_pooled[0]
max_conf = torch.zeros((scores.shape[0])).to(scores.device)
for cls_ind in range(1, scores.shape[1]):
cls_scores = scores[:, cls_ind]
keep = nms(dets, cls_scores, 0.3)
max_conf[keep] = torch.where(cls_scores[keep] > max_conf[keep],
cls_scores[keep],
max_conf[keep])
keep_boxes = torch.nonzero(max_conf >= CONF_THRESH).flatten()
if len(keep_boxes) < MIN_BOXES:
keep_boxes = torch.argsort(max_conf, descending=True)[:MIN_BOXES]
elif len(keep_boxes) > MAX_BOXES:
keep_boxes = torch.argsort(max_conf, descending=True)[:MAX_BOXES]
image_feat = feats[keep_boxes]
image_bboxes = dets[keep_boxes]
soft_labels = scores[keep_boxes]
image_objects_conf = np.max(scores[keep_boxes].numpy()[:,1:], axis=1)
image_objects = np.argmax(scores[keep_boxes].numpy()[:,1:], axis=1)
if not attr_scores is None:
attr_scores = attr_scores[0]
image_attrs_conf = np.max(attr_scores[keep_boxes].numpy()[:,1:], axis=1)
image_attrs = np.argmax(attr_scores[keep_boxes].numpy()[:,1:], axis=1)
info = {
'image_id': im_file.split('.')[0],
'image_h': np.size(im, 0),
'image_w': np.size(im, 1),
'num_boxes': len(keep_boxes),
'objects_id': image_objects,
'objects_conf': image_objects_conf,
'attrs_id': image_attrs,
'attrs_conf': image_attrs_conf,
}
else:
info = {
'image_id': im_file.split('.')[0],
'image_h': np.size(im, 0),
'image_w': np.size(im, 1),
'num_boxes': len(keep_boxes),
'objects_id': image_objects,
'objects_conf': image_objects_conf
}
output_file = os.path.join(args.output_dir, im_file.split('.')[0])
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
if args.feat_struct == 'default':
if args.soft_label:
np.savez_compressed(output_file, x=image_feat, bbox=image_bboxes, num_bbox=len(keep_boxes), soft_labels=soft_labels, image_h=np.size(im, 0), image_w=np.size(im, 1), info=info)
else:
np.savez_compressed(output_file, x=image_feat, bbox=image_bboxes, num_bbox=len(keep_boxes), image_h=np.size(im, 0), image_w=np.size(im, 1), info=info)
elif args.feat_struct == 'uniter':
# for uniter
image_h, image_w = np.size(im, 0), np.size(im, 1)
norm_bbox = get_norm_bb(image_bboxes, image_w=image_w, image_h=image_h)
norm_bbox = norm_bbox.astype(np.float16)
features = image_feat.numpy().astype(np.float16)
conf = image_objects_conf.astype(np.float16)
soft_labels = soft_labels.numpy().astype(np.float16)
np.savez_compressed(output_file,
norm_bb=norm_bbox,
features=features,
conf=conf,
soft_labels=soft_labels)
else:
raise NotImplementedError
def save_bbox(args, cfg, im_file, im, dataset_dict, boxes, scores):
MIN_BOXES = cfg.MODEL.BUA.EXTRACTOR.MIN_BOXES
MAX_BOXES = cfg.MODEL.BUA.EXTRACTOR.MAX_BOXES
CONF_THRESH = cfg.MODEL.BUA.EXTRACTOR.CONF_THRESH
scores = scores[0]
boxes = boxes[0]
num_classes = scores.shape[1]
boxes = BUABoxes(boxes.reshape(-1, 4))
boxes.clip((dataset_dict['image'].shape[1]/dataset_dict['im_scale'], dataset_dict['image'].shape[2]/dataset_dict['im_scale']))
boxes = boxes.tensor.view(-1, num_classes*4) # R x C x 4
cls_boxes = torch.zeros((boxes.shape[0], 4))
for idx in range(boxes.shape[0]):
cls_idx = torch.argmax(scores[idx, 1:]) + 1
cls_boxes[idx, :] = boxes[idx, cls_idx * 4:(cls_idx + 1) * 4]
max_conf = torch.zeros((scores.shape[0])).to(scores.device)
for cls_ind in range(1, num_classes):
cls_scores = scores[:, cls_ind]
keep = nms(cls_boxes, cls_scores, 0.3)
max_conf[keep] = torch.where(cls_scores[keep] > max_conf[keep],
cls_scores[keep],
max_conf[keep])
keep_boxes = torch.argsort(max_conf, descending=True)[:MAX_BOXES]
image_bboxes = cls_boxes[keep_boxes]
output_file = os.path.join(args.output_dir, im_file.split('.')[0])
np.savez_compressed(output_file, bbox=image_bboxes, num_bbox=len(keep_boxes), image_h=np.size(im, 0), image_w=np.size(im, 1))
def save_roi_features_by_bbox(args, cfg, im_file, im, dataset_dict, boxes, scores, features_pooled, attr_scores=None):
MIN_BOXES = cfg.MODEL.BUA.EXTRACTOR.MIN_BOXES
MAX_BOXES = cfg.MODEL.BUA.EXTRACTOR.MAX_BOXES
CONF_THRESH = cfg.MODEL.BUA.EXTRACTOR.CONF_THRESH
dets = boxes[0] / dataset_dict['im_scale']
scores = scores[0]
feats = features_pooled[0]
keep_boxes = [i for i in range(scores.shape[0])]
image_feat = feats[keep_boxes]
image_bboxes = dets[keep_boxes]
image_objects_conf = np.max(scores[keep_boxes].numpy()[:,1:], axis=1)
image_objects = np.argmax(scores[keep_boxes].numpy()[:,1:], axis=1)
if not attr_scores is None:
attr_scores = attr_scores[0]
image_attrs_conf = np.max(attr_scores[keep_boxes].numpy()[:,1:], axis=1)
image_attrs = np.argmax(attr_scores[keep_boxes].numpy()[:,1:], axis=1)
info = {
'image_id': im_file.split('.')[0],
'image_h': np.size(im, 0),
'image_w': np.size(im, 1),
'num_boxes': len(keep_boxes),
'objects_id': image_objects,
'objects_conf': image_objects_conf,
'attrs_id': image_attrs,
'attrs_conf': image_attrs_conf,
}
else:
info = {
'image_id': im_file.split('.')[0],
'image_h': np.size(im, 0),
'image_w': np.size(im, 1),
'num_boxes': len(keep_boxes),
'objects_id': image_objects,
'objects_conf': image_objects_conf
}
output_file = os.path.join(args.output_dir, im_file.split('.')[0])
np.savez_compressed(output_file, x=image_feat, bbox=image_bboxes, num_bbox=len(keep_boxes), image_h=np.size(im, 0), image_w=np.size(im, 1), info=info)