MNN笔记

自定义graph

当网络使用转换工具转到一个平台时, 经常会遇到部分算子不支持的问题(比如一些网络的后处理部分), 这时可以考虑白剽MNN的算子. 这里以softmax为例.
假如你的分类网络(比如resent)的主要部分能够在gpu上推理,但是后面的softmax却不支持, 那么你可以使用MNN的expression库来实现, 使用cpu后端的话支持的算子当然多, 而且针对嵌入式平台也有优化. 实现代码如下:

#include <MNN/expr/Expr.hpp>
#include <MNN/expr/ExprCreator.hpp>
#include <MNN/expr/Executor.hpp>

//-------lyy
#include "MNN_generated.h"
#include <MNN/MNNForwardType.h>
#include <MNN/Interpreter.hpp>

#include <MNN/expr/Expr.hpp>
#include <MNN/MNNDefine.h>

void run_my_ops(){
    MNN_PRINT("test my ops");
    // define your graph
    auto x1 = _Input({1, 2, 3, 4}, NCHW);
    auto x = _Softmax(x1, -1);
    
    // auto x2 = _Input({1, 2, 3, 4}, NCHW);
    // auto x = _Add(x1, x2);
    // x = _Softmax(x, -1);

    std::unique_ptr<MNN::NetT> netTable(new MNN::NetT);
    MNN::Express::Variable::save({x}, netTable.get());
    flatbuffers::FlatBufferBuilder builder(1024);
    auto offset = CreateNet(builder, netTable.get());
    builder.Finish(offset);
    const void* buf = builder.GetBufferPointer();
    size_t size = builder.GetSize();
    std::unique_ptr<MNN::Interpreter> net(MNN::Interpreter::createFromBuffer(buf, size));
    net->setSessionMode(MNN::Interpreter::Session_Release);
    MNN::ScheduleConfig config;
    auto session = net->createSession(config);
    net->releaseModel();
    auto inputTensor = net->getSessionInput(session, NULL);
    std::shared_ptr<MNN::Tensor> inputTensorHost(MNN::Tensor::createHostTensorFromDevice(inputTensor, false));
    int eleSize = inputTensorHost->elementSize();
    for (int i = 0; i < eleSize; ++i) {
        inputTensorHost->host<float>()[i] = float(i);
    }
    MNN_PRINT("Input tensor:");
    inputTensorHost->print();
    auto outputTensor = net->getSessionOutput(session, NULL);
    std::shared_ptr<MNN::Tensor> outputTensorHost(MNN::Tensor::createHostTensorFromDevice(outputTensor, false));

    inputTensor->copyFromHostTensor(inputTensorHost.get());
    net->runSession(session);
    outputTensor->copyToHostTensor(outputTensorHost.get());
    MNN_PRINT("output tensor:");
    outputTensorHost->print();
}

参考资料:
MNN仓库:

benchmark/exprModels/SqueezeNetExpr.hpp
benchmark/benchmarkExprModels.cpp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值