1. 程式人生 > >Ubuntu 14.04 Caffe和TensorFlow的ARM NN SDK編譯環境搭建及MNIST程式測試

Ubuntu 14.04 Caffe和TensorFlow的ARM NN SDK編譯環境搭建及MNIST程式測試

Ubuntu 14.04下Caffe和TensorFlow的ARM NN SDK的aarch64編譯環境搭建及MNIST程式測試

本片文章是參考了Configuring the Arm NN SDK build environment for TensorFlow

和GarryLau的Ubuntu 16.04環境下Configuring the Arm NN SDK build environment for Caffe的文章再根據自己實際過程中遇到問題進行了總結。在搭建環境之前最好先建一個目錄用於放編譯所需的環境,筆者的目錄是/home/armnn/

ARM官方測試環境

• 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及相關

1. SCons安裝

無論安裝是否安裝過SCons,都可以在root使用者下執行以下命令

 > 1.rm /usr/local/bin/scons*
 >2.rm -r /usr/local/lib/scons*

下載官方推薦版本,將安裝包放入/home/armnn,解壓進入/home/armnn/scons-2.4.1# 執行

>sudo python setup.py install

應該會順利的安裝結束

2.安裝CMake

解除安裝原有系統上安裝的cmake(如果不解除安裝系統自帶的,直接安裝,可能會出現錯誤)。

 > apt-get autoremove cmake

下載CMake-3.5.1-linux-x86_64.tar.gz,或者官網下載,然後直接sh cmake-3.11.1-Linux-x86_64.sh即可;如果是下載了*tar.gz的放到/home/armnn/下解壓然後執行

ln -s /home/armnn/cmake-3.5.1-Linux-x86_64/bin/* /usr/bin/

3.下載安裝boost

官網下載1.64.0版本,放入/home/armnn/解壓之後的資料夾為boost_1_64_0,進入該資料夾執行

sh bootstrap.sh

執行結果如下所示:

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 ;
}//注意編譯的時候要是能你的編譯器的環境變數

儲存後執行

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

如圖所示64位arm平臺:
在這裡插入圖片描述

4.使用 SCons編譯Compute Library using

對於ARMv7-A架構的裝置在/home/armnn/Compute Library/執行下面的指令進行編譯:

scons extra_cxx_flags="-fPIC" benchmark_tests=1 validation_tests=1 neon=1

aarch-ARMV8的架構執行

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為“”空,
前提要先使能編譯器環境在這裡使用的nxp的編譯器aarch64-poky-linux-g

elif env[‘arch’] == ‘arm64-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 = “”

大概一個小時可以編譯完吧,期間可以安裝後面的環境

5.編譯protobuf

下載

git clone https://github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive

首先為了能夠使用x86的protoc編譯Caffe,這裡先不對protobuf進行任何配置直接執行

./autogen.sh

一般會提示

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

此時需要執行

apt-get install libtool

執行autogen.sh之後,再執行./configure
依次執行make、make check 、make install 時間在半小時至一小時不等,可以先做其他事
在這裡插入圖片描述之後我們去編譯Caffe和配置TensorFlow,等編譯完成後,我們執行make clean後重新配置configure按照以下兩種平臺執行
ARMv7-A架構:

./configure --host=arm-linux \
CC=arm-linux-gnueabihf-gcc \
CXX=arm-linux-gnueabihf-g++
–with-protoc=/home/armnn/protobuf/bin/protoc


ARMv8-A架構使用自己你的目標板編譯器:

./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 clone https://github.com/BVLC/caffe.git
安裝環境可以參考官方安裝
以下筆者根據官方的挑出14.04需要的

BLAS: apt-get install libatlas-base-dev or install OpenBLAS by sudo apt-get install libopenblas-dev or MKL for better CPU performance.
Python (optional): if you use the default Python you will need to sudo apt-get install the python-dev package 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”)

7下載TensorFlow

下載到/home/armnn目錄下

git clone https://github.com/tensorflow/tensorflow.git

進入到tensorflow執行

/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編譯沒使用預設的x86編譯,重新編譯protobuf,之後make install

8 編譯ARMNN-SDK

下載:

git clone https://github.com/ARM-software/armnn.git

進入armnn,修改CMakeLists.txt,在首行新增

set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion -pthread”)
SET(BOOST_ROOT /home/garylau/boost_1_64_0)

建立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下會出現armnn的caffe和tensorflow庫和單元測試程式UnitTests

在這裡插入圖片描述
下面我們將環境移植到開發板,筆者使用的是NXP的i.mx8mqevk的開發板,通過scp的命令將build下的程式考入目標板

scp -r libarmnn* UnitTests [email protected]:/home/root/

在這裡插入圖片描述libarmnn字首的4個庫都mv到根目錄/lib/

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

首先現在官方提供的例程:

git clone https://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架構沒有在caffe的編譯出增加 -lpthread,但V8不增加的話會提示錯誤
之後進行make,編譯完成後把資料夾下面的data和modle和生成的mnist_caffe\mnist_tf都scp到目標板,分別執行mnist-caffe和mnist_tf如下在這裡插入圖片描述到此,環境搭建並測試完成,有問題歡迎大家留言討論