机器人与自动驾驶课程请关注深蓝学院
我是来自山区、朴实、不偷电瓶的AI算法工程师阿chai,给大家分享人工智能、自动驾驶、机器人、3D感知相关的知识
树莓派是一款优秀的卡片式电脑,是很多创客玩家必修的心法。对于想要将深度学习模型部署的小伙伴来说,树莓派是我们绝对首选的设备,但是树莓派毕竟是嵌入式,嵌入式上搭建环境经常很多坑,今天阿chai给大家整理一下树莓派上安装深度学习推理框架的过程,希望对大家做模型部署有帮助。
这里先说明一下,阿chai测试的平台是4B、3B,其他的树莓派平台未测试过,系统用的是官方的raspbian OS与Lite,Ubuntu Mate可能会出一些问题。
安装依赖:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo 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.config
nano Makefile.config
使用的CPU版本:
# CPU_ONLY := 1
CPU_ONLY := 1
编译(不要多开):
mkdir build && cd build
cmake ..
make
sudo make install
现在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_ROOT
echo "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 dependencies
sudo 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
下载opencv与opencv-contrib:
git clone https://github.com/opencv/opencv.git
git 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 intltool
sudo 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-master
mkdir build && cd build
cmake -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 -j4
sudo make install
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
编译前请先安装opencv。
在树莓派上卸载源码(如果网速慢请切换成gitee):
git clone -b tengine-lite https://github.com/OAID/Tengine.git Tengine-Lite
编译:
cd Tengine-Lite
mkdir build
cd build
cmake ..
make
make install
Arm64 Linux 交叉编译
cd Tengine-Lite
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/aarch64-linux-gnu.toolchain.cmake ..
make
make install
Arm32 Linux 交叉编译
cd Tengine-Lite
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/arm-linux-gnueabihf.toolchain.cmake ..
make
make install
编译完成后会生成 libtengine-lite.so
文件,并且会把相关的头文件、libtengine-lite.so
文件和相关的测试程序复制到 build/install
目录中。
编译前请先安装opencv。
安装依赖:
sudo apt-get install -y git cmake
sudo apt-get install -y gfortran
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libatlas-base-dev
编译(vulkan得等等,树莓派的这个很玄学):
mkdir build && cd build
cmake -DCMAKE_VERBOSE_MAKEFILE=1 -DCAFFE2_CPU_FLAGS="-mfpu=neon -mfloat-abi=hard" ..
make
sudo make install
编译前请先安装opencv。
安装依赖:
sudo apt-get install autoconf automake libtool curl cmake g++
编译protobuf:
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
编译MNN:
git clone https://gitee.com/mirrors/mnn.git (用的码云,git太慢)
cd MNN/
./schema/generate.sh
mkdir build
cd build
cmake .. -DMNN_BUILD_CONVERTER=true && make -j4
编译前请先安装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.gz
tar -zxvf cmake-3.10.3.tar.gz
cd cmake-3.10.3
./configure
make
sudo make install
编译:
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite/lite/tools/
bash build_linux.sh --arch=armv7hf