目录

Pytorch部署错误

jetson nano部署错误


Pytorch以及其他框架部署错误

1_错误1:torch.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 npdata = np.random.rand(3, 640, 640)
print(data.flatten().shape)    # (1228800,)

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

jetson nano部署错误

2_错误1:onnxruntime.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,onnxruntimefrom net import MainNettorch_model = MainNet()batch_size = 1# Initialize model with the pretrained weights
map_location = lambda storage, loc: storage
if torch.cuda.is_available():map_location = Nonetorch_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 runx,                         # 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 fileopset_version=12,          # the ONNX version to export the model todo_constant_folding=True,  # whether to execute constant folding for optimizationinput_names = ['input'],   # the model's input namesoutput_names = ['output'], # the model's output namesdynamic_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_错误2:pycuda._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

【深度学习】模型部署的错误整理相关推荐

  1. 为什么将表格的method改为post后就无法工作_用Python将Keras深度学习模型部署为Web应用程序...

    构建一个很棒的机器学习项目是一回事,但归根结底,你希望其他人能够看到你的辛勤工作.当然,你可以将整个项目放在GitHub上,但是怎么让你的祖父母也看到呢?我们想要的是将深度学习模型部署为世界上任何人都 ...

  2. 面向生产环境!深度学习模型部署资源全辑

    点上方计算机视觉联盟获取更多干货 仅作学术分享,不代表本公众号立场,侵权联系删除 转载于:机器学习实验室   Author:louwill Machine Learning Lab AI博士笔记系列推 ...

  3. 深度学习模型部署学习一

    深度学习模型部署 学习链接:模型部署入门教程(一):模型部署简介 写在前面: 本文档为学习上述链接的相关记录,基本内容一致,仅用于学习用途,若侵权请联系我删除 目   录 深度学习模型部署 1 为什么 ...

  4. 深度学习模型部署技术方案

    深度学习模型部署技术方案 训练好的深度学习模型如何进行部署的相关技术方案 1 什么是模型部署? 2 数据科学项目整个开发流程 3 使用flask 将 Keras深度学习模型部署为Web应用程序 4 T ...

  5. 深度学习模型部署之模型优化

    文章目录 前言 模型剪枝 MNIST 常规训练 Setup 常规训练模型 模型评估 Pruning 模型定义 训练模型 评估模型 pruning your model API prune_low_ma ...

  6. 深度学习模型部署简要介绍

    一.模型部署简介 近几年来,随着算力的不断提升和数据的不断增长,深度学习算法有了长足的发展.深度学习算法也越来越多的应用在各个领域中,比如图像处理在安防领域和自动驾驶领域的应用,再比如语音处理和自然语 ...

  7. 如何将深度学习模型部署到实际工程中?(分类+检测+分割)

    应用背景介绍 早在遥远的1989年,一家叫做ALVIVN的公司首次将神经网络用在汽车上,进行车道线检测和地面分割.时至今日,深度学习已经应用在自动驾驶系统的多个分支领域.首先是感知领域,常用的传感器有 ...

  8. 【深度学习】基于web端和C++的两种深度学习模型部署方式

    深度学习 Author:louwill Machine Learning Lab 本文对深度学习两种模型部署方式进行总结和梳理.一种是基于web服务端的模型部署,一种是基于C++软件集成的方式进行部署 ...

  9. pytorch基于web端和C++的两种深度学习模型部署方式

    本文对深度学习两种模型部署方式进行总结和梳理.一种是基于web服务端的模型部署,一种是基于C++软件集成的方式进行部署. 基于web服务端的模型部署,主要是通过REST API的形式来提供接口方便调用 ...

  10. 【项目实战课】从零掌握安卓端Pytorch原生深度学习模型部署

    欢迎大家来到我们的项目实战课,本期内容是<从零掌握安卓端Pytorch原生深度学习模型部署>.所谓项目课,就是以简单的原理回顾+详细的项目实战的模式,针对具体的某一个主题,进行代码级的实战 ...

最新文章

  1. 图片管理之获取图片列表数据
  2. PHP 选取数组中最大的 键 和 值
  3. micro-mvc框架支持mvc各层业务代码热部署
  4. 2015年4月8日主从不同步故障解决(字符集导致)
  5. 一个能描述erp系统的小故事。
  6. 1.常用字符对象方法
  7. AttributeError: 'NoneType' object has no attribute '__array_interface__'
  8. 你还不会手写SpringBoot启动器吗
  9. mac终端操作文件或文件夹(持续更新)
  10. 从DevOps到Cloud Native,应用上云姿势全解锁 1
  11. 关于Spring Cloud Netflix
  12. mp4 avc格式_sps_pps
  13. 智能指针是一种类,别名称为句柄类
  14. Vrep/CoppeliaSim:基础操作(1)
  15. 刘宇凡:从吃饭中的道理领悟SEO
  16. 一个简单的爬虫例子-天气
  17. 读了100多本书只向你推荐这6本(豆瓣评分8.0以上)
  18. springboot整合手机验证码
  19. 从数据库得到数据导出指定格式的xml文件,上传到NC接口,返回回执到本地一个xml文件
  20. iOS开发:在苹果开发者官网回复邮件的时候,上传附件一定要用英文命名的原因

热门文章

  1. matlab各个指令的含义,matlab的常用指令及其含义
  2. 男人常吃6种食物当心伤精少精
  3. 安防监控摄像头接入云端实现直播、录像和大屏展示
  4. Baumer工业相机堡盟相机如何使用Binning像素合并功能( Binning像素合并功能的优点和行业应用)(C++)(C#)
  5. IPS面板和PLS液晶面板区别
  6. jemalloc源码解读(六)基数树
  7. POI生成word文档,图片显示为空白或不显示
  8. ffplay 内存优化
  9. 4.3 脉冲响应不变法
  10. 计算机网络(IP/TCP/HTTP)