【转】在ncnn上把玩mobileNet

转自:https://blog.csdn.net/computerme/article/details/77876633

ncnn是腾讯优图最近开源的适合移动端的深度学习框架。mobileNet是谷歌在2017年4月份发表的论文MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications中提出的网络。由于引入了depthwise convolution,mobileNet的模型非常小,1000类的分类模型只有16.9M,很适合在移动端部署。本文尝试在Mac上用ncnn来运行mobileNet。

1. 下载并编译ncnn

git clone https://github.com/Tencent/ncnn
cd ncnn
mkdir build && cd build
cmake ..
make -j
make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

编译完成后在ncnn/build/tools 目录下,可以看到已经生成了 caffe2ncnn和ncnn2mem这两个可执行文件。caffe2ncnn可的作用是将caffe模型生成ncnn 模型,ncnn2mem可对模型进行加密。

2. 下载MobileNet的caffe模型和配置文件

可从https://github.com/shicai/MobileNet-Caffe中下载,下载后得到mobilenet_deploy.prototxt和mobilenet.caffemodel两个文件。

3. 旧版caffe模型转新版caffe模型

因为ncnn只支持转换新版的caffe模型,所以需要先把第二步下载的caffe模型转换为新版的caffe模型。新版caffe框架中自带了转换的工具,使用姿势如下。

$ ~/caffe/build/tools/upgrade_net_proto_text mobilenet_deploy.prototxt mobilenet_deploy_new.prototxt
$ ~/caffe/build/tools/upgrade_net_proto_binary mobilenet.caffemodel mobilenet_new.caffemodel
  • 1
  • 2

4. 新版caffe模型转ncnn模型

在第一步生成的ncnn/build/tools目录下用caffe2ncnn来转换新版的mobileNet模型。

$./caffe2ncnn mobilenet_deploy_new.prototxt mobilenet_new.caffemodel mobilenet.param mobilenet.bin
  • 1

注意生成的ncnn格式的模型中,.param可以理解为网络的配置文件,.bin可以理解为网络的参数(各种权重)文件。
若需要对模型进行加密,可用如下命令

 $./ncnn2mem mobilenet.param mobilenet.bin mobilenet.id.h mobilenet.mem.h
  • 1

最后可生成 mobilenet.param.bin 这样的二进制加密文件。ncnn对加密和非加密两种文件的读取方式不一样。

//load非加密的ncnn模型
ncnn::Net net;
net.load_param("mobilenet.param");
net.load_model("mobilenet.bin");
//load加密的ncnn模型
ncnn::Net net;
net.load_param_bin("mobilenet.param.bin");
net.load_model("mobilenet.bin");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5. 开工:使用Xcode编写代码运行

使用Xcode新建一个工程,并把第一步中编译完成的ncnn库导入工程中。编译完成的ncnn lib在ncnn/build/install目录下。
配置好ncnn库后,可以借鉴example下面的squeezenet.cpp代码进行mobileNet模型的部署。修改后的代码如下

static int detect_mobileNet(const cv::Mat& bgr, std::vector<float>& cls_scores)
{
    ncnn::Net mobileNet;
    mobileNet.load_param("/Users/Guigu/Documents/projects/ncnn_mobileNet/mobilenet.param");
    mobileNet.load_model("/Users/Guigu/Documents/projects/ncnn_mobileNet/mobilenet.bin");

    ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 224, 224);

    const float mean_vals[3] = {103.94f, 116.78f, 123.68f};
    const float norm_vals[3] = {0.017f,0.017f,0.017f};
    in.substract_mean_normalize(mean_vals, norm_vals);


    ncnn::Extractor ex = mobileNet.create_extractor();
    ex.set_light_mode(true);

    ex.input("data", in);

    ncnn::Mat out;
    ex.extract("fc7", out);  //此处与squeezenet不同

    cls_scores.resize(out.c);
    for (int j=0; j<out.c; j++)
    {
        const float* prob = out.data + out.cstep * j;
        cls_scores[j] = prob[0];
    }

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在main函数中调用该接口就OK了。

下面这张图是我测试的图
这里写图片描述

mobileNet识别的结果如下:

detection time: 852ms
917 = 13.889417 ( comic book)
643 = 13.157956 ( mask)
921 = 7.961194 ( book jacket, dust cover, dust jacket, dust wrapper)
Program ended with exit code: 0
  • 1
  • 2
  • 3
  • 4
  • 5

细心的人可能观察到耗时比较长,原因是用Mac自带的编译器编ncnn的时候不能把openmp编进去。另外,Mac上也不能通过arm neon来加速(毕竟平台不一样嘛)。

完整的工程地址:https://github.com/Revo-Future/ncnn_mobileNet

Bonus

如果想编译源代码,可以把ncnn中src目录下的文件加上build/src下的platform.h layer_registry.h 和layer_declaration.h放到一起替换上面的ncnn lib进行源码的编译研究。

Reference:
http://blog.csdn.net/best_coder/article/details/76201275

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值