MNN环境配置
首先可以通过官网提供的window,mac,linux版本运行库来配置自己的mnn环境 https://github.com/alibaba/MNN。
或者可以自己下载最新的mnn源码根据 https://www.yuque.com/mnn/cn的官方文档来编译出最新的运行库来配置自己的mnn环境,文档中各种版本的编译都很详细,可以根据需要自行编译。
MNN推理示例
配置好环境,准备已经转换好的mnn模型之后开始推理
#include "MNN/Interpreter.hpp"
#include <memory>
#include <string>
#include<iostream>
#include <fstream>
using namespace std;
using namespace MNN; // 使用MNN的命名空间
int main() {
string model_path = "model.mnn";
// 加载模型和配置参数
shared_ptr<Interpreter> net = shared_ptr<Interpreter>(Interpreter::createFromFile(model_path.c_str()));
ScheduleConfig config;
config.numThread = 2;
config.backupType = MNN_FORWARD_CPU;
BackendConfig backendConfig;
backendConfig.memory = BackendConfig::Memory_Normal; // 内存
backendConfig.power = BackendConfig::Power_Normal; // 功耗
backendConfig.precision = BackendConfig::PrecisionMode::Precision_Low; // 精度
config.backendConfig = &backendConfig;
Session* session = net->createSession(config);
// input和output是参于session的计算的输入和输出
std::string input_name = "input";
Tensor* input = net->getSessionInput(session, input_name.c_str());
std::string output_name = "output";
Tensor* output = net->getSessionOutput(session, output_name.c_str());
// 转换自己mnn的输入维度
net->resizeTensor(input, { 1, 131, 80 });
net->resizeSession(session);
Tensor* input_tensor = Tensor::create<float>(input->shape(), NULL, Tensor::CAFFE);
// 将输入一一赋值
for (int i = 0; i < input_tensor->elementSize(); i++) {
input_tensor->host<float>()[i] = 0.5;
}
// 将赋值的tensorcopy给input用户session计算
input->copyFromHostTensor(input_tensor);
// input->printShape();
// 运行会话
net->runSession(session);
// 创建output一样大小和格式的tensor来copy数据
Tensor output_tensor(output, output->getDimensionType());
output->copyToHostTensor(&output_tensor);
// output->print();
for (int i = 0; i < output_tensor.elementSize(); i++) {
cout << output_tensor.host<int>()[i] << endl;
}
net->releaseModel();
net->releaseSession(session);
return 0;
}