Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use batchsize in onnxruntime? #5577

Closed
synsis opened this issue Oct 23, 2020 · 1 comment
Closed

How to use batchsize in onnxruntime? #5577

synsis opened this issue Oct 23, 2020 · 1 comment

Comments

@synsis
Copy link

synsis commented Oct 23, 2020

Describe the bug
I read the sample of imagenet and the Q&A(#1632), but I still do not know how to use batchsize.
I think the sample means that the session itself do not care the shape of the input, just set the input_tensor the proper shape with the batchsize, and run the session, like the code below:
auto output_tensors = session.Run(Ort::RunOptions{nullptr}, input_node_names.data(), &input_tensor, 1, output_node_names.data(), 2);

But, it doesn't work for me in the current version. It gives me some errors like:
terminate called after throwing an instance of 'Ort::Exception'
what(): Got invalid dimensions for input: data for the following indices
index: 0 Got: 4 Expected: 1
Please fix either the inputs or the model.

I deleted the check code and re-make the whole project.
But it still doesn't work. In output tensor, it just gives me random result except the first data in a batchsize.

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 18.04):
  • ONNX Runtime installed from (source or binary): source
  • ONNX Runtime version:1.5.2
  • GCC/Compiler version (if compiling from source): g++ 7.5.0
  • CUDA/cuDNN version:10.2
  • GPU model and memory: 1660Ti 6G
@hariharans29
Copy link
Member

"I think the sample means that the session itself do not care the shape of the input, just set the input_tensor the proper shape with the batchsize," - That is true but your model should also support it. In this case, your model does not - it is not flexible in its requirement - it expects batch size to be 1 and you provided 4.

Usually the model input has a "dynamic" shape for the batch dimension (see #2118 #1944). I think some frameworks (Torch for example) support exporting the model with "dynamic" input shape requirements that can be used for batched inferencing.

@snnn snnn closed this as completed Mar 19, 2021
yan12125 pushed a commit to EMCLab-Sinica/Stateful-CNN that referenced this issue Oct 14, 2021
- use onnxruntime to run all models
- implement dumping intermediate results
- implement batch inference. Apparently onnxruntime does not support
  it directly if the ONNX model is not exported with a dynamic batch
  size [1]. I rewrite the model to work-around it.
- Split out model configs so that it can be used in both transform.py
  and original_model_run.py

Other changes: implement start for load_data_mnist()

[1] microsoft/onnxruntime#5577
yan12125 pushed a commit to EMCLab-Sinica/Stateful-CNN that referenced this issue Oct 24, 2021
* Add an external MAML implementation as a submodule
* Load omniglot
* Model rewriting in transform.py
  - Change inputs for KWS to eliminate unsupported operators from the
    final ONNX model.
  - Save models at each step of rewriting for debugging
  - Inference dynamic shapes to get rid of a complex graph from forward
    steps like `x = x.view(x.size(0), -1)`
  - Constant folding for Squeeze and Reshape nodes with known new shape
    and constant input
  - Reduce global variables so that the latest model is always used
    after model rewriting
* Other changes in transform.py
  - Move more ONNX helpers to utils.py
  - Make transformation of input samples more robust in terms of input
    shape
  - Don't use a default batch size; make that argument required.
* Implement BatchNormalization
* Use libc_nano instead of libc as the model omniglot-maml is too large

[1] onnx/optimizer#38
[2] microsoft/onnxruntime#5577
yan12125 pushed a commit to EMCLab-Sinica/Stateful-CNN that referenced this issue Apr 5, 2022
* Add an external MAML implementation as a submodule
* Load omniglot
* Model rewriting in transform.py
  - Change inputs for KWS to eliminate unsupported operators from the
    final ONNX model.
  - Save models at each step of rewriting for debugging
  - Inference dynamic shapes to get rid of a complex graph from forward
    steps like `x = x.view(x.size(0), -1)`
  - Constant folding for Squeeze and Reshape nodes with known new shape
    and constant input
  - Reduce global variables so that the latest model is always used
    after model rewriting
* Other changes in transform.py
  - Move more ONNX helpers to utils.py
  - Make transformation of input samples more robust in terms of input
    shape
  - Don't use a default batch size; make that argument required.
* Implement BatchNormalization
* Use libc_nano instead of libc as the model omniglot-maml is too large

[1] onnx/optimizer#38
[2] microsoft/onnxruntime#5577
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants