移动端机器学习框架 MDL 简介与实践

这篇文章介绍了百度研发的移动端深度学习框架MDL,用于图像识别,如在手机百度App中的自动识别物体功能。文章详细讲解了如何在Android和iOS平台上使用MDL,包括模型转换、库导入和性能兼容性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mobile-deep-learningMDL

MDL 是百度研发的可以部署在移动端的基于卷积神经网络实现的移动端框架,可以应用在图像识别领域。

具体应用:在手机百度 App 中,用户只需要点击自动拍开关,将手机对准物体,当手稳的时候,它会自动找到物体并进行框选,无需拍照就可以发起图像的搜索功能。

初识 MDL

运行示例程序

  1. clone 项目代码,GitHub - PaddlePaddle/Paddle-Lite: PaddlePaddle High Performance Deep Learning Inference Engine for Mobile and Edge (飞桨高性能深度学习端侧推理引擎) 
  2.  IDE 中导入 example 

  1. 运行

开发要求

  1. 安装 NDK
  2. 安装 Cmake
  3. 安装 protocol buffers

使用 MDL 库

  1. 在 mac/linux 上执行测试

# mac or linux:

./build.sh mac

cd build/release/x86/build

./mdlTest

  1. 在项目中使用mdl

开发

  1. 编译MDL源码(Android

# android:

# prerequisite: install ndk from google

./build.sh android

 cd build/release/armv-v7a/build

./deploy_android.sh

adb shell

cd /data/local/tmp

./mdlTest

  1. iOS

# ios:

# prerequisite: install xcode from apple

./build.sh ios

copy ./build/release/ios/build/libmdl-static.a to your iOS project

模型转换

MDL 需要与之兼容的模型才能使用,可以使用 MDL 提供的脚本将其他深度学习工具训练的模型转换为 MDL 模型。推荐使用 PaddlePaddle 模型。

  1. 将 PaddlePaddle 模型转换成 MDL 模式

# Environmental requirements

# paddlepaddle

cd tools/python

python paddle2mdl.py

  1. 将 caffemodel 模型转换成 MDL 模式

#Convert model.prototxt and model.caffemodel to model.min.json and data.min.bin that mdl use

./build.sh mac

cd ./build/release/x86/tools/build

# copy your model.prototxt and model.caffemodel to this path

./caffe2mdl model.prototxt model.caffemodel

# the third para is optional, if you want to test the model produced by this script, provide color value array of an image as the third parameter ,like this:

./caffe2mdl model.prototxt model.caffemodel data

# the color value should in order of rgb,and transformed according to the model.

# then you will get a new data.min.bin with test data inside

# after this command, model.min.json data.min.bin will be created in current

# some difference step you need to do if you convert caffe model to iOS GPU format

# see this:

open iOS/convert/iOSConvertREADME.md

Android Sample 分析

下面以 Android 平台为例分析 MDL 在移动端平台上面的工作

  1. 在项目中导入 libmdl.so 
  2. 初始化 mdl,加载 so 库,设置线程数量

private void initMDL() {

     String assetPath = "mdl_demo";

     String sdcardPath = Environment.getExternalStorageDirectory()

             + File.separator + assetPath;

     copyFilesFromAssets(this, assetPath, sdcardPath);

     mdlSolver = new MDL();

     try {

         String jsonPath = sdcardPath + File.separator + type.name()

                 + File.separator + "model.min.json";

         String weightsPath = sdcardPath + File.separator + type.name()

                 + File.separator + "data.min.bin";

         Log.d("mdl","mdl load "+ jsonPath + "weightpath ="+weightsPath);

         mdlSolver.load(jsonPath, weightsPath);

         if (type == mobilenet) {

             mdlSolver.setThreadNum(1);

         } else {

             mdlSolver.setThreadNum(3);

         }

     } catch (MDLException e) {

         e.printStackTrace();

     }

 }

  1. 拍摄照片

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// save pic in sdcard 

Uri imageUri = Uri.fromFile(getTempImage());

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_PHOTO_REQUEST_CODE);

  1. 图像处理,包括设置采样率图像缩放
  2. 图像预测

mdlSolver.predictImage(inputData);

  1. 在MDL库中,predictImage 方法是进行图像预测的 JNI 方法。方法声明如下:

JNIEXPORT jfloatArray JNICALL Java_com_baidu_mdl_demo_MDL_predictImage(JNIEnv *env, jclass thiz, jfloatArray buf)

这里需要传入图像的3维数组结构,真正执行预测的是 Net#predict(data) 方法,Net 模块是 MDL 网络管理模块,主要负责网络中各层 Layer 的初始化及管理工作。开发者在调用预测方法的时候,只需要调用对应 java  predictImage 方法,传入图像数据即可。

  1. 预测完成,在 demo 的界面中返回预测耗时和结果。

性能和兼容性

总结

MDL 在 Android  iOS 系统上性能表现十分出色,并且 API 设计也很简单易用,也支持其他的框架模型转换。总体来讲是一个非常优秀的移动端深度学习框架。

参考:

https://github.com/baidu/mobile-deep-learning

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值