【深度学习】模型部署的错误整理

目录

Pytorch部署错误

jetson nano部署错误


Pytorch以及其他框架部署错误

1_错误1torch.nn.modules.module.ModuleAttributeError: 'MainNet' object has no attribute 'copy'

解决方法:重新打包pt文件,进行加载

1_错误2:TypeError: argument for rectangle() given by name ('thickness') and position (4)

解决方法:先把图片的数据类型转为uint8形式,

img=np.ascontiguousarray(img)

然后调用cv2.rectangle()函数。

1_其他1在编写YOLOV5后处理代码时,最好使用numpy中的flatten()函数将图片转换为流

import numpy as np

data = np.random.rand(3, 640, 640) 
print(data.flatten().shape)    # (1228800,)

注:直接使用 np.flatten() Pycharm会找不到

 

jetson nano部署错误

2_错误1onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : Load model from super_resolution.onnx failed:This is an invalid model. Type Error: Type 'tensor(float)' of input parameter (4135) of operator (ConstantOfShape) in node (ConstantOfShape_2526) is invalid.

解决方法:torch.onnx.export中opset_version版本错误,我的环境是Python3.8 + torch1.6 + torchvision 0.7,需要将opset_version=12(Pytorch官网的示例给的10,默认为9,YoloV5源码中给的opset_version=12)

"""
# 打包与测试代码 
# 环境:Python3.8 pytorch1.6  torchvision0.7 
"""

# Some standard imports 
import numpy as np
import torch.onnx
import onnx,onnxruntime

from net import MainNet

torch_model = MainNet()

batch_size = 1

# Initialize model with the pretrained weights
map_location = lambda storage, loc: storage
if torch.cuda.is_available():
    map_location = None

torch_model.load_state_dict(torch.load(r"weights\70.pt", map_location=map_location),)

# set the model to inference mode
torch_model.eval()

x = torch.randn(batch_size, 3, 315, 315, requires_grad=True)
torch_out = torch_model(x)

# Export the model
torch.onnx.export(torch_model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "super_resolution.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=12,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable lenght axes
                                'output' : {0 : 'batch_size'}})


onnx_model = onnx.load("super_resolution.onnx")

onnx.checker.check_model(onnx_model)
print(1)    """ 使用print检测哪里出错了 """

ort_session = onnxruntime.InferenceSession("super_resolution.onnx")

""" 未使用 """
def to_numpy(tensor):
    return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()

# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: x.detach().numpy() }
ort_outs = ort_session.run(None, ort_inputs)
print(2)

# compare ONNX Runtime and PyTorch results
""" Pytorch官网给的测试代码是不符合我的环境版本的,下面是错误实例 """
np.testing.assert_allclose(to_numpy(torch_out), ort_outs[0], rtol=1e-03, atol=1e-05)

""" 这是修改后的 """
np.testing.assert_allclose(torch_out[0].detach().numpy() , ort_outs[0], rtol=1e-03, atol=1e-05)

print("Exported model has been tested with ONNXRuntime, and the result looks good!")

2_错误2pycuda._driver.LogicError: cuMemcpyHtoDAsync failed: invalid argument

原因:TensorRt接收到的图片大小和当初打包ONNX时,占位的“input”大小不一样

解决方法:使用cv2.resize(img, ( w, h)) 进行变形或使用cv2.copyMakeBorder(src, top, bottom, left, right, borderType, dst=None, value=None)进行填充图片

拓展:cv2之padding扩充——cv2.copyMakeBorder

2_错误3:把onnx模型转TensorRT模型的trt模型报错:Your ONNX model has been generated with INT64 weights. while TensorRT does not natively support INT64. Attempting to cast down to INT32. 

解决方法:

1、安装onnx-simplifier

pip install onnx-simplifier 

2、把之前转化的onnx模型转化为占位更少的onnx模型

python -m onnxsim complex.onnx sim.onnx

3、然后在把onnx模型转换为TensorRT的trt模型

更新日志:

2020/11/9 --- 更新1_错误1和1_错误2

2020/11/11 --- 更新2_错误3和1_其他1

2020/12/4 --- 更新2_错误3和1_错误2

作者:阳一子

本文地址:https://blog.csdn.net/qq_279033270/article/details/109583514

你可能感兴趣的:(部署,深度学习,人工智能,计算机视觉)