Skip to content

Commit 0a97d24

Browse files
sefiraqingqing01
authored andcommitted
Faster RCNN Generate Proposal Labels (#12616)
* Add generate_proposal_labels for Faster-RCNN.
1 parent cfa6bbb commit 0a97d24

File tree

8 files changed

+949
-4
lines changed

8 files changed

+949
-4
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ paddle.fluid.layers.ssd_loss ArgSpec(args=['location', 'confidence', 'gt_box', '
303303
paddle.fluid.layers.detection_map ArgSpec(args=['detect_res', 'label', 'class_num', 'background_label', 'overlap_threshold', 'evaluate_difficult', 'has_state', 'input_states', 'out_states', 'ap_version'], varargs=None, keywords=None, defaults=(0, 0.3, True, None, None, None, 'integral'))
304304
paddle.fluid.layers.rpn_target_assign ArgSpec(args=['loc', 'scores', 'anchor_box', 'gt_box', 'rpn_batch_size_per_im', 'fg_fraction', 'rpn_positive_overlap', 'rpn_negative_overlap'], varargs=None, keywords=None, defaults=(256, 0.25, 0.7, 0.3))
305305
paddle.fluid.layers.anchor_generator ArgSpec(args=['input', 'anchor_sizes', 'aspect_ratios', 'variance', 'stride', 'offset', 'name'], varargs=None, keywords=None, defaults=(None, None, [0.1, 0.1, 0.2, 0.2], None, 0.5, None))
306+
paddle.fluid.layers.generate_proposal_labels ArgSpec(args=['rpn_rois', 'gt_classes', 'gt_boxes', 'im_scales', 'batch_size_per_im', 'fg_fraction', 'fg_thresh', 'bg_thresh_hi', 'bg_thresh_lo', 'bbox_reg_weights', 'class_nums'], varargs=None, keywords=None, defaults=(256, 0.25, 0.25, 0.5, 0.0, [0.1, 0.1, 0.2, 0.2], None))
306307
paddle.fluid.layers.generate_proposals ArgSpec(args=['scores', 'bbox_deltas', 'im_info', 'anchors', 'variances', 'pre_nms_top_n', 'post_nms_top_n', 'nms_thresh', 'min_size', 'eta', 'name'], varargs=None, keywords=None, defaults=(6000, 1000, 0.5, 0.1, 1.0, None))
307308
paddle.fluid.layers.iou_similarity ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None)
308309
paddle.fluid.layers.box_coder ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None)

paddle/fluid/operators/detection/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ target_assign_op.cu)
2929
detection_library(polygon_box_transform_op SRCS polygon_box_transform_op.cc
3030
polygon_box_transform_op.cu)
3131
detection_library(rpn_target_assign_op SRCS rpn_target_assign_op.cc)
32+
detection_library(generate_proposal_labels_op SRCS generate_proposal_labels_op.cc)
3233
detection_library(generate_proposals_op SRCS generate_proposals_op.cc)
3334
#Export local libraries to parent
3435
set(DETECTION_LIBRARY ${LOCAL_DETECTION_LIBS} PARENT_SCOPE)

paddle/fluid/operators/detection/generate_proposal_labels_op.cc

Lines changed: 515 additions & 0 deletions
Large diffs are not rendered by default.

paddle/fluid/operators/detection/rpn_target_assign_op.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
8686
std::minstd_rand engine,
8787
std::vector<int>* inds) const {
8888
std::uniform_real_distribution<float> uniform(0, 1);
89-
const int64_t size = static_cast<int64_t>(inds->size());
89+
const int64_t size = static_cast<int64_t>(inds->size() - offset);
9090
if (size > num) {
9191
for (int64_t i = num; i < size; ++i) {
9292
int rng_ind = std::floor(uniform(engine) * i);
@@ -126,7 +126,7 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
126126
neg_threshold, target_label_data, fg_inds, bg_inds);
127127
// Reservoir Sampling
128128
ReservoirSampling(fg_num, fg_offset, engine, fg_inds);
129-
int bg_num = rpn_batch_size - fg_inds->size();
129+
int bg_num = rpn_batch_size - (fg_inds->size() - fg_offset);
130130
ReservoirSampling(bg_num, bg_offset, engine, bg_inds);
131131
}
132132

paddle/fluid/operators/gather_op.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,8 @@ namespace ops = paddle::operators;
101101
REGISTER_OPERATOR(gather, ops::GatherOp, ops::GatherOpMaker,
102102
paddle::framework::DefaultGradOpDescMaker<true>);
103103
REGISTER_OPERATOR(gather_grad, ops::GatherGradOp);
104-
REGISTER_OP_CPU_KERNEL(gather, ops::GatherOpKernel<float>);
105-
REGISTER_OP_CPU_KERNEL(gather_grad, ops::GatherGradientOpKernel<float>);
104+
REGISTER_OP_CPU_KERNEL(gather, ops::GatherOpKernel<float>,
105+
ops::GatherOpKernel<int>, ops::GatherOpKernel<double>);
106+
REGISTER_OP_CPU_KERNEL(gather_grad, ops::GatherGradientOpKernel<float>,
107+
ops::GatherGradientOpKernel<int>,
108+
ops::GatherGradientOpKernel<double>);

python/paddle/fluid/layers/detection.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'detection_map',
4040
'rpn_target_assign',
4141
'anchor_generator',
42+
'generate_proposal_labels',
4243
'generate_proposals',
4344
]
4445

@@ -1256,6 +1257,64 @@ def _is_list_or_tuple_(data):
12561257
return anchor, var
12571258

12581259

1260+
def generate_proposal_labels(rpn_rois,
1261+
gt_classes,
1262+
gt_boxes,
1263+
im_scales,
1264+
batch_size_per_im=256,
1265+
fg_fraction=0.25,
1266+
fg_thresh=0.25,
1267+
bg_thresh_hi=0.5,
1268+
bg_thresh_lo=0.0,
1269+
bbox_reg_weights=[0.1, 0.1, 0.2, 0.2],
1270+
class_nums=None):
1271+
"""
1272+
** Generate proposal labels Faster-RCNN **
1273+
TODO(buxingyuan): Add Document
1274+
"""
1275+
1276+
helper = LayerHelper('generate_proposal_labels', **locals())
1277+
1278+
rois = helper.create_tmp_variable(dtype=rpn_rois.dtype)
1279+
labels_int32 = helper.create_tmp_variable(dtype=gt_classes.dtype)
1280+
bbox_targets = helper.create_tmp_variable(dtype=rpn_rois.dtype)
1281+
bbox_inside_weights = helper.create_tmp_variable(dtype=rpn_rois.dtype)
1282+
bbox_outside_weights = helper.create_tmp_variable(dtype=rpn_rois.dtype)
1283+
1284+
helper.append_op(
1285+
type="generate_proposal_labels",
1286+
inputs={
1287+
'RpnRois': rpn_rois,
1288+
'GtClasses': gt_classes,
1289+
'GtBoxes': gt_boxes,
1290+
'ImScales': im_scales
1291+
},
1292+
outputs={
1293+
'Rois': rois,
1294+
'LabelsInt32': labels_int32,
1295+
'BboxTargets': bbox_targets,
1296+
'BboxInsideWeights': bbox_inside_weights,
1297+
'BboxOutsideWeights': bbox_outside_weights
1298+
},
1299+
attrs={
1300+
'batch_size_per_im': batch_size_per_im,
1301+
'fg_fraction': fg_fraction,
1302+
'fg_thresh': fg_thresh,
1303+
'bg_thresh_hi': bg_thresh_hi,
1304+
'bg_thresh_lo': bg_thresh_lo,
1305+
'bbox_reg_weights': bbox_reg_weights,
1306+
'class_nums': class_nums
1307+
})
1308+
1309+
rois.stop_gradient = True
1310+
labels_int32.stop_gradient = True
1311+
bbox_targets.stop_gradient = True
1312+
bbox_inside_weights.stop_gradient = True
1313+
bbox_outside_weights.stop_gradient = True
1314+
1315+
return rois, labels_int32, bbox_targets, bbox_inside_weights, bbox_outside_weights
1316+
1317+
12591318
def generate_proposals(scores,
12601319
bbox_deltas,
12611320
im_info,

python/paddle/fluid/tests/test_detection.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,55 @@ def test_anchor_generator(self):
146146
assert anchor.shape[3] == 4
147147

148148

149+
class TestGenerateProposalLabels(unittest.TestCase):
150+
def test_generate_proposal_labels(self):
151+
rpn_rois = layers.data(
152+
name='rpn_rois',
153+
shape=[4, 4],
154+
dtype='float32',
155+
lod_level=1,
156+
append_batch_size=False)
157+
gt_classes = layers.data(
158+
name='gt_classes',
159+
shape=[6],
160+
dtype='int32',
161+
lod_level=1,
162+
append_batch_size=False)
163+
gt_boxes = layers.data(
164+
name='gt_boxes',
165+
shape=[6, 4],
166+
dtype='float32',
167+
lod_level=1,
168+
append_batch_size=False)
169+
im_scales = layers.data(
170+
name='im_scales',
171+
shape=[1],
172+
dtype='float32',
173+
lod_level=1,
174+
append_batch_size=False)
175+
class_nums = 5
176+
rois, labels_int32, bbox_targets, bbox_inside_weights, bbox_outside_weights = fluid.layers.generate_proposal_labels(
177+
rpn_rois=rpn_rois,
178+
gt_classes=gt_classes,
179+
gt_boxes=gt_boxes,
180+
im_scales=im_scales,
181+
batch_size_per_im=2,
182+
fg_fraction=0.5,
183+
fg_thresh=0.5,
184+
bg_thresh_hi=0.5,
185+
bg_thresh_lo=0.0,
186+
bbox_reg_weights=[0.1, 0.1, 0.2, 0.2],
187+
class_nums=class_nums)
188+
assert rois.shape[1] == 4
189+
assert rois.shape[0] == labels_int32.shape[0]
190+
assert rois.shape[0] == bbox_targets.shape[0]
191+
assert rois.shape[0] == bbox_inside_weights.shape[0]
192+
assert rois.shape[0] == bbox_outside_weights.shape[0]
193+
assert bbox_targets.shape[1] == 4 * class_nums
194+
assert bbox_inside_weights.shape[1] == 4 * class_nums
195+
assert bbox_outside_weights.shape[1] == 4 * class_nums
196+
197+
149198
class TestMultiBoxHead(unittest.TestCase):
150199
def test_multi_box_head(self):
151200
data_shape = [3, 224, 224]

0 commit comments

Comments
 (0)