vision icon indicating copy to clipboard operation
vision copied to clipboard

ONNX export of MaskRCNN: inference fails when batch size > 1 and no detections

Open zuzaanto opened this issue 6 years ago • 5 comments

🐛 Bug

When running inference with MaskRCNN exported to ONNX with batch size bigger than one, an exception is thrown on images with no detections.

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Non-zero status code returned while running SplitToSequence node. Name:'SplitToSequence_4427' Status Message: split_size_sum (75) != split_dim_size (72)

To Reproduce

Steps to reproduce the behavior: NOTE: input_tensor is of size (4,6,1024,1024) -> batch size is 4

  1. Load and export a pretrained MaskRCNN model:
model = torchvision.models.detection.maskrcnn_resnet50_fpn(
            pretrained=False,
            min_size=1024, max_size=1024,
            pretrained_backbone=False,
            num_classes=num_classnames + 1,  # + background class
            image_mean=image_mean,
            image_std=image_std,
)
torch.onnx.export(
        model,
        input_tensor.float(),
        onnx_model_filepath,
        export_params=True,
        opset_version=12,
        do_constant_folding=False,
        input_names=["images_tensors"],
        output_names=["boxes", "labels", "scores", "masks"],
        dynamic_axes={"images_tensors": [0, 1, 2, 3], "boxes": [0, 1], "labels": [0],
                      "scores": [0], "masks": [0, 1, 2, 3]},
)
  1. Infer on an image that has detections (image's values are in range 0.0-1.0):
input_array = input_tensor.cpu().numpy()
ort_session = onnxruntime.InferenceSession(onnx_model_filepath)
ort_inputs = {"images_tensors": input_array}
ort_outs = ort_session.run(None, ort_inputs)

Works correctly.

  1. Infer on an image that will have no detections, ex. a random one or a black one (making sure that image's values are still in range 0.0-1.0):
random_tensor = torch.randn(input_tensor.shape) 
# also tried:
# random_tensor = torch.zeros(input_tensor.shape) 
random_array = random_tensor.cpu().numpy()
ort_session = onnxruntime.InferenceSession(onnx_model_filepath)
ort_inputs = {"images_tensors": random_array}
ort_outs = ort_session.run(None, ort_inputs)

This throws the exception:

[E:onnxruntime:, sequential_executor.cc:281 Execute] Non-zero status code returned while running SplitToSequence node. Name:'SplitToSequence_4427' Status Message: split_size_sum (61) != split_dim_size (15)
Traceback (most recent call last):
  File "/.../maskrcnn_deployment.py", line 1087, in <module>
    main()
  File "/.../maskrcnn_deployment.py", line 948, in main
    onnx_prediction = infer_onnx_model_on_single_image(
  File "/.../maskrcnn_deployment.py", line 527, in infer_onnx_model_on_single_image
    ort_outs = ort_session.run(None, ort_inputs)
  File "/.../python3.8/site-packages/onnxruntime/capi/session.py", line 111, in run
    return self._sess.run(output_names, input_feed, run_options)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Non-zero status code returned while running SplitToSequence node. Name:'SplitToSequence_4427' Status Message: split_size_sum (61) != split_dim_size (15)

Expected behavior

I would expect analogical behaviour to the one with batch size = 1: empty prediction arrays returned and no exceptions.

Environment

PyTorch version: 1.6.0.dev20200526+cu101
Is debug build: No
CUDA used to build PyTorch: 10.1

OS: Ubuntu 20.04 LTS
GCC version: (Ubuntu 9.3.0-10ubuntu2) 9.3.0
CMake version: version 3.16.3

Python version: 3.8
Is CUDA available: Yes
CUDA runtime version: 10.0.130
GPU models and configuration: 
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti
GPU 2: GeForce RTX 2080 Ti
GPU 3: GeForce RTX 2080 Ti

Nvidia driver version: 440.64
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.4

Versions of relevant libraries:
[pip3] numpy==1.18.4
[pip3] torch==1.6.0.dev20200526+cu101
[pip3] torchvision==0.7.0.dev20200526+cu101
[conda] Could not collect

Also:

ONNX_runtime and ONNX_runtime_gpu==1.3.0
ONNX==1.7.0

Additional context

With batch size = 1 no such error occurs. Also, the number in SplitToSequence error varies depending on the batch size used, ex. if batch size was 2, the error would be:

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Non-zero status code returned while running SplitToSequence node. Name:'SplitToSequence_3575' Status Message: split_size_sum (22) != split_dim_size (6)

Also, it's connected to my previous issue, #2251

zuzaanto avatar Jun 10 '20 12:06 zuzaanto

Hi @zuzaanto

Thanks for opening the issue (and sorry I missed #2251)

@neginraoof can you have a look?

fmassa avatar Jun 10 '20 13:06 fmassa

Thanks for reporting this issue. Looks like issue #2309 and #2311 are similar. I was able to repro this. Will take a look.

neginraoof avatar Jun 15 '20 20:06 neginraoof

Actually looks like this is a known issue in export of models for batch size > 1, and current tests only cover batch size = 1. We have a long term plan to address this limitation.

cc @fmassa

neginraoof avatar Jun 16 '20 21:06 neginraoof

Hi, thanks for your answer. I was not aware of this limitation. Is there any way you could give me a time estimate (rough is ok) of this long term plan? I'm asking because this issue is making MaskRCNN export to ONNX quite unusable to me now.

zuzaanto avatar Jun 17 '20 11:06 zuzaanto

Sorry but we don't have an ETA for support of dynamic batch size > 1. This feature would require changes to model code as well. cc @fmassa FYI, thanks.

neginraoof avatar Jun 22 '20 16:06 neginraoof