MNN学习笔记

中文文档

https://www.yuque.com/mnn/cn

MNN安装

MNN源码:https://github.com/alibaba/MNN/tree/master/tools/converter
先安装3.0以上版本的protobuf,再安装,基本无坑。
简单测试:

./backendTest.out ../benchmark/models/mobilenet-v1-1.0.mnn 10 0

./MNNConvert -h

模型转换

tensorflow:

./MNNConvert -f TF/ONNX/TFLITE --modelFile XXX.pb/XXX.onnx/XXX.tflite --MNNModel XXX.XX --bizCode XXX

caffe:

./MNNConvert -f CAFFE --modelFile XXX.caffemodel --prototxt XXX.prototxt --MNNModel XXX.XX --bizCode XXX

pytorch:

import torch
import torchvision

dummy_input = torch.randn(10, 3, 224, 224, device='cuda')
model = torchvision.models.alexnet(pretrained=True).cuda()

input_names = [ "actual_input_1" ] + [ "learned_%d" % i for i in range(16) ]
output_names = [ "output1" ]

torch.onnx.export(model, dummy_input, "alexnet.onnx", verbose=True, input_names=input_names, output_names=output_names, do_constant_folding=True)
./MNNConvert -f ONNX --modelFile alexnet.onnx --MNNModel alexnet.mnn --bizCode MNN

推理示例代码

# Copyright @ 2019 Alibaba. All rights reserved.
# Created by ruhuan on 2019.09.09
""" python demo usage about MNN API """
from __future__ import print_function
import numpy as np
import MNN
import cv2
def inference():
    """ inference mobilenet_v1 using a specific picture """
    interpreter = MNN.Interpreter("mobilenet_v1.mnn")
    session = interpreter.createSession()
    input_tensor = interpreter.getSessionInput(session)
    image = cv2.imread('ILSVRC2012_val_00049999.JPEG')
    #cv2.imshow("image",image)
    #cv2.waitKey(0)
    #cv2 read as bgr format
    image = image[..., ::-1]
    #change to rgb format
    image = cv2.resize(image, (224, 224))
    #resize to mobile_net tensor size
    image = image.astype(float)
    image = image - (103.94, 116.78, 123.68)
    image = image * (0.017, 0.017, 0.017)
    #preprocess it
    image = image.transpose((2, 0, 1))
    
    #cv2 read shape is NHWC, Tensor's need is NCHW,transpose it
    tmp_input = MNN.Tensor((1, 3, 224, 224), MNN.Halide_Type_Float,\
                    image, MNN.Tensor_DimensionType_Caffe)
    
    #construct tensor from np.ndarray
    input_tensor.copyFrom(tmp_input)
    interpreter.runSession(session)
    output_tensor = interpreter.getSessionOutput(session)
    print("expect 983")
    print("output belong to class: {}".format(np.argmax(output_tensor.getData())))
if __name__ == "__main__":
    inference()

MNN中NC4HW4格式

https://www.zhihu.com/question/337513515

FlatBuffer

https://www.jianshu.com/p/8eb153c12a4b
借图一用,侵删。
flatbuffer数据结构
在这里插入图片描述
mnn模型
在这里插入图片描述
存储结构
https://www.jianshu.com/p/b1fa70005dbf

MNN量化源码解析

https://zhuanlan.zhihu.com/p/153562409?from_voters_page=true

参考上文写了更详细的:
https://blog.csdn.net/qq_38109843/article/details/107181824

MNN模型转换流程

主流程:https://zhuanlan.zhihu.com/p/124295758
算子转换:https://zhuanlan.zhihu.com/p/124304103

MNN推理过程

在这里插入图片描述
https://zhuanlan.zhihu.com/p/136809881

MNN CPU backend

https://zhuanlan.zhihu.com/p/136813972

MNN推理优化

在这里插入图片描述
1.https://zhuanlan.zhihu.com/p/136801718
2.https://zhuanlan.zhihu.com/p/136806154
3.https://zhuanlan.zhihu.com/p/136807941

MNN模型定制化转换

https://www.jianshu.com/p/df1868aef2c3

官方文档:https://www.yuque.com/mnn/cn/customize_op

MNN实战YOLOV3部署

https://github.com/wlguan/MNN-yolov3

MNN实战Mobilenet SSD部署

https://zhuanlan.zhihu.com/p/70323042

pymnn

mnnconvert -f ONNX --modelFile resnet18.onnx --MNNModel resnet18.mnn
mnnquant src_mnn dst_mnn config

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值