From 9277f0bcb645640d050185f539d37c0d72562ee4 Mon Sep 17 00:00:00 2001 From: WJJ1995 Date: Thu, 3 Jun 2021 11:16:10 +0800 Subject: [PATCH] Fix caffe bug && Add onnx 3d interpolate support (#611) * fix caffe kernel_size=0 bug * add onnx 3d interpolate support --- .../op_mapper/caffe2paddle/caffe_op_mapper.py | 8 +- .../op_mapper/onnx2paddle/opset9/opset.py | 106 +++++++++++++----- 2 files changed, 81 insertions(+), 33 deletions(-) diff --git a/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py b/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py index 364844e..582f90c 100644 --- a/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py +++ b/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py @@ -62,15 +62,15 @@ def _get_kernel_parameters(kind, params): "Convolution", "Pooling", "Deconvolution", "ConvolutionDepthwise" ] [k_h, k_w] = [1, 1] - if isinstance(params.kernel_size, numbers.Number): + if params.kernel_h > 0 or params.kernel_w > 0: + k_h = params.kernel_h + k_w = params.kernel_w + elif isinstance(params.kernel_size, numbers.Number): [k_h, k_w] = [params.kernel_size] * 2 elif len(params.kernel_size) > 0: k_h = params.kernel_h if params.kernel_h > 0 else params.kernel_size[0] k_w = params.kernel_w if params.kernel_w > 0 else params.kernel_size[ len(params.kernel_size) - 1] - elif params.kernel_h > 0 or params.kernel_w > 0: - k_h = params.kernel_h - k_w = params.kernel_w [s_h, s_w] = [1, 1] if isinstance(params.stride, numbers.Number): [s_h, s_w] = [params.stride] * 2 diff --git a/x2paddle/op_mapper/onnx2paddle/opset9/opset.py b/x2paddle/op_mapper/onnx2paddle/opset9/opset.py index 393dfeb..9a61e12 100755 --- a/x2paddle/op_mapper/onnx2paddle/opset9/opset.py +++ b/x2paddle/op_mapper/onnx2paddle/opset9/opset.py @@ -309,35 +309,83 @@ class OpSet9(): elif len(node.layer.input) == 4: # opset 11 val_sizes = self.graph.get_input_node(node, idx=3, copy=True) - var_nc, var_hw = val_sizes.name + '_nc', val_sizes.name + '_hw' - self.paddle_graph.add_layer( - 'paddle.split', - inputs={"x": val_sizes.name}, - outputs=[var_nc, var_hw], - num_or_sections=[2, 2], - axis=0) - self.paddle_graph.add_layer( - "paddle.cast", - inputs={"x": var_hw}, - outputs=[var_hw], - dtype=string('int32')) - inputs['size'] = var_hw - attrs = { - "align_corners": False, - "mode": string(node.get_attr('mode', 'nearest')) - } - mode = node.get_attr('mode', 'nearest') - if mode == "linear": - attrs["mode"] = string("bilinear") - if node.get_attr('coordinate_transformation_mode', - 'half_pixel') == 'pytorch_half_pixel': - attrs["align_corners"] = False - attrs["align_mode"] = 0 - self.paddle_graph.add_layer( - kernel="paddle.nn.functional.interpolate", - inputs=inputs, - outputs=[node.name], - **attrs) + val_x_shape = val_x.out_shapes[0] + if len(val_x_shape) == 3: + var_n, var_hw = val_sizes.name + '_n', val_sizes.name + '_hw' + self.paddle_graph.add_layer( + 'paddle.split', + inputs={"x": val_sizes.name}, + outputs=[var_n, var_hw], + num_or_sections=[1, 2], + axis=0) + self.paddle_graph.add_layer( + "paddle.cast", + inputs={"x": var_hw}, + outputs=[var_hw], + dtype=string('int32')) + inputs['size'] = var_hw + attrs = { + "align_corners": False, + "mode": string(node.get_attr('mode', 'nearest')) + } + mode = node.get_attr('mode', 'nearest') + if mode == "linear": + attrs["mode"] = string("bilinear") + if node.get_attr('coordinate_transformation_mode', + 'half_pixel') == 'pytorch_half_pixel': + attrs["align_corners"] = False + attrs["align_mode"] = 0 + if node.get_attr('coordinate_transformation_mode', + 'half_pixel') == 'align_corners': + attrs["align_corners"] = True + self.paddle_graph.add_layer( + 'paddle.unsqueeze', + inputs={"x": val_x.name}, + outputs=[val_x.name], + axis=0) + self.paddle_graph.add_layer( + kernel="paddle.nn.functional.interpolate", + inputs=inputs, + outputs=[node.name], + **attrs) + self.paddle_graph.add_layer( + 'paddle.squeeze', + inputs={"x": node.name}, + outputs=[node.name], + axis=0) + else: + var_nc, var_hw = val_sizes.name + '_nc', val_sizes.name + '_hw' + self.paddle_graph.add_layer( + 'paddle.split', + inputs={"x": val_sizes.name}, + outputs=[var_nc, var_hw], + num_or_sections=[2, 2], + axis=0) + self.paddle_graph.add_layer( + "paddle.cast", + inputs={"x": var_hw}, + outputs=[var_hw], + dtype=string('int32')) + inputs['size'] = var_hw + attrs = { + "align_corners": False, + "mode": string(node.get_attr('mode', 'nearest')) + } + mode = node.get_attr('mode', 'nearest') + if mode == "linear": + attrs["mode"] = string("bilinear") + if node.get_attr('coordinate_transformation_mode', + 'half_pixel') == 'pytorch_half_pixel': + attrs["align_corners"] = False + attrs["align_mode"] = 0 + if node.get_attr('coordinate_transformation_mode', + 'half_pixel') == 'align_corners': + attrs["align_corners"] = True + self.paddle_graph.add_layer( + kernel="paddle.nn.functional.interpolate", + inputs=inputs, + outputs=[node.name], + **attrs) return elif node.layer_type == 'Upsample': val_scales = self.graph.get_input_node(node, idx=1, copy=True) -- GitLab