diff --git a/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/priorbox.py b/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/priorbox.py index eb4f5f99deca576dabda4374e768da8cb4a7738d..ee3fe16c54d4d3a956ba527f32e7ce8e585ba208 100644 --- a/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/priorbox.py +++ b/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/priorbox.py @@ -13,7 +13,85 @@ # limitations under the License. import paddle -import paddle.fluid as fluid +from paddle import _C_ops +from paddle.common_ops_import import Variable, LayerHelper, check_variable_and_dtype, check_type, check_dtype + + +def prior_box(input, + image, + min_sizes, + max_sizes=None, + aspect_ratios=[1.], + variance=[0.1, 0.1, 0.2, 0.2], + flip=False, + clip=False, + steps=[0.0, 0.0], + offset=0.5, + min_max_aspect_ratios_order=False, + name=None): + helper = LayerHelper("prior_box", **locals()) + dtype = helper.input_dtype() + check_variable_and_dtype( + input, 'input', ['uint8', 'int8', 'float32', 'float64'], 'prior_box') + + def _is_list_or_tuple_(data): + return (isinstance(data, list) or isinstance(data, tuple)) + + if not _is_list_or_tuple_(min_sizes): + min_sizes = [min_sizes] + if not _is_list_or_tuple_(aspect_ratios): + aspect_ratios = [aspect_ratios] + if not (_is_list_or_tuple_(steps) and len(steps) == 2): + raise ValueError('steps should be a list or tuple ', + 'with length 2, (step_width, step_height).') + + min_sizes = list(map(float, min_sizes)) + aspect_ratios = list(map(float, aspect_ratios)) + steps = list(map(float, steps)) + + cur_max_sizes = None + if max_sizes is not None and len(max_sizes) > 0 and max_sizes[0] > 0: + if not _is_list_or_tuple_(max_sizes): + max_sizes = [max_sizes] + cur_max_sizes = max_sizes + + if in_dynamic_mode(): + attrs = ('min_sizes', min_sizes, 'aspect_ratios', aspect_ratios, + 'variances', variance, 'flip', flip, 'clip', clip, 'step_w', + steps[0], 'step_h', steps[1], 'offset', offset, + 'min_max_aspect_ratios_order', min_max_aspect_ratios_order) + if cur_max_sizes is not None: + attrs += ('max_sizes', cur_max_sizes) + box, var = _C_ops.prior_box(input, image, *attrs) + return box, var + else: + attrs = { + 'min_sizes': min_sizes, + 'aspect_ratios': aspect_ratios, + 'variances': variance, + 'flip': flip, + 'clip': clip, + 'step_w': steps[0], + 'step_h': steps[1], + 'offset': offset, + 'min_max_aspect_ratios_order': min_max_aspect_ratios_order + } + + if cur_max_sizes is not None: + attrs['max_sizes'] = cur_max_sizes + + box = helper.create_variable_for_type_inference(dtype) + var = helper.create_variable_for_type_inference(dtype) + helper.append_op( + type="prior_box", + inputs={"Input": input, + "Image": image}, + outputs={"Boxes": box, + "Variances": var}, + attrs=attrs, ) + box.stop_gradient = True + var.stop_gradient = True + return box, var class PriorBox(object): @@ -32,8 +110,7 @@ class PriorBox(object): } def __call__(self, x0, x1): - box, var = fluid.layers.prior_box( - input=x0, image=x1, **self.priorbox_layer_attrs) + box, var = prior_box(input=x0, image=x1, **self.priorbox_layer_attrs) box = paddle.reshape(x=box, shape=[1, 1, -1]) var = paddle.reshape(x=var, shape=[1, 1, -1]) out = paddle.concat(x=[box, var], axis=1) diff --git a/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/roipooling.py b/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/roipooling.py index 5f6b81b2993cdd7428cfe9dd1cb72becf24c1210..456605bc8812187ff6f252ba41c2b7c10e130c9b 100644 --- a/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/roipooling.py +++ b/x2paddle/op_mapper/caffe2paddle/caffe_custom_layer/roipooling.py @@ -13,7 +13,50 @@ # limitations under the License. import paddle -import paddle.fluid as fluid +from paddle import _C_ops +from paddle import in_dynamic_mode +from paddle.common_ops_import import Variable, LayerHelper, check_variable_and_dtype, check_type, check_dtype + + +def roi_pool(input, + rois, + pooled_height, + pooled_width, + spatial_scale=1.0, + rois_num=None, + name=None): + if in_dynamic_mode(): + assert rois_num is not None, "rois_num should not be None in dygraph mode." + pool_out, argmaxes = _C_ops.roi_pool( + input, rois, rois_num, "pooled_height", pooled_height, + "pooled_width", pooled_width, "spatial_scale", spatial_scale) + return pool_out, argmaxes + + else: + check_variable_and_dtype(input, 'input', ['float32'], 'roi_pool') + check_variable_and_dtype(rois, 'rois', ['float32'], 'roi_pool') + helper = LayerHelper('roi_pool', **locals()) + 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=inputs, + outputs={"Out": pool_out, + "Argmax": argmaxes}, + attrs={ + "pooled_height": pooled_height, + "pooled_width": pooled_width, + "spatial_scale": spatial_scale + }) + return pool_out, argmaxes class ROIPooling(object): @@ -26,6 +69,5 @@ class ROIPooling(object): def __call__(self, x0, x1): slice_x1 = paddle.slice(input=x1, axes=[1], starts=[1], ends=[5]) - out = fluid.layers.roi_pool( - input=x0, rois=slice_x1, **self.roipooling_layer_attrs) + out = roi_pool(input=x0, rois=slice_x1, **self.roipooling_layer_attrs) return out diff --git a/x2paddle/op_mapper/onnx2paddle/onnx_custom_layer/nms.py b/x2paddle/op_mapper/onnx2paddle/onnx_custom_layer/nms.py index 17d573dbffaf9f0d37a7e1d1b366946cd3ae624f..32f770a30820ccee809a75aafe4b0a2d248aec9f 100644 --- a/x2paddle/op_mapper/onnx2paddle/onnx_custom_layer/nms.py +++ b/x2paddle/op_mapper/onnx2paddle/onnx_custom_layer/nms.py @@ -13,9 +13,9 @@ # limitations under the License. import paddle -from paddle.fluid import core -from paddle.fluid.framework import Variable, in_dygraph_mode -from paddle.fluid.layer_helper import LayerHelper +from paddle import _C_ops +from paddle import in_dynamic_mode +from paddle.common_ops_import import Variable, LayerHelper def multiclass_nms(bboxes, @@ -33,13 +33,13 @@ def multiclass_nms(bboxes, name=None): helper = LayerHelper('multiclass_nms3', **locals()) - if in_dygraph_mode(): + if in_dynamic_mode(): attrs = ('background_label', background_label, 'score_threshold', score_threshold, 'nms_top_k', nms_top_k, 'nms_threshold', nms_threshold, 'keep_top_k', keep_top_k, 'nms_eta', nms_eta, 'normalized', normalized) - output, index, nms_rois_num = core.ops.multiclass_nms3(bboxes, scores, - rois_num, *attrs) + output, index, nms_rois_num = _C_ops.multiclass_nms3(bboxes, scores, + rois_num, *attrs) if not return_index: index = None return output, nms_rois_num, index