Ubuntu 14.04 Caffe和TensorFlow的ARM NN SDK編譯環(huán)境搭建及MNIST程序測試

ARM官方測試環(huán)境

1. SCons安裝

2.安裝CMake

3.下載安裝boost

4.使用 SCons編譯Compute Library

5.編譯protobuf

6編譯Caffe

7下載TensorFlow

8 編譯ARMNN-SDK

9 MNIST(手寫體圖片識別測試程序)的測試

本片文章是參考了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ù)自己實(shí)際過程中遇到問題進(jìn)行了總結(jié)。在搭建環(huán)境之前最好先建一個(gè)目錄用于放編譯所需的環(huán)境,筆者的目錄是/home/armnn/

ARM官方測試環(huán)境

? 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)

1. SCons安裝

無論安裝是否安裝過SCons,都可以在root用戶下執(zhí)行以下命令

rm /usr/local/bin/scons*

rm -r /usr/local/lib/scons*

下載官方推薦版本,將安裝包放入/home/armnn,解壓進(jìn)入/home/armnn/scons-2.4.1# 執(zhí)行

sudo python setup.py install

應(yīng)該會順利的安裝結(jié)束

2.安裝CMake

卸載原有系統(tǒng)上安裝的cmake(如果不卸載系統(tǒng)自帶的,直接安裝,可能會出現(xiàn)錯(cuò)誤)。

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/

3.下載安裝boost

官網(wǎng)下載1.64.0版本,放入/home/armnn/解壓之后的文件夾為boost_1_64_0,進(jìn)入該文件夾運(yùn)行

sh bootstrap.sh,

運(yùn)行結(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 ;

}//注意編譯的時(shí)候要是能你的編譯器的環(huán)境變量

保存后執(zhí)行

./b2 link=static cxxflags=-fPIC --with-filesystem --with-test --with-log --with-program_options

如圖所示64位arm平臺:

4.使用 SCons編譯Compute Library

git clone https://github.com/ARM-software/ComputeLibrary下載

對于ARMv7-A架構(gòu)的設(shè)備在/home/armnn/Compute Library/運(yùn)行下面的指令進(jìn)行編譯:

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

如果提示以下錯(cuò)誤:

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 = “”

大概一個(gè)小時(shí)可以編譯完吧,期間可以安裝后面的環(huán)境

注意:

在編譯最新版時(shí),可能會出現(xiàn)(新版計(jì)算庫會提高推理速度)

arm-linux-gnueabihf-g++: internal compiler error: Killed (program cc1plus)原因是內(nèi)存不足,需要增加臨時(shí)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

5.編譯protobuf

下載

git clone https://github.com/protocolbuffers/protobuf.git

cd protobuf

git submodule update --init --recursive

首先為了能夠使用x86的protoc編譯Caffe,這里先不對protobuf進(jìn)行任何配置直接執(zhí)行

./autogen.sh

一般會提示

configure.ac:104: error: possibly undefined macro: AC_PROG_LIBTOOL

錯(cuò)誤

此時(shí)需要執(zhí)行

apt-get install libtool

執(zhí)行autogen.sh之后,再執(zhí)行./configure

依次執(zhí)行make、make check 、make install 時(shí)間在半小時(shí)至一小時(shí)不等,可以先做其他事

之后我們?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

6編譯Caffe

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

安裝完成之后進(jìn)入caffe目錄

mkdir build

cmake …

make all

make install

make runtest

make all時(shí)匯報(bào)很多錯(cuò)誤,在錯(cuò)誤的開頭會提示

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.

此時(shí)需要在CMakeList.txt 的第57行添加

set(CMAKE_CXX_FLAGS “-fPIC -std=c++11”)

7下載TensorFlow

下載到/home/armnn目錄下

git clonehttps://github.com/tensorflow/tensorflow.git

進(jìn)入到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

是因?yàn)閜rotobuf編譯沒使用默認(rèn)的x86編譯,重新編譯protobuf,之后make install

8 編譯ARMNN-SDK

下載:

git clonehttps://github.com/ARM-software/armnn.git

進(jìn)入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,進(jìn)入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個(gè)庫都mv到根目錄/lib/

9 MNIST(手寫體圖片識別測試程序)的測試

首先下載官方提供的例程:

git clonehttps://github.com/ARM-software/ML-examples.git

進(jìn)入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

編譯的時(shí)候要注意你使用的交叉編譯器,你的protobuf庫,v7架構(gòu)沒有在caffe的編譯出增加-lpthread,但V8不增加的話會提示錯(cuò)誤

之后進(jìn)行make,編譯完成后把文件夾下面的data和modle和生成的mnist_caffe\mnist_tf都scp到目標(biāo)板,分別執(zhí)行mnist-caffe和mnist_tf如下

到此,環(huán)境搭建并測試完成,有問題歡迎大家留言討論

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容