私は以前に記事を書き、モデルをトレーニングし、それをmnnモデルに変換し、Pythonを使用して呼び出しました
しかし、それは呼び出すためにc ++を使用しませんでした
モデルは引き続きその記事のモデルを使用しており、呼び出しコードは次のとおりです。
#include <iostream>
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include<MNN/Interpreter.hpp>
#include<MNN/ImageProcess.hpp>
using namespace std;
using namespace cv;
using namespace MNN;
int main()
{ auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile("E:\\vs2019\\first\\first\\model-flow.mnn"));//创建解释器
cout << "Interpreter created" << endl;
ScheduleConfig config;
config.numThread = 8;
config.type = MNN_FORWARD_CPU;
auto session = net->createSession(config);//创建session
cout << "session created" << endl;
//读取图片
cv::Mat image = cv::imread("C:\\Users\\Administrator\\Pictures\\397.jpg");
cv::Mat img_resized;
cv::resize(image, img_resized, cv::Size(180,180));
auto inTensor = net->getSessionInput(session, NULL);
auto outTensor = net->getSessionInput(session, NULL);
auto _Tensor = MNN::Tensor::create<float>(inTensor->shape(), NULL, MNN::Tensor::TENSORFLOW);
//cout << _Tensor->elementSize() << endl;
for (int i = 0; i < _Tensor->elementSize(); i++) {
_Tensor->host<float>()[i] = image.data[i];
}
inTensor->copyFromHostTensor(_Tensor);
inTensor->printShape();
//cout << *(inTensor->host<float>()+1) << endl;
//_Tensor->print();
_Tensor->printShape();
//推理
net->runSession(session);
auto output= net->getSessionOutput(session, NULL);
MNN::Tensor feat_tensor(output, output->getDimensionType());
output->copyToHostTensor(&feat_tensor);
feat_tensor.print();
waitKey(0);
return 0;
}
操作の結果は次のとおりです。
データの結果は、前のpython呼び出しの結果と一致しないことに注意してください。データを割り当てるときは、rgbの順序が逆になっている必要があります。
このmemcpyコピーが私のマシンで失敗したため、memcpyを使用して入力テンソルを読み取るために多くの記事を使用しない理由データはシステムによって自動的に初期化された値であり、毎回結果に一貫性がなくなります、これは乱数です。