Skip to content

Commit

Permalink
update interface for inference, test=develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrywgz committed Aug 19, 2020
1 parent ac03701 commit e690dc0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
66 changes: 39 additions & 27 deletions python/paddle/fluid/layers/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2985,7 +2985,14 @@ def generate_proposals(scores,
dtype=bbox_deltas.dtype)
rpn_roi_probs = helper.create_variable_for_type_inference(
dtype=scores.dtype)
rpn_rois_num = helper.create_variable_for_type_inference(dtype='int32')
outputs = {
'RpnRois': rpn_rois,
'RpnRoiProbs': rpn_roi_probs,
}
if return_rois_num:
rpn_rois_num = helper.create_variable_for_type_inference(dtype='int32')
rpn_rois_num.stop_gradient = True
outputs['RpnRoisNum'] = rpn_rois_num

helper.append_op(
type="generate_proposals",
Expand All @@ -3003,14 +3010,9 @@ def generate_proposals(scores,
'min_size': min_size,
'eta': eta
},
outputs={
'RpnRois': rpn_rois,
'RpnRoiProbs': rpn_roi_probs,
'RpnRoisNum': rpn_rois_num
})
outputs=outputs)
rpn_rois.stop_gradient = True
rpn_roi_probs.stop_gradient = True
rpn_rois_num.stop_gradient = True

if return_rois_num:
return rpn_rois, rpn_roi_probs, rpn_rois_num
Expand Down Expand Up @@ -3606,21 +3608,27 @@ def distribute_fpn_proposals(fpn_rois,
multi_rois = [
helper.create_variable_for_type_inference(dtype) for i in range(num_lvl)
]
rois_num_per_level = [
helper.create_variable_for_type_inference(dtype='int32')
for i in range(num_lvl)
]

restore_ind = helper.create_variable_for_type_inference(dtype='int32')

inputs = {'FpnRois': fpn_rois}
outputs = {
'MultiFpnRois': multi_rois,
'RestoreIndex': restore_ind,
}

if rois_num is not None:
inputs['RoisNum'] = rois_num
rois_num_per_level = [
helper.create_variable_for_type_inference(dtype='int32')
for i in range(num_lvl)
]
outputs['MultiLevelRoIsNum'] = rois_num_per_level

helper.append_op(
type='distribute_fpn_proposals',
inputs={'FpnRois': fpn_rois,
'RoisNum': rois_num},
outputs={
'MultiFpnRois': multi_rois,
'RestoreIndex': restore_ind,
'MultiLevelRoisNum': rois_num_per_level
},
inputs=inputs,
outputs=outputs,
attrs={
'min_level': min_level,
'max_level': max_level,
Expand Down Expand Up @@ -3790,18 +3798,22 @@ def collect_fpn_proposals(multi_rois,
input_rois = multi_rois[:num_lvl]
input_scores = multi_scores[:num_lvl]
output_rois = helper.create_variable_for_type_inference(dtype)
rois_num = helper.create_variable_for_type_inference(dtype='int32')
output_rois.stop_gradient = True
rois_num.stop_gradient = True

inputs = {
'MultiLevelRois': input_rois,
'MultiLevelScores': input_scores,
}
outputs = {'FpnRois': output_rois}
if rois_num_per_level is not None:
inputs['MultiLevelRoIsNum'] = rois_num_per_level
rois_num = helper.create_variable_for_type_inference(dtype='int32')
rois_num.stop_gradient = True
outputs['RoisNum'] = rois_num
helper.append_op(
type='collect_fpn_proposals',
inputs={
'MultiLevelRois': input_rois,
'MultiLevelScores': input_scores,
'MultiLevelRoIsNum': rois_num_per_level
},
outputs={'FpnRois': output_rois,
'RoisNum': rois_num},
inputs=inputs,
outputs=outputs,
attrs={'post_nms_topN': post_nms_top_n})
if rois_num_per_level is not None:
return output_rois, rois_num
Expand Down
35 changes: 22 additions & 13 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6808,7 +6808,7 @@ def roi_pool(input,
pooled_height (int, optional): The pooled output height, data type is int32. Default: 1
pooled_width (int, optional): The pooled output height, data type is int32. Default: 1
spatial_scale (float, optional): Multiplicative spatial scale factor to translate ROI coords from their input scale to the scale used when pooling. Default: 1.0
rois_num (Variable): The number of RoIs in each image. Default: None
rois_num (Tensor): The number of RoIs in each image. Default: None

Returns:
Variable: The pooled feature, 4D-Tensor with the shape of [num_rois, C, pooled_height, pooled_width].
Expand All @@ -6828,11 +6828,11 @@ def roi_pool(input,

input_data = np.array([i for i in range(1,17)]).reshape(1,1,4,4).astype(DATATYPE)
roi_data =fluid.create_lod_tensor(np.array([[1., 1., 2., 2.], [1.5, 1.5, 3., 3.]]).astype(DATATYPE),[[2]], place)
rois_num_data = np.array([2])
rois_num_data = np.array([2]).astype('int32')

x = fluid.data(name='input', shape=[None,1,4,4], dtype=DATATYPE)
rois = fluid.data(name='roi', shape=[None,4], dtype=DATATYPE)
rois_num = fluid.data(name='rois_num', shape=[None], dtype='int64')
rois_num = fluid.data(name='rois_num', shape=[None], dtype='int32')

pool_out = fluid.layers.roi_pool(
input=x,
Expand All @@ -6853,11 +6853,16 @@ def roi_pool(input,
dtype = helper.input_dtype()
pool_out = helper.create_variable_for_type_inference(dtype)
argmaxes = helper.create_variable_for_type_inference(dtype='int32')

inputs = {
"X": input,
"ROIs": rois,
}
if rois_num is not None:
inputs['RoisNum'] = rois_num
helper.append_op(
type="roi_pool",
inputs={"X": input,
"ROIs": rois,
"RoisNum": rois_num},
inputs=inputs,
outputs={"Out": pool_out,
"Argmax": argmaxes},
attrs={
Expand All @@ -6875,8 +6880,8 @@ def roi_align(input,
pooled_width=1,
spatial_scale=1.0,
sampling_ratio=-1,
name=None,
rois_num=None):
rois_num=None,
name=None):
"""
:alias_main: paddle.nn.functional.roi_align
:alias: paddle.nn.functional.roi_align,paddle.nn.functional.vision.roi_align
Expand All @@ -6895,10 +6900,10 @@ def roi_align(input,
pooled_width (int32, optional): ${pooled_width_comment} Default: 1
spatial_scale (float32, optional): ${spatial_scale_comment} Default: 1.0
sampling_ratio(int32, optional): ${sampling_ratio_comment} Default: -1
rois_num (Tensor): The number of RoIs in each image. Default: None
name(str, optional): For detailed information, please refer
to :ref:`api_guide_Name`. Usually name is no need to set and
None by default.
rois_num (Variable): The number of RoIs in each image. Default: None

Returns:
Variable:
Expand All @@ -6914,7 +6919,7 @@ def roi_align(input,
name='data', shape=[None, 256, 32, 32], dtype='float32')
rois = fluid.data(
name='rois', shape=[None, 4], dtype='float32')
rois_num = fluid.data(name='rois_num', shape=[None], dtype='int64')
rois_num = fluid.data(name='rois_num', shape=[None], dtype='int32')
align_out = fluid.layers.roi_align(input=x,
rois=rois,
pooled_height=7,
Expand All @@ -6929,11 +6934,15 @@ def roi_align(input,
helper = LayerHelper('roi_align', **locals())
dtype = helper.input_dtype()
align_out = helper.create_variable_for_type_inference(dtype)
inputs = {
"X": input,
"ROIs": rois,
}
if rois_num is not None:
inputs['RoisNum'] = rois_num
helper.append_op(
type="roi_align",
inputs={"X": input,
"ROIs": rois,
"RoisNum": rois_num},
inputs=inputs,
outputs={"Out": align_out},
attrs={
"pooled_height": pooled_height,
Expand Down

0 comments on commit e690dc0

Please sign in to comment.