MNN学习笔记

    技术2022-07-15  67

    中文文档

    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

    Processed: 0.013, SQL: 9