繁体   English   中英

成功将 Midas Model 转换为 ONNX 后的 Onnxruntime 测试错误

[英]Onnxruntime Test Error after Successfully Converting Midas Model to ONNX

最后,我问我的第一个问题。 过去几天我一直在为此苦苦挣扎,在这里找不到同样的问题。

我使用以下代码将此预训练的 model转换为 ONNX:

import torch
from midas import midas_net
import onnx

model_path = "model-f46da743.pt"
model = midas_net.MidasNet(model_path, non_negative=True)
sample = torch.randn(1,3,384,672) # input_shape
torch.onnx.export(model,
                  sample,
                  "midas.onnx",
                  opset_version=11)

onnx_model = onnx.load("midas.onnx")
# Print a human readable representation of the graph
graph_output = onnx.helper.printable_graph(onnx_model.graph)
with open("graph.txt", mode="w") as fout:
    fout.write(graph_output)

我用 Netron 打开了 midas.onnx,它看起来不错。 然后我尝试使用 onnxruntime 对其进行测试:

import torch
import utils
import cv2
from torchvision.transforms import Compose
from midas import transforms
import onnxruntime # Tool for scoring ONNX models
import onnx

# select device
device = torch.device("cpu")
# print("device: %s" % device)

model = "/home/ipu/libraries/MiDaS/midas.onnx"
sess = onnxruntime.InferenceSession(model) # Load a ONNX model
# print(sess) # <onnxruntime.capi.session.InferenceSession object at 0x7f07d5e10f10>

input_name = sess.get_inputs()[0].name
# print(input_name) # input.1

input_shape = sess.get_inputs()[0].shape
# print("input shape", input_shape) # [1, 3, 384, 672]

output_name = sess.get_outputs()[0].name
# print(output_name) # 1176

output_shape = sess.get_outputs()[0].shape
# print("output shape", output_shape) # [1, 384, 672]

# Check that the Intermediate Representation (IR) is well formed
# print("check onnx model ", onnx.checker.check_model(onnx.load(model))) # None

transform = Compose(
    [
        transforms.Resize(
            384,
            384,
            resize_target=None,
            keep_aspect_ratio=True,
            ensure_multiple_of=32,
            resize_method="lower_bound",
            image_interpolation_method=cv2.INTER_CUBIC,
        ),
        transforms.NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
        transforms.PrepareForNet(),
    ]
)

# get input
img = utils.read_image("input/20200629_123103.jpg")
img_input = transform({"image": img})["image"]

sample = torch.from_numpy(img_input).to(device).unsqueeze(0)
# print("sample", sample)
# print("sample shape", sample.shape) # torch.Size([1, 3, 384, 672])
# print(type(sample)) # <class 'torch.Tensor'>

def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()

# print("to numpy sample", to_numpy(sample))
# print("to numpy sample shape", to_numpy(sample).shape) # (1, 3, 384, 672)
# print(type(to_numpy(sample))) # <class 'numpy.ndarray'>

# Input must be a list of dictionaries or a single numpy array
ort_outs = sess.run([output_name], {input_name: to_numpy(sample)})
print(ort_outs)

但是,我得到了这个错误:

2020-07-15 10:55:23.402764485 [E:onnxruntime:, sequential_executor.cc:281 Execute] Non-zero status code returned while running Resize node. Name:'' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/tensor/upsample.h:283 void onnxruntime::UpsampleBase::ScalesValidation(const std::vector<float>&, onnxruntime::UpsampleMode) const scales.size() == 2 || (scales.size() == 4 && scales[0] == 1 && scales[1] == 1) was false. 'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the Resize operator

Traceback (most recent call last):
  File "simple_test.py", line 73, in <module>
    ort_outs = sess.run([output_name], {input_name: to_numpy(sample)})
  File "/home/ipu/anaconda3/envs/midas/lib/python3.7/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.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running Resize node. Name:'' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/tensor/upsample.h:283 void onnxruntime::UpsampleBase::ScalesValidation(const std::vector<float>&, onnxruntime::UpsampleMode) const scales.size() == 2 || (scales.size() == 4 && scales[0] == 1 && scales[1] == 1) was false. 'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the Resize operator

有关更多信息,我使用 Pytorch (1.3.1)、ONNX (1.7.0) 和 Onnxruntime (1.3.0)

我真的很感激任何帮助。

你能附上onnx model吗? 我似乎无法运行导出脚本。 它失败并出现此错误“ImportError:无法导入名称'midas_net'”。 此外,该错误是不言自明的。 这里 scales 是这里提到的 scales 数组: https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Resize-11 scales 输入是否与错误消息中提到的约束匹配?

我通过将 pytorch 版本从 1.3.1 升级到 1.4.0 解决了这个问题。

conda install pytorch=1.4.0 -c pytorch

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM