@ -20,7 +20,8 @@ from __future__ import print_function
from . layer_function_generator import generate_layer_fn
from . layer_function_generator import autodoc , templatedoc
from . . layer_helper import LayerHelper
from . . framework import Variable
from . . framework import Variable , in_dygraph_mode
from . . import core
from . loss import softmax_with_cross_entropy
from . import tensor
from . import nn
@ -2893,8 +2894,8 @@ def generate_proposals(scores,
nms_thresh = 0.5 ,
min_size = 0.1 ,
eta = 1.0 ,
name= Non e,
return_rois_num= Fals e) :
return_rois_num= Fals e,
name= Non e) :
"""
: alias_main : paddle . nn . functional . generate_proposals
: alias : paddle . nn . functional . generate_proposals , paddle . nn . functional . vision . generate_proposals
@ -2949,6 +2950,10 @@ def generate_proposals(scores,
num of each image in one batch . The N is the image ' s num. For example, the tensor has values [4,5] that represents
the first image has 4 Rois , the second image has 5 Rois . It only used in rcnn model .
' False ' by default .
name ( str , optional ) : For detailed information , please refer
to : ref : ` api_guide_Name ` . Usually name is no need to set and
None by default .
Returns :
tuple :
A tuple with format ` ` ( rpn_rois , rpn_roi_probs ) ` ` .
@ -2969,6 +2974,14 @@ def generate_proposals(scores,
im_info , anchors , variances )
"""
if in_dygraph_mode ( ) :
assert return_rois_num , " return_rois_num should be True in dygraph mode. "
attrs = ( ' pre_nms_topN ' , pre_nms_top_n , ' post_nms_topN ' , post_nms_top_n ,
' nms_thresh ' , nms_thresh , ' min_size ' , min_size , ' eta ' , eta )
rpn_rois , rpn_roi_probs , rpn_rois_num = core . ops . generate_proposals (
scores , bbox_deltas , im_info , anchors , variances , * attrs )
return rpn_rois , rpn_roi_probs , rpn_rois_num
helper = LayerHelper ( ' generate_proposals ' , * * locals ( ) )
check_variable_and_dtype ( scores , ' scores ' , [ ' float32 ' ] ,
@ -2986,7 +2999,14 @@ def generate_proposals(scores,
dtype = bbox_deltas . dtype )
rpn_roi_probs = helper . create_variable_for_type_inference (
dtype = scores . dtype )
rpn_rois_lod = 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 " ,
@ -3004,17 +3024,12 @@ def generate_proposals(scores,
' min_size ' : min_size ,
' eta ' : eta
} ,
outputs = {
' RpnRois ' : rpn_rois ,
' RpnRoiProbs ' : rpn_roi_probs ,
' RpnRoisLod ' : rpn_rois_lod
} )
outputs = outputs )
rpn_rois . stop_gradient = True
rpn_roi_probs . stop_gradient = True
rpn_rois_lod . stop_gradient = True
if return_rois_num :
return rpn_rois , rpn_roi_probs , rpn_rois_ lod
return rpn_rois , rpn_roi_probs , rpn_rois_ num
else :
return rpn_rois , rpn_roi_probs
@ -3656,6 +3671,7 @@ def distribute_fpn_proposals(fpn_rois,
max_level ,
refer_level ,
refer_scale ,
rois_num = None ,
name = None ) :
"""
: alias_main : paddle . nn . functional . distribute_fpn_proposals
@ -3687,6 +3703,11 @@ def distribute_fpn_proposals(fpn_rois,
come from .
refer_level ( int32 ) : The referring level of FPN layer with specified scale .
refer_scale ( int32 ) : The referring scale of FPN layer with specified level .
rois_num ( Tensor ) : 1 - D Tensor contains the number of RoIs in each image .
The shape is [ B ] and data type is int32 . B is the number of images .
If it is not None then return a list of 1 - D Tensor . Each element
is the output RoIs ' number of each image on the corresponding level
and the shape is [ B ] . None by default .
name ( str , optional ) : For detailed information , please refer
to : ref : ` api_guide_Name ` . Usually name is no need to set and
None by default .
@ -3702,6 +3723,10 @@ def distribute_fpn_proposals(fpn_rois,
the number of total rois . The data type is int32 . It is
used to restore the order of fpn_rois .
rois_num_per_level ( List ) : A list of 1 - D Tensor and each Tensor is
the RoIs ' number in each image on the corresponding level. The shape
is [ B ] and data type of int32 . B is the number of images
Examples :
. . code - block : : python
@ -3716,26 +3741,52 @@ def distribute_fpn_proposals(fpn_rois,
refer_level = 4 ,
refer_scale = 224 )
"""
num_lvl = max_level - min_level + 1
if in_dygraph_mode ( ) :
assert rois_num is not None , " rois_num should not be None in dygraph mode. "
attrs = ( ' min_level ' , min_level , ' max_level ' , max_level , ' refer_level ' ,
refer_level , ' refer_scale ' , refer_scale )
multi_rois , restore_ind , rois_num_per_level = core . ops . distribute_fpn_proposals (
fpn_rois , rois_num , num_lvl , num_lvl , * attrs )
return multi_rois , restore_ind , rois_num_per_level
check_variable_and_dtype ( fpn_rois , ' fpn_rois ' , [ ' float32 ' , ' float64 ' ] ,
' distribute_fpn_proposals ' )
helper = LayerHelper ( ' distribute_fpn_proposals ' , * * locals ( ) )
dtype = helper . input_dtype ( ' fpn_rois ' )
num_lvl = max_level - min_level + 1
multi_rois = [
helper . create_variable_for_type_inference ( dtype ) 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 } ,
outputs = { ' MultiFpnRois ' : multi_rois ,
' RestoreIndex ' : restore_ind } ,
inputs = inputs ,
outputs = outputs ,
attrs = {
' min_level ' : min_level ,
' max_level ' : max_level ,
' refer_level ' : refer_level ,
' refer_scale ' : refer_scale
} )
if rois_num is not None :
return multi_rois , restore_ind , rois_num_per_level
return multi_rois , restore_ind
@ -3820,6 +3871,7 @@ def collect_fpn_proposals(multi_rois,
min_level ,
max_level ,
post_nms_top_n ,
rois_num_per_level = None ,
name = None ) :
"""
: alias_main : paddle . nn . functional . collect_fpn_proposals
@ -3846,6 +3898,12 @@ def collect_fpn_proposals(multi_rois,
min_level ( int ) : The lowest level of FPN layer to collect
max_level ( int ) : The highest level of FPN layer to collect
post_nms_top_n ( int ) : The number of selected RoIs
rois_num_per_level ( list , optional ) : The List of RoIs ' numbers.
Each element is 1 - D Tensor which contains the RoIs ' number of each
image on each level and the shape is [ B ] and data type is
int32 , B is the number of images . If it is not None then return
a 1 - D Tensor contains the output RoIs ' number of each image and
the shape is [ B ] . 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 .
@ -3856,6 +3914,9 @@ def collect_fpn_proposals(multi_rois,
fpn_rois ( Variable ) : 2 - D LoDTensor with shape [ N , 4 ] and data type is
float32 or float64 . Selected RoIs .
rois_num ( Tensor ) : 1 - D Tensor contains the RoIs ' s number of each
image . The shape is [ B ] and data type is int32 . B is the number of
images .
Examples :
. . code - block : : python
@ -3879,21 +3940,38 @@ def collect_fpn_proposals(multi_rois,
"""
check_type ( multi_rois , ' multi_rois ' , list , ' collect_fpn_proposals ' )
check_type ( multi_scores , ' multi_scores ' , list , ' collect_fpn_proposals ' )
num_lvl = max_level - min_level + 1
input_rois = multi_rois [ : num_lvl ]
input_scores = multi_scores [ : num_lvl ]
if in_dygraph_mode ( ) :
assert rois_num_per_level is not None , " rois_num_per_level should not be None in dygraph mode. "
attrs = ( ' post_nms_topN ' , post_nms_top_n )
output_rois , rois_num = core . ops . collect_fpn_proposals (
input_rois , input_scores , rois_num_per_level , * attrs )
helper = LayerHelper ( ' collect_fpn_proposals ' , * * locals ( ) )
dtype = helper . input_dtype ( ' multi_rois ' )
check_dtype ( dtype , ' multi_rois ' , [ ' float32 ' , ' float64 ' ] ,
' collect_fpn_proposals ' )
num_lvl = max_level - min_level + 1
input_rois = multi_rois [ : num_lvl ]
input_scores = multi_scores [ : num_lvl ]
output_rois = helper . create_variable_for_type_inference ( dtype )
output_rois . 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
} ,
outputs = { ' FpnRois ' : output_rois } ,
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
return output_rois