【问题标题】:Run inference using ONNX model in python input incompatibility problem?在 python 输入不兼容问题中使用 ONNX 模型运行推理?
【发布时间】:2020-06-14 12:38:51
【问题描述】:

我是编程初学者,我正在尝试运行“tinyyolov2-8.onnx”模型,我在输入格式化方面遇到了困难,谁能建议如何为这个模型格式化输入。代码如下,

import numpy as np
from PIL import Image
import tensorflow as tf



sess_ort = ort.InferenceSession("tinyyolov2-8.onnx")
# sess_ort = ort.InferenceSession("mobilenetv2-7 .onnx")

in__1 = sess_ort.get_inputs()[0].name;
print(in__1)

out__1= sess_ort.get_outputs()[0].name;
img= np.array(Image.open('416_416.png'), np.float)
img= img/255;

# img = np.random.random((1,3,416,416)).astype(np.float32)
img= img.reshape(1,3,416,416);
print(np.shape(img))
res =  sess_ort.run(out__1, {in__1 : img})[0]```

ERROR is:

```runfile('D:/ANN/ONNX_files/ONNX_model_RUN.py', wdir='D:/ANN/ONNX_files')
image
tensor(float)
['None', 3, 416, 416]

grid
tensor(float)
['None', 125, 13, 13]

(1, 3, 416, 416)
Traceback (most recent call last):

  File "D:\ANN\ONNX_files\ONNX_model_RUN.py", line 47, in <module>
    res =  sess_ort.run(out__1, {in__1 : img})[0]

  File "D:\ProgramData\Anaconda3\envs\tensor_python3_6\lib\site-packages\onnxruntime\capi\session.py", line 111, in run
    return self._sess.run(output_names, input_feed, run_options)

TypeError: run(): incompatible function arguments. The following argument types are supported:
    1. (self: onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession, arg0: List[str], arg1: Dict[str, object], arg2: onnxruntime.capi.onnxruntime_pybind11_state.RunOptions) -> List[object]

Invoked with: <onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession object at 0x000001F4803CE960>, 'grid', {'image': array([[[[0.38823529, 0.71372549, 0.68235294, ..., 0.79607843,```




【问题讨论】:

    标签: python numpy tensorflow onnx onnxruntime


    【解决方案1】:
    help(sess_ort)
    ...
     |  run(self, output_names, input_feed, run_options=None)
     |      Compute the predictions.
     |      
     |      :param output_names: name of the outputs
     |      :param input_feed: dictionary ``{ input_name: input_value }``
     |      :param run_options: See :class:`onnxruntime.RunOptions`.
     |      
     |      ::
     |      
     |          sess.run([output_name], {input_name: x})
    

    'run' 采用输出名称的集合。更新对此的调用:

    res = sess_ort.run([out__1], {in__1 : img})[0]

    另请注意,您很可能正在加载 HWC 格式的图像,而 ONNX 运行时需要 CHW,因此您可能需要对其进行转置(而不仅仅是重塑)。

    即如果您检查 'img' 的形状,它可能是 {416, 416, 3},如果是这样,您需要执行 numpy.transpose(img1, (2, 0, 1)) 使其成为 {3, 416, 416} .之后,您可以重塑为 {1, 3, 416, 416}。

    不知道这一行的目的是什么: img= img/255;

    【讨论】:

    • 如果 Onnx 模型有多个输入怎么办?
    • C++ 接口可以选择为模型生成输入。 python接口是否也存在该选项?类似session_options.generate_input_test_data=True
    猜你喜欢
    • 2018-12-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2020-10-23
    • 2022-07-20
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多