PaddlePaddle / FastDeploy

⚡️An Easy-to-use and Fast Deep Learning Model Deployment Toolkit for ☁️Cloud 📱Mobile and 📹Edge. Including Image, Video, Text and Audio 20+ main stream scenarios and 150+ SOTA models with end-to-end optimization, multi-platform and multi-framework support.
https://www.paddlepaddle.org.cn/fastdeploy
Apache License 2.0
2.81k stars 441 forks source link

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: scale_factor Got: 1 Expected: 2 Please fix either the inputs or the model. #1345

Open jo-dean opened 1 year ago

jo-dean commented 1 year ago

Environment

算能tpu-mlir容器

FastDeploy version: e.g 0.8.0 or the latest code in develop branch OS Platform: e.g. Linux x64 Hardware: e.g. Nvidia GPU 3060 Program Language: e.g. Python 3.8

Problem description

按照官方文档进行容器的创建,然后是模型转换出现下面问题 `root@ce2aa914b856:/workspace/ppyoloe_crn_s_300e_coco/workspace# model_transform.py --model_name ppyoloe_crn_s_300e_coco --model_def ../ppyoloe_crn_s_300e_coco.onnx --input_shapes [[1,3,640,640],[1,2]] --keep_aspect_ratio --pixel_format rgb --output_names p2o.Div.1,p2o.Concat.29 --test_input ../inputs.npz --test_result ppyoloe_crn_s_300e_coco_top_outputs.npz --mlir ppyoloe_crn_s_300e_coco.mlir SOPHGO Toolchain v0.8.beta.0-20230215 2023/02/16 17:26:44 - INFO :


| preprocess:                                           |
|   (x - mean) * scale                                  |
'-------------------------------------------------------'

config Preprocess args : resize_dims : same to net input dims keep_aspect_ratio : True pad_value : 0 pad_type : center

mean                  : [0.0, 0.0, 0.0]
scale                 : [1.0, 1.0, 1.0]
--------------------------
pixel_format          : rgb
channel_format        : nchw
model_format          : image

Run onnxsim 1 times, model simplified: True Run onnxsim 1 times, model simplified: True Save mlir file: ppyoloe_crn_s_300e_coco_origin.mlir [Running]: tpuc-opt --init --canonicalize --mark-FLOPs --save-weight --mlir-print-debuginfo ppyoloe_crn_s_300e_coco_origin.mlir -o ppyoloe_crn_s_300e_coco.mlir [Success]: tpuc-opt --init --canonicalize --mark-FLOPs --save-weight --mlir-print-debuginfo ppyoloe_crn_s_300e_coco_origin.mlir -o ppyoloe_crn_s_300e_coco.mlir Mlir file generated:ppyoloe_crn_s_300e_coco.mlir Traceback (most recent call last): File "/workspace/tpu-mlir/python/tools/model_transform.py", line 192, in tool.model_validate(args.test_input, args.tolerance, args.excepts, args.test_result) File "/workspace/tpu-mlir/python/tools/model_transform.py", line 65, in model_validate ref_outputs = self.origin_inference(inputs) File "/workspace/tpu-mlir/python/tools/model_transform.py", line 102, in origin_inference return onnx_inference(inputs, self.converter.onnx_file) File "/workspace/tpu-mlir/python/tools/model_runner.py", line 221, in onnx_inference outs = session.run(None, data) File "/usr/local/lib/python3.7/dist-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 200, in run return self._sess.run(output_names, input_feed, run_options) onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: scale_factor Got: 1 Expected: 2 Please fix either the inputs or the model. ` image

KhantiInNaraka commented 1 year ago

您好,请查看inputs.npz是否是2个输入,用np.load('inputs.npz').files即可查看。

jo-dean commented 1 year ago

你好,有2个输入。 image

jo-dean commented 1 year ago

@KhantiInNaraka 您好,有空帮忙看一下吗

xyzeng4657 commented 1 year ago

++ 在按照教程转换自己训练的ppyoloe+模型的时候遇到了相同的问题, 请问现在有什么相关进展么?

root@c84f89456ce4:/workspace/tpu-mlir# model_transform.py \

--model_name ppyoloe_plus_crn_m_80e_coco \ --model_def ppyoloe_plus_crn_m_80e_coco.onnx \ --input_shapes [[1,3,640,640],[1,2]] \ --keep_aspect_ratio \ --pixel_format rgb \ --output_names p2o.Div.1,p2o.Concat.29 \ --test_input inputs.npz \ --test_result ppyoloe_plus_crn_m_80e_coco_top_outputs.npz \ --mlir ppyoloe_plus_crn_m_80e_coco.mlir SOPHGO Toolchain v0.8.beta.0-71-g7e4ee67f-20230302 2023/03/02 15:42:44 - INFO :


| preprocess: | | (x - mean) * scale | '-------------------------------------------------------' config Preprocess args : resize_dims : same to net input dims keep_aspect_ratio : True pad_value : 0 pad_type : center

mean : [0.0, 0.0, 0.0] scale : [1.0, 1.0, 1.0]

pixel_format : rgb channel_format : nchw model_format : image

Run onnxsim 1 times, model simplified: True Run onnxsim 1 times, model simplified: True Save mlir file: ppyoloe_plus_crn_m_80e_coco_origin.mlir [Running]: tpuc-opt --init --canonicalize --mark-FLOPs --save-weight --mlir-print-debuginfo ppyoloe_plus_crn_m_80e_coco_origin.mlir -o ppyoloe_plus_crn_m_80e_coco.mlir %584 = "top.Slice"(%583) {offset = [0, 0, 0], steps = [1, 1, 1]} : (tensor<1x8400x4xf32>) -> tensor<1x8400x2xf32> loc("split_0.tmp_0_Split") %584 = "top.Slice"(%583) {offset = [0, 0, 0], steps = [1, 1, 1]} : (tensor<1x8400x4xf32>) -> tensor<1x8400x2xf32> loc("split_0.tmp_0_Split") [Success]: tpuc-opt --init --canonicalize --mark-FLOPs --save-weight --mlir-print-debuginfo ppyoloe_plus_crn_m_80e_coco_origin.mlir -o ppyoloe_plus_crn_m_80e_coco.mlir Mlir file generated:ppyoloe_plus_crn_m_80e_coco.mlir Traceback (most recent call last): File "/workspace/tpu-mlir/python/tools/model_transform.py", line 192, in tool.model_validate(args.test_input, args.tolerance, args.excepts, args.test_result) File "/workspace/tpu-mlir/python/tools/model_transform.py", line 65, in model_validate ref_outputs = self.origin_inference(inputs) File "/workspace/tpu-mlir/python/tools/model_transform.py", line 102, in origin_inference return onnx_inference(inputs, self.converter.onnx_file) File "/workspace/tpu-mlir/python/tools/model_runner.py", line 236, in onnx_inference outs = session.run(None, data) File "/usr/local/lib/python3.7/dist-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 200, in run return self._sess.run(output_names, input_feed, run_options) onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: scale_factor Got: 1 Expected: 2 Please fix either the inputs or the model.

xyzeng4657 commented 1 year ago

参考paddle2onnx,我对生成.npz文件部分进行了以下修改:

import cv2
import numpy as np

im = cv2.imread('000000014439.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
#[640 640]为ppyoloe_crn_s_300e_coco的输入大小
im_scale_y = 640 / float(im.shape[0])
im_scale_x = 640 / float(im.shape[1])
im = cv2.resize(
            im, None, None, fx=im_scale_x, fy=im_scale_y)
print(im.shape)
im = np.transpose(im,(2,0,1)) 
# or np.transpose(im, (2,1,0))??
print(im.shape)
inputs = {}
inputs['image'] = np.array((im, )).astype('float32')
inputs['scale_factor'] = np.array(([im_scale_y, im_scale_x],)).astype('float32')
np.savez('inputs.npz', image = inputs['image'], scale_factor = inputs['scale_factor'])

请问在这种情况下模型输入image的通道顺序是否是正确的呢?

srd2018 commented 11 months ago

@jo-dean 请问你解决了吗?