Skip to content

Instantly share code, notes, and snippets.

@mrlonely001
Created February 8, 2018 03:19
Show Gist options
  • Save mrlonely001/063ddef3ad473d5cc3b0e9419e2998a9 to your computer and use it in GitHub Desktop.
Save mrlonely001/063ddef3ad473d5cc3b0e9419e2998a9 to your computer and use it in GitHub Desktop.
ncnn for android
ubuntu 14.04 64bit caffe model -> ncnn model -> ncnn android ->android apk
0、protobuf 安装 //没这玩意儿opencv都编译不过
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
1、opencv 环境搭建 (请自行百度proxychains神器指南)
参考 https://gist.github.com/Da-Capo/78377603e6579006f70d671305b8adba 下的 raspi_install_opencv_without_contrib.sh 文件即可。
需要注意的是proxychains安装之前,需要使用pip安装配置好shadowsocks, 之后如果需要翻墙安装一些包时,在命令前加 proxychains 就可以了。
2、caffe(CPU-Only) 环境搭建
//安装依赖
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install build-essential
sudo apt-get install vim cmake git
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev
sudo apt-get install python-pip
//git clone caffe源码
git clone https://github.com/BVLC/caffe.git
//安装caffe 依赖的python包
cd caffe/python
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
//安装官方指定的依赖
sudo su
for req in $(cat requirements.txt); do pip install $req; done
exit
//修改caffe编译配置
cd ~/caffe
cp Makefile.config.example Makefile.config
vim Makefile.config
<1>将含有CPU-ONLY的一行去掉注释
<2>修改python包目录,这段
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
改为
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include
make all -j4
make test
make runtest
make pycaffe
cd caffe/python
python
import caffe //引入成功即caffe cpu版环境搭建成功
3、ncnn 编译
参考 https://github.com/Tencent/ncnn/wiki/how-to-build
需要注意的是:
<1>Build for Linux x86 ,再 Build for Android
并且在Build for Android时 注释掉上一步打开的 add_subdirectory(examples)
<2>如果在编译是遇到 DCUDA_CUDART_LIBRARY 报错,在cmake 时加下面这行
-DCUDA_CUDART_LIBRARY=/usr/local/cuda/lib64/libcudart.so
到这里,应该能够跑通linux版自己的caffe model转换的ncnn model。
4、Android Studio 环境搭建
参看 https://developer.android.com/studio/install.html
5、ncnn-mobile 修改并嵌入自己模型
项目地址:https://github.com/dangbo/ncnn-mobile
修改嵌入自己model方法:http://blog.csdn.net/linmingan/article/details/77988382
这里需要注意的是:ncnn-mobile 里的squeezencnn_jni.cpp 这个文件中
const float* prob = out.data + out.cstep * j 改为下面这行
const float* prob = out.channel(j) //jni 还不怎么懂,需要再熟悉下
自己model的so包生成方法,参考nihui大神的那条评论 :https://github.com/Tencent/ncnn/issues/70
@Andrawzyf
Copy link

楼主有微信么?想请教你一下ncnn在安卓移植上的问题。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment