作者:阿chai
转自:阿chai带你学AI
我是来自山区、朴实、不偷电瓶的AI算法工程师阿chai,给大家分享人工智能、自动驾驶、机器人、3D感知相关的知识
树莓派是一款优秀的卡片式电脑,是很多创客玩家必修的心法。对于想要将深度学习模型部署的小伙伴来说,树莓派是我们绝对首选的设备,但是树莓派毕竟是嵌入式,嵌入式上搭建环境经常很多坑,今天阿chai给大家整理一下树莓派上安装深度学习推理框架的过程,希望对大家做模型部署有帮助。
这里先说明一下,阿chai测试的平台是4B、3B,其他的树莓派平台未测试过,系统用的是官方的raspbian OS与Lite,Ubuntu Mate可能会出一些问题。
1.Caffe
安装依赖:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compilersudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install libopenblas-dev liblapack-dev libatlas-base-devsudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
clone Caffe的代码:
git clone https://github.com/BVLC/caffe.git
进入Caffe的工程文件夹打开配置文件,工程中包含一个Makefile的例子,我们需将它复制并修改:
cp Makefile.config.example Makefile.confignano Makefile.config
使用的CPU版本:
# CPU_ONLY := 1
CPU_ONLY := 1
编译(不要多开):
mkdir build && cd build
cmake..
make sudo make install
2.Caffe2
现在Caffe2集成在了PyTorch里面,所以需要clone PyTorch的项目。
git clone https://github.com/pytorch/pytorch.git
我们看一下官方提供的编译脚本build\_raspbian.sh
CAFFE2_ROOT="$( cd "$(dirname -- "$0")"/.. ; pwd -P)"echo "Caffe2 codebase root is: $CAFFE2_ROOT"BUILD_ROOT=${BUILD_ROOT:-"$CAFFE2_ROOT/build"}mkdir -p $BUILD_ROOTecho "Build Caffe2 raspbian into: $BUILD_ROOT"# obtain dependencies.echo "Installing dependencies."sudo apt-get install \ cmake \ libgflags-dev \ libgoogle-glog-dev \ libprotobuf-dev \ libpython-dev \ python-pip \ python-numpy \ protobuf-compiler \ python-protobuf# python dependenciessudo pip install hypothesis# Now, actually build the raspbian target.echo "Building caffe2"cd $BUILD_ROOT# Note: you can add more dependencies above if you need libraries such as# leveldb, lmdb, etc.cmake "$CAFFE2_ROOT" \ -DCMAKE_VERBOSE_MAKEFILE=1 \ -DCAFFE2_CPU_FLAGS="-mfpu=neon -mfloat-abi=hard" \ || exit 1# Note: while Raspberry pi has 4 cores, running too many builds in parallel may# cause out of memory errors so we will simply run -j 2 only.make -j 2 || exit 1
可以根据自己的需求把里面的部分进行修改,比如对python的添加等,如果想要使用python调用直接运行即可。
bash build_raspbian.sh
3.opencv DNN
下载opencv与opencv-contrib:
git clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git
下载慢可以使用sftp的put命令从本地上传到树莓派。
安装相关依赖:
sudo apt-get install devscripts debhelper cmake libgtkmm-3.0-dev libarchive-dev libcurl4-openssl-dev intltoolsudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev libblas-dev libeigen{2,3}-dev liblapack-dev gfortran
编译
cd opencv-mastermkdir build && cd buildcmake -D CMAKE_BUILD_TYPE=RELEASE \-D CMAKE_INSTALL_PREFIX=/usr/local \-D OPENCV_GENERATE_PKGCONFIG=ON \-D INSTALL_PYTHON_EXAMPLES=ON \-D INSTALL_C_EXAMPLES=ON \-D OPENCV_ENABLE_NONFREE=ON \-D OPENCV_EXTRA_MODULES_PATH= 你的路径/opencv_contrib/modules \-D BUILD_EXAMPLES=ON ..make -j4sudo make install
4.TF-Lite
TensorFlow官方提供了非常不错的树莓派上build的教程,过程非常的简单。
编译前请先安装opencv。
首先clone下来:
git clone https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi.git
我们修改一下build文件:
nano get_pi_requirements.sh
将opencv的版本切换为自己需要的,比如4.X,一般来说不会有什么大的影响。
修改后我们直接执行build脚本即可:
bash get_pi_requirements.sh
5.Tengine
编译前请先安装opencv。
a.直接编译
在树莓派上卸载源码(如果网速慢请切换成gitee):
git clone -b tengine-lite https://github.com/OAID/Tengine.git Tengine-Lite
编译:
cd Tengine-Litemkdir build cd buildcmake ..makemake install
b.PC上交叉编译
Arm64 Linux 交叉编译
cd Tengine-Litemkdir build cd buildcmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake ..makemake install
Arm32 Linux 交叉编译
cd Tengine-Litemkdir build cd buildcmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake ..makemake install
编译完成后会生成 libtengine-lite.so
文件,并且会把相关的头文件、libtengine-lite.so
文件和相关的测试程序复制到 build/install
目录中。
6.NCNN
编译前请先安装opencv。
安装依赖:
sudo apt-get install -y git cmakesudo apt-get install -y gfortransudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compilersudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libatlas-base-dev
编译(vulkan得等等,树莓派的这个很玄学):
mkdir build && cd buildcmake -DCMAKE_VERBOSE_MAKEFILE=1 -DCAFFE2_CPU_FLAGS="-mfpu=neon -mfloat-abi=hard" ..make sudo make install
7.MNN
编译前请先安装opencv。
安装依赖:
sudo apt-get install autoconf automake libtool curl cmake g++
编译protobuf:
git clone https://github.com/protocolbuffers/protobuf.gitcd protobufgit submodule update --init --recursive./autogen.sh./configuremakemake checksudo make installsudo ldconfig
编译MNN:
git clone https://gitee.com/mirrors/mnn.git (用的码云,git太慢)cd MNN/./schema/generate.shmkdir buildcd buildcmake .. -DMNN_BUILD_CONVERTER=true && make -j4
8.Paddle-Lite
编译前请先安装opencv。
安装依赖:
sudo apt-get install gcc g++ git wget python
CMake版本要高于3.10。
wget https://www.cmake.org/files/v3.10/cmake-3.10.3.tar.gztar -zxvf cmake-3.10.3.tar.gzcd cmake-3.10.3./configuremakesudo make install
编译:
git clone https://github.com/PaddlePaddle/Paddle-Lite.gitcd Paddle-Lite/lite/tools/bash build_linux.sh --arch=armv7hf
相关阅读
关注元峰微信号“AIZOOTech”
更多算法模型相关请关注AIZOO专栏