使用Arm NN進行深度學習推論:使用TensorFlow模型

本文主要說明Arm NN在RK3399平台Ubuntu進行深度學習推論(inference),使用的是已訓練好的TensorFlow模型。因Arm NN需要在4GB RAM環境下編譯,本次使用的是 Firefly-RK3399 4GB/128GB 发烧版 開發板,並安裝Ubuntu 16.04作業系統( 映像檔為 FIREFLY-RK3399-UBUNTU16.04-GPT-20190403-1019.img.7z ):

Arm NN編譯請參照官方文件” Configuring the Arm NN SDK build environment for TensorFlow”之說明,以下整理各步驟的命令。在”Before you begin”步驟:

sudo apt-get update

sudo apt-get install git

sudo apt-get install scons

sudo apt-get install cmake

#####

sudo apt-get install autoconf automake libtool

sudo apt-get install libffi-dev

#####

mkdir armnn-tf && cd armnn-tf

export BASEDIR=`pwd`

在”Downloading the Arm NN and Compute Library files from GitHub”步驟:

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

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

在”Setting up the build dependencies”步驟:

wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2

tar xf boost_1_64_0.tar.bz2

cd $BASEDIR/boost_1_64_0

./bootstrap.sh

./b2 --build-dir=$BASEDIR/boost_1_64_0/build toolset=gcc link=static cxxflags=-fPIC --with-filesystem --with-test --with-log --with-program_options install --prefix=$BASEDIR/boost

###

cd $BASEDIR

git clone -b v3.5.0 https://github.com/google/protobuf.git

cd $BASEDIR/protobuf

git submodule update --init --recursive

./autogen.sh

./configure --prefix=$BASEDIR/protobuf-host

make

make install

###

cd $BASEDIR

git clone -b r1.13 https://github.com/tensorflow/tensorflow.git

在”Building the environment”步驟:

cd $BASEDIR/ComputeLibrary

scons arch=arm64-v8a opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" benchmark_tests=0 validation_tests=0

###

cd $BASEDIR

###

pushd tensorflow

../armnn/scripts/generate_tensorflow_protobuf.sh ../tensorflow-protobuf ../protobuf-host

popd

###

cd $BASEDIR/armnn

mkdir build

cd build

cmake .. -DARMCOMPUTECL=1  -DARMCOMPUTE_ROOT=$BASEDIR/ComputeLibrary -DARMCOMPUTE_BUILD_DIR=$BASEDIR/ComputeLibrary/build -DBOOST_ROOT=$BASEDIR/boost -DTF_GENERATED_SOURCES=$BASEDIR/tensorflow-protobuf  -DBUILD_TF_PARSER=1 -DPROTOBUF_ROOT=$BASEDIR/protobuf-host

make

在”Testing the build”步驟:

./UnitTests

Arm NN使用TensorFlow模型可參考官方說明,以下使用ARM提供的範例程式;從GitHub進行下載:

cd $BASEDIR

git clone https://github.com/ARM-software/ML-examples.git

cd ML-examples/armnn-mnist/

接著修改Makefile如下所示:

ARMNN_LIB = $(BASEDIR)/armnn/build
ARMNN_INC = $(BASEDIR)/armnn/include

all:  mnist_tf

mnist_tf: mnist_tf.cpp mnist_loader.hpp
  g++ -O3 -std=c++14 -I$(ARMNN_INC) mnist_tf.cpp -o mnist_tf -L$(ARMNN_LIB) -larmnn -larmnnTfParser -lpthread

clean:
  -rm -f mnist_tf

test:  mnist_tf
  LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(ARMNN_LIB) ./mnist_tf

修改完成後進行編譯:

make

編譯無誤後,用以下命令進行手寫數字(MNIST)推論:

make test

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *