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

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

问题:<built-in method locked of _thread.lock object at 0x000000002A2E7328>returned a result with an error set
原因分析:Python文件执行时间太长耽误了C++主线程

解决办法:C++启动多线程调用Python(仍然无法解决)
另外的方法:一条一条执行每条python指令(已解决)
这样就可以运行处模型结果
#include "workthread.h" #include <QtDebug> #include <Python.h> #include <QMutex> QMutex mutex; WorkThread::WorkThread() {
} void WorkThread::run() { mutex.lock(); Py_Initialize(); if ( !Py_IsInitialized() ) { qDebug()<<"python初始化失败"; } else {
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"); if (!pModule) { qDebug()<< "Cant open python file!\n" << endl; } else { PyObject* pFunhello= PyObject_GetAttrString(pModule,"hello"); PyRun_SimpleString("print('!!!!!!!')"); if(!pFunhello) { qDebug()<<"Get function hello failed"<< endl; } PyObject_CallFunction(pFunhello,NULL); } Py_Finalize(); } mutex.unlock(); }
|
有任何疑问和想法,欢迎在评论区与我交流。