码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    C++ 端进行调用

    c++环境配置
    libtorch常用API

    #include 
    #include 
    
    #include 
    
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    可能出错的问题

    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,可能是一个列表或者元组什么的
  • 相关阅读:
    【教学类-18-02】20221124《蒙德里安“红黄蓝黑格子画”-A4竖版》(大班)
    天翼云探索云原生、边缘计算融合新思路
    PHP 操作日期各种转换,常见日期转换,涉及聊天时间转换、涉及日周月年转换、涉及到图表日期转换
    大一学生WEB前端静态网页——唯品会1页 包含hover效果
    CsPbI3钙钛矿量子点 CsPbI3 QDs发射波长670±10nm
    SpringBoot整合Quartz示例
    golang之slice并发访问
    TSINGSEE青犀视频AI算法助力构建城市市容·街面秩序管理解决方案
    ZGC关键技术分析
    c++多态
  • 原文地址:https://blog.csdn.net/u011489887/article/details/133798294
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号