本片文章是參考了Configuring the Arm NN SDK build environment for TensorFlow和GarryLau的Ubuntu 16.04環(huán)境下Configuring the Arm NN SDK build environment for Caffe的文章再根據(jù)自己實際過程中遇到問題進行了總結(jié)。在搭建環(huán)境之前最好先建一個目錄用于放編譯所需的環(huán)境,筆者的目錄是/home/armnn/
? SCons. Arm has tested SCons 2.4.1, other versions might work.
? CMake. Arm has tested CMake 3.5.1, other versions might work.
? Boost. Arm have tested version 1.64.
? Protobuf. Arm has tested version 3.5.0.
? ARM NN SDK
? Compute Library
? Caffe及相關(guān)
無論安裝是否安裝過SCons扁眯,都可以在root用戶下執(zhí)行以下命令
rm /usr/local/bin/scons*
rm -r /usr/local/lib/scons*
下載官方推薦版本,將安裝包放入/home/armnn扛邑,解壓進入/home/armnn/scons-2.4.1# 執(zhí)行
sudo python setup.py install
應(yīng)該會順利的安裝結(jié)束
卸載原有系統(tǒng)上安裝的cmake(如果不卸載系統(tǒng)自帶的,直接安裝铐然,可能會出現(xiàn)錯誤)蔬崩。
apt-get autoremove cmake
下載CMake-3.5.1-linux-x86_64.tar.gz,或者官網(wǎng)下載搀暑,然后直接sh cmake-3.11.1-Linux-x86_64.sh即可沥阳;如果是下載了*tar.gz的放到/home/armnn/下解壓然后執(zhí)行
ln -s /home/armnn/cmake-3.5.1-Linux-x86_64/bin/* /usr/bin/
官網(wǎng)下載1.64.0版本,放入/home/armnn/解壓之后的文件夾為boost_1_64_0自点,進入該文件夾運行
sh bootstrap.sh桐罕,
運行結(jié)果如下所示:
Building Boost.Build engine with toolset gcc… tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version… 2.7
Detecting Python root… /opt/fsl-imx-wayland/4.9.51-mx8-ga/sysroots/x86_64-pokysdk-linux/usr
Unicode/ICU support for Boost.Regex?.. not found.
Generating Boost.Build configuration in project-config.jam…
Bootstrapping is done. To build, run:
./b2
To adjust configuration, edit ‘project-config.jam’.
Further information:
-Command line help:
./b2 --help
- Getting started guide:
http://www.boost.org/more/getting_started/unix-variants.html
- Boost.Build documentation:
http://www.boost.org/build/doc/html/index.html
修改修改project-config.jam文件
if ! gcc in [ feature.values? ]
{
using gcc? :? arm :? aarch64-poky-linux-gcc --sysroot=$SDKTARGETSYSROOT
}//注意冒號后面的空格若為32位平臺則如下
if ! gcc in [ feature.values? ]
{
using gcc : arm : arm-linux-gnueabihf-gcc ;
}//注意編譯的時候要是能你的編譯器的環(huán)境變量
保存后執(zhí)行
./b2 link=static cxxflags=-fPIC --with-filesystem --with-test --with-log --with-program_options
如圖所示64位arm平臺:
git clone https://github.com/ARM-software/ComputeLibrary下載
對于ARMv7-A架構(gòu)的設(shè)備在/home/armnn/Compute Library/運行下面的指令進行編譯:
scons extra_cxx_flags="-fPIC" benchmark_tests=1 validation_tests=1 neon=1 opencl=1 embed_kernels=1
aarch-ARMV8的架構(gòu)執(zhí)行
scons arch=arm64-v8a extra_cxx_flags="-fPIC" benchmark_tests=0 validation_tests=0 opencl=1 embed_kernels=1 neon=1
如果提示以下錯誤:
ERROR: Compiler ’ aarch64-linux-gnu-aarch64-poky-linux-g++?
--sysroot=/opt/fsl-imx-wayland/4.9.51-mx8-ga/sysroots/aarch64-poky-linux’
not found
修改SContruct文件下ARMV8的prefix為“”空,
前提要先使能編譯器環(huán)境在這里使用的nxp的編譯器aarch64-poky-linux-g
elif env[‘a(chǎn)rch’] == ‘a(chǎn)rm64-v8a’:
env.Append(CXXFLAGS = [’-march=armv8-a’])
env.Append(CPPDEFINES = [‘ARM_COMPUTE_AARCH64_V8A’,‘NO_DOT_IN_TOOLCHAIN’])
if env[‘os’] == ‘linux’:
prefix = “aarch64-linux-gnu-”//改為? prefix = “”
大概一個小時可以編譯完吧,期間可以安裝后面的環(huán)境
注意:
在編譯最新版時桂敛,可能會出現(xiàn)(新版計算庫會提高推理速度)
arm-linux-gnueabihf-g++: internal compiler error: Killed (program cc1plus)原因是內(nèi)存不足功炮,需要增加臨時swap空間
step 1:
#sudo dd if=/dev/zero of=/home/swap bs=64M count=16
step 2:
# sudo mkswap /home/swap (可能會提示warning: don’t erase bootbits sectorson whole disk. Use -f to force,不用理會)
step 3:
#sudo swapon /home/swap
step 4:重新執(zhí)行編譯
完成編譯后釋放空間
關(guān)閉:
step 1:sudo swapoff /home/swap
step 2:sudo rm /home/swap
下載
git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
首先為了能夠使用x86的protoc編譯Caffe术唬,這里先不對protobuf進行任何配置直接執(zhí)行
./autogen.sh
一般會提示
configure.ac:104: error: possibly undefined macro: AC_PROG_LIBTOOL
錯誤
此時需要執(zhí)行
apt-get install libtool
執(zhí)行autogen.sh之后薪伏,再執(zhí)行./configure
依次執(zhí)行make、make check 粗仓、make install 時間在半小時至一小時不等嫁怀,可以先做其他事
之后我們?nèi)ゾ幾gCaffe和配置TensorFlow,等編譯完成后潦牛,我們執(zhí)行make clean后重新配置configure按照以下兩種平臺執(zhí)行
ARMv7-A架構(gòu):
./configure --host=arm-linux \
CC=arm-linux-gnueabihf-gcc \
CXX=arm-linux-gnueabihf-g++
–with-protoc=/home/armnn/protobuf/bin/protoc
ARMv8-A架構(gòu)使用自己你的目標(biāo)板編譯器:
./configure --host=aarch64-linux? CC=aarch64-linux-gnu-gcc
CXX=aarch64-linux-gnu-g++ --with-protoc=/home/armnn/protobuf/bin/protoc
git下載地址 git clonehttps://github.com/BVLC/caffe.git
安裝環(huán)境可以參考官方安裝
以下筆者根據(jù)官方的挑出14.04需要的
BLAS:apt-get install libatlas-base-devor install OpenBLAS by sudoapt-get install libopenblas-devor MKL for better CPU performance.
Python (optional): if you use the default Python you will need to sudoapt-get install the python-devpackage to have the Python headers for building the pycaffe interface.
apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev? libhdf5-serial-dev protobuf-compiler
apt-get install --no-install-recommends libboost-all-dev
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
安裝完成之后進入caffe目錄
mkdir build
cmake …
make all
make install
make runtest
make all時匯報很多錯誤眶掌,在錯誤的開頭會提示
error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11
or -std=gnu++11 compiler options.
此時需要在CMakeList.txt 的第57行添加
set(CMAKE_CXX_FLAGS “-fPIC -std=c++11”)
下載到/home/armnn目錄下
git clonehttps://github.com/tensorflow/tensorflow.git
進入到tensorflow執(zhí)行
/home/armnn/armnn/scripts/generate_tensorflow_protobuf.sh /home/armnn/protobuf/src/
正常情況什么都不提示挡育,如果提示
/home/armnn/protobuf/bin/protoc: 1: /home/armnn/protobuf/bin/protoc: Syntax error: Unterminated quoted string
是因為protobuf編譯沒使用默認(rèn)的x86編譯巴碗,重新編譯protobuf,之后make install
下載:
git clonehttps://github.com/ARM-software/armnn.git
進入armnn即寒,修改CMakeLists.txt橡淆,在首行添加
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror -Wno-unused-variable -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion -pthread”)
SET(BOOST_ROOT /home/armnn/boost_1_64_0)
SET(CMAKE_C_COMPILER? /home/tronlong/gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /home/tronlong/gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++)
創(chuàng)建build,進入build
cmake . . -DARMCOMPUTE_ROOT=/home/armnn/ComputeLibrary
-DARMCOMPUTE_BUILD_DIR=/home/armnn/ComputeLibrary/build?
-DBOOST_ROOT=/home/armnn/boost_1_64_0
-DTF_GENERATED_SOURCES=/home/armnn/protobuf/src
-DCAFFE_GENERATED_SOURCES=/home/armnn/caffe/build/include
-DBUILD_CAFFE_PARSER=1? -DARMCOMPUTENEON=1 -DBUILD_TF_PARSER=1
-DPROTOBUF_LIBRARY_RELEASE=/home/armnn/protobuf/src/.libs/libprotobuf.so
-DPROTOBUF_LIBRARY_DEBUG=/home/armnn/protobuf/src/.libs/libprotobuf.so
-DPROTOBUF_INCLUDE_DIRS=/usr/local
make
完成后如圖母赵,第一次編譯完成后忘記保存圖片
build下會出現(xiàn)armnn的caffe和tensorflow庫和單元測試程序UnitTests
下面我們將環(huán)境移植到開發(fā)板逸爵,筆者使用的是NXP的i.mx8mqevk的開發(fā)板,通過scp的命令將build下的程序考入目標(biāo)板
scp -r libarmnn* UnitTestsroot@192.168.0.101:/home/root/
scp -r /home/armnn/protobuf/src/.libs/libprotobuf.so.17root@192.168.0.101:/home/root/
將libarmnn前綴的4個庫都mv到根目錄/lib/
首先下載官方提供的例程:
git clonehttps://github.com/ARM-software/ML-examples.git
進入ML-examples/armnn-mnist凹嘲,編輯Makefile如下
ARMNN_LIB = /home/armnn/armnn/build
ARMNN_INC = /home/armnn/armnn/include
BOOST_ROOT = /home/armnn/boost_1_64_0
PROTOBUF = /home/armnn/protobuf/src/.libs
all: mnist_caffe mnist_tf
mnist_caffe: mnist_caffe.cpp mnist_loader.hpp
$(CXX) -O3 -std=c++14 -I$(ARMNN_INC) -I$(BOOST_ROOT) mnist_caffe.cpp -o mnist_caffe -L$(PROTOBUF) -L$(ARMNN_LIB) -lprotobuf? -larmnn -larmnnCaffeParser -lpthread
mnist_tf: mnist_tf.cpp mnist_loader.hpp
$(CXX) -O3 -std=c++14 -I$(ARMNN_INC) -I$(BOOST_ROOT) mnist_tf.cpp -o mnist_tf? -L$(PROTOBUF) -L$(ARMNN_LIB) -lprotobuf -larmnn -larmnnTfParser -lpthread
clean:
-rm -f mnist_tf mnist_caffe
test: mnist_caffe mnist_tf
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(ARMNN_LIB) ./mnist_caffe
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(ARMNN_LIB) ./mnist_tf
編譯的時候要注意你使用的交叉編譯器师倔,你的protobuf庫,v7架構(gòu)沒有在caffe的編譯出增加-lpthread周蹭,但V8不增加的話會提示錯誤
之后進行make趋艘,編譯完成后把文件夾下面的data和modle和生成的mnist_caffe\mnist_tf都scp到目標(biāo)板疲恢,分別執(zhí)行mnist-caffe和mnist_tf如下
到此,環(huán)境搭建并測試完成瓷胧,有問題歡迎大家留言討論