当前位置: 首页 > news >正文

pytorch 模型部署之Libtorch

Python端生成pt模型文件

net.load(model_path)
net.eval()
net.to("cuda")example_input = torch.rand(1, 3, 240, 320).to("cuda")
traced_model = torch.jit.trace(net, example_input)
traced_model.save("model.pt")output = traced_model(example_input)
# 输出查看是否与c++输出一致。
print(len(output))

C++ 端进行调用

c++环境配置
libtorch常用API

#include <torch/script.h>
#include <torch/torch.h>#include <iostream>int main() {std::cout <<"cuda::is_available():" << torch::cuda::is_available() << std::endl;torch::Tensor tensor = torch::rand({2, 3}).to(at::kCUDA);std::cout << tensor << std::endl;torch::jit::script::Module module;module = torch::jit::load("/home/yang/Documents/demo/opencv/model.pt");// 创建一个示例输入std::vector<torch::jit::IValue> inputs;inputs.push_back(torch::rand({1, 3, 240, 320}).to(at::kCUDA));// 运行模型// at::Tensor output = module.forward(inputs).toTensor();//auto output = module.forward(inputs).toTensorList();auto out = module.forward(inputs);auto tpl = out.toTuple();auto out_ct_hm = tpl->elements()[0].toTensor();out_ct_hm.print();auto out_wh = tpl->elements()[1].toTensor();out_wh.print();// 打印输出//std::cout << output << "\n";}

可能出错的问题

  1. terminate called after throwing an instance of ‘c10::Error’
    what(): open file failed, file path: model.pt (FileAdapter at …/…/caffe2/serialize/file_adapter.cc:11)。 模型路径有问题,使用绝对路径解决。
  2. ‘c10::Error’ what(): isTensor() INTERNAL ASSERT FAILED。
    很明显,模型的输出应该不是一个 Tensor,可能是一个列表或者元组什么的
http://www.lryc.cn/news/195551.html

相关文章:

  • Unity——数据存储的几种方式
  • 『heqingchun-ubuntu系统下安装cuda与cudnn』
  • Unity AI Muse 基础教程
  • pgsl基于docker的安装
  • idea设置某个文件修改后所在父文件夹变蓝色
  • 代码随想录训练营二刷第五十八天 | 583. 两个字符串的删除操作 72. 编辑距离
  • 秋日有感之秋诉-于光
  • ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法
  • 【微信小程序】6天精准入门(第2天:小程序的视图层、逻辑层、事件系统及页面生命周期)
  • 速学Linux丨一文带你打开Linux学习之门
  • 符尧:别卷大模型训练了,来卷数据吧!【干货十足】
  • 2023年中国半导体检测仪器设备销售收入、产值及市场规模分析[图]
  • 诊断DLL——Visual Studio安装与dll使用
  • 专业课138,总分390+,西工大,西北工业大学827信号与系统考研分享
  • css3链接
  • 第五章 运输层 | 计算机网络(谢希仁 第八版)
  • CustomTabBar 自定义选项卡视图
  • 卡片翻转效果的实现思路
  • blob和ArrayBuffer格式图片如何显示
  • MySQL学习(四)——事务与存储引擎
  • 3.3 Tessellation Shader (TESS) Geometry Shader(GS)
  • C++:超越C语言的独特魅力
  • 【LeetCode】27. 移除元素
  • AWS SAP-C02教程4--身份与联合身份认证
  • Mybatis Plus入门进阶:特殊符号、动态条件、公共语句、关联查询、多租户插件
  • Webpack 什么是loader?什么是plugin?loader与plugin区别是什么?
  • js面向对象(工厂模式、构造函数模式、原型模式、原型和原型链)
  • grid网格布局,比flex方便太多了,介绍几种常用的grid布局属性
  • 企业如何凭借软文投放实现营销目标?
  • 【AI】深度学习——循环神经网络