Skip to content

Conv with auto_pad="SAME_UPPER" does not work as expected. #495

Closed
@lynic

Description

@lynic

Describe the bug
When set strides > 1 with auto_pad="SAME_UPPER", the output shape is not the same as input.
if x.shape is {1,1,7,5}, strides={2,2}, then the output should be {1,1,7,5}, but I got {1,1,4,3}

(shapes (1, 1, 7, 5), (1, 1, 4, 3) mismatch)

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu16.04
  • ONNX Runtime installed from (source or binary): pip install onnxruntime
  • ONNX Runtime version: 0.2.1
  • Python version: 3.6
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

To Reproduce
Describe steps/code to reproduce the behavior:

import onnx
# import onnxruntime as onnxrt
import onnxruntime.backend as backend 
# from onnx_tf import backend

import numpy as np

def _extract_value_info(arr, name):  # type: (np.ndarray, Text) -> onnx.ValueInfoProto
    return onnx.helper.make_tensor_value_info(
        name=name,
        elem_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[arr.dtype],
        shape=arr.shape)


def expect(node,  # type: onnx.NodeProto
           inputs,  # type: Sequence[np.ndarray]
           outputs,  # type: Sequence[np.ndarray]
           name,  # type: Text
           **kwargs  # type: Any
           ):  # type: (...) -> None
    present_inputs = [x for x in node.input if (x != '')]
    present_outputs = [x for x in node.output if (x != '')]
    inputs_vi = [_extract_value_info(arr, arr_name)
                 for arr, arr_name in zip(inputs, present_inputs)]
    outputs_vi = [_extract_value_info(arr, arr_name)
                  for arr, arr_name in zip(outputs, present_outputs)]
    graph = onnx.helper.make_graph(
        nodes=[node],
        name=name,
        inputs=inputs_vi,
        outputs=outputs_vi)
    kwargs[str('producer_name')] = 'backend-test'
    model_def = onnx.helper.make_model(graph, **kwargs)
    onnx.checker.check_model(model_def)
    pm = backend.prepare(model_def)
    outs = list(pm.run(inputs))
    for ref_o, o in zip(outputs, outs):
        np.testing.assert_almost_equal(ref_o, o)

def export_conv_with_strides():  # type: () -> None

        x = np.array([[[[0., 1., 2., 3., 4.],  # (1, 1, 7, 5) input tensor
                        [5., 6., 7., 8., 9.],
                        [10., 11., 12., 13., 14.],
                        [15., 16., 17., 18., 19.],
                        [20., 21., 22., 23., 24.],
                        [25., 26., 27., 28., 29.],
                        [30., 31., 32., 33., 34.]]]]).astype(np.float32)
        W = np.array([[[[1., 1., 1.],  # (1, 1, 3, 3) tensor for convolution weights
                        [1., 1., 1.],
                        [1., 1., 1.]]]]).astype(np.float32)

        # Convolution with strides=2 and padding only along one dimension (the H dimension in NxCxHxW tensor)
        node_with_asymmetric_padding = onnx.helper.make_node(
            'Conv',
            inputs=['x', 'W'],
            outputs=['y'],
            kernel_shape=[3, 3],
            # pads=[1, 0, 1, 0],
            auto_pad="SAME_UPPER",
            strides=[2, 2],  # Default values for other attributes: dilations=[1, 1], groups=1
        )
        y_with_asymmetric_padding = np.array([[[[0., 1., 2., 3., 4.],  # (1, 1, 7, 5) input tensor
                        [5., 6., 7., 8., 9.],
                        [10., 11., 12., 13., 14.],
                        [15., 16., 17., 18., 19.],
                        [20., 21., 22., 23., 24.],
                        [25., 26., 27., 28., 29.],
                        [30., 31., 32., 33., 34.]]]]).astype(np.float32)
        expect(node_with_asymmetric_padding, inputs=[x, W], outputs=[y_with_asymmetric_padding],
               name='test_conv_with_strides_and_asymmetric_padding')


if __name__ == "__main__":
    export_conv_with_strides()

Expected behavior
A clear and concise description of what you expected to happen.
output shape should be {1,1,7,5}

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions