C++调用pytorch模型

9 篇文章 0 订阅
3 篇文章 0 订阅

本文节选自我的博客:C++ 调用 pytorch 模型

  • 💖 作者简介:大家好,我是MilesChen,偏前端的全栈开发者。
  • 📝 CSDN主页:爱吃糖的猫🔥
  • 📣 我的博客:爱吃糖的猫
  • 📚 Github主页: MilesChen
  • 🎉 支持我:点赞👍+收藏⭐️+留言📝
  • 💬介绍:The mixture of WEB+DeepLearning+Iot+anything🍁

前言

记一次C++调用pytorch模型踩坑记录,跨语言是AI中急需解决的问题,本文采用#include <Python.h>执行python代码

问题:Cant open python file!

94b0172a-4093-4f54-bd42-71b13fc18bdc.png

解决办法:将python环境的Dlls文件复制过来(通过sys.path排除)

db9a0db2-73ff-4ddb-8320-8d501b80de63.png

问题:<built-in method locked of _thread.lock object at 0x000000002A2E7328>returned a result with an error set

原因分析:Python文件执行时间太长耽误了C++主线程
841e2047-7114-49c7-9fee-2846e8044f3d.png

解决办法:C++启动多线程调用Python(仍然无法解决)

另外的方法:一条一条执行每条python指令(已解决)

这样就可以运行处模型结果

#include "workthread.h"
#include <QtDebug>
#include <Python.h>
#include <QMutex>
QMutex mutex;
WorkThread::WorkThread()
{

}
void WorkThread::run()
{
    mutex.lock();
    Py_Initialize();          //初始化Python解释器
    if ( !Py_IsInitialized() )
    {
        qDebug()<<"python初始化失败";
    }
    else
    {
        //PyRun_SimpleString("import sys");        //执行单句Python语句,用于给出调用模块的路径,否则将无法找到相应的调用模块
        //PyRun_SimpleString("print(sys.path)");                       //在C++中执行python语句

        PyRun_SimpleString("import torch");
        PyRun_SimpleString("import numpy as np");
        PyRun_SimpleString("from torch.autograd import Variable");
        PyRun_SimpleString("from torchvision import transforms");
        PyRun_SimpleString("from PIL import Image");
        PyRun_SimpleString("img = Image.open('1.jpg')");
        PyRun_SimpleString("transform = transforms.Compose([transforms.Resize((300, 300), interpolation=2),transforms.ToTensor(),transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])");
        PyRun_SimpleString("img = transform(img)");
        PyRun_SimpleString("img = img.unsqueeze(0)");
        PyRun_SimpleString("im = Variable(img)");
        PyRun_SimpleString("Resnet_model = torch.load('Resnet18_1.pkl', map_location=lambda storage, loc: storage)");
        PyRun_SimpleString("Resnet_model.eval()");
        PyRun_SimpleString("pre = Resnet_model(im)");
        PyRun_SimpleString("numpy_array = pre.detach().numpy()");
        PyRun_SimpleString("numpy_array = np.squeeze(numpy_array, axis=0)");
        PyRun_SimpleString("output_np = np.argmax(numpy_array, axis=0)");
        PyRun_SimpleString("pre_data = output_np.astype(np.uint8)");
        PyRun_SimpleString("print(pre_data)");
        PyRun_SimpleString("print('end')");

        PyObject* pModule = PyImport_ImportModule("use_model");  // 这里的test_py就是创建的python文件
        if (!pModule)
        {
                qDebug()<< "Cant open python file!\n" << endl;
        }
        else
        {
            PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello");  // 这里的hellow就是python文件定义的函数
            PyRun_SimpleString("print('!!!!!!!')");                       //在C++中执行python语句
            if(!pFunhello)
            {
                qDebug()<<"Get function hello failed"<< endl;
            }
            PyObject_CallFunction(pFunhello,NULL);
        }
         Py_Finalize();
    }
    mutex.unlock();
}

有任何疑问和想法,欢迎在评论区与我交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值