diff --git a/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py b/x2paddle/op_mapper/caffe2paddle/caffe_op_mapper.py index 364844e0565b14db7b5949bbd1c785011ef6f71f..582f90ca9a1e209db10a473f4056c00db802634c 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 393dfebb538e09d4490880c6a0fcc4fcce7d9ec9..9a61e1205a8c728644d26d9f7c506a9681370030 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)