-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode_bboxes.py
70 lines (57 loc) · 2.45 KB
/
decode_bboxes.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
# Copyright (c) 2017-present, Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
from detectron.core.config import cfg
import detectron.utils.boxes as box_utils
class DecodeBBoxesOp(object):
"""Output predicted bbox, by Zhaowei Cai for Cascade R-CNN.
"""
def __init__(self, bbox_reg_weights):
self._bbox_reg_weights = bbox_reg_weights
def forward(self, inputs, outputs):
"""See modeling.detector.DecodeBBoxes for inputs/outputs
documentation.
"""
bbox_deltas = inputs[0].data
assert cfg.MODEL.CLS_AGNOSTIC_BBOX_REG
assert bbox_deltas.shape[1] == 8
bbox_deltas = bbox_deltas[:, -4:]
bbox_data = inputs[1].data
assert bbox_data.shape[1] == 5
batch_inds = bbox_data[:, :1]
bbox_prior = bbox_data[:, 1:]
# Transform bbox priors into proposals via bbox transformations
bbox_decode = box_utils.bbox_transform(
bbox_prior, bbox_deltas, self._bbox_reg_weights
)
# remove mal-boxes with non-positive width or height and ground
# truth boxes during training
if len(inputs) > 2:
mapped_gt_boxes = inputs[2].data
max_overlap = mapped_gt_boxes[:, 4]
keep = _filter_boxes(bbox_decode, max_overlap)
bbox_decode = bbox_decode[keep, :]
batch_inds = batch_inds[keep, :]
bbox_decode = np.hstack((batch_inds, bbox_decode))
outputs[0].reshape(bbox_decode.shape)
outputs[0].data[...] = bbox_decode
def _filter_boxes(boxes, max_overlap):
"""Only keep boxes with positive height and width, and not-gt.
"""
ws = boxes[:, 2] - boxes[:, 0] + 1
hs = boxes[:, 3] - boxes[:, 1] + 1
keep = np.where((ws > 0) & (hs > 0) & (max_overlap < 1.0))[0]
return keep