1. 程式人生 > >在ARM板子上把玩Tensorflow Lite

在ARM板子上把玩Tensorflow Lite

前幾天Google的IO大會上釋出的ML Kit,ML Kit為端上部署深度學習模型提供了一套完整的解決方案,本地執行、雲端都支援。裡面本地部署用到的就是Tensorflow lite。

Tensorflow Lite是在Google去年IO大會上發表的,目前Tensorflow Lite也還在不斷的完善迭代中。

Tensorflow Lite在Android和iOS上部署官網有比較詳細的介紹以及對應的Demo。而對於ARM板子上的部署及測試,官網及網上的資料則相對較少。本文主要描述如何把Tensorflow Lite編譯到ARM板子上,並執行相應的Demo。

0.準備工作:在Ubuntu上準備ARM的交叉編譯環境

可以通過 apt-get install 直接在ubuntu上安裝交叉編譯環境

sudo apt-get install g++-arm-linux-gnueabihf
sudo apt-get install -y gcc-arm-linux-gnueabihf

1.下載Tensorflow原始碼

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

2.下載Tensorflow相關依賴包

先cd到Tensorflow工程的根目錄,然後執行下面的指令碼

./tensorflow/contrib/lite/download_dependencies.sh

3.編譯Tensorflow Lite

注意./tensorflow/contrib/lite/build_rpi_lib.sh中的目標編譯平臺是ARMV7,這裡最好不要改,即使你的目標平臺是ARMv8。改為ARMv8能也能編譯通過,但是貌似沒有把優化編譯進去,執行起來會非常慢,親測。

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR/../../.." #change CC_PREFIX if u need CC_PREFIX=arm-linux-gnueabihf- make -j 3 -f tensorflow/contrib/lite/Makefile TARGET=RPI TARGET_ARCH=armv7

在根目錄下執行該指令碼,如下:

./tensorflow/contrib/lite/build_rpi_lib.sh

編譯結束,會在tensorflow/contrib/lite/gen/lib/rpi_armv7目錄下產生libtensorflow-lite.a靜態庫。

4.編譯 label_image Demo

第三步的 build_rpi_lib.sh 指令碼實際是呼叫的 ./tensorflow/contrib/lite/MakefileTensorflow Lite 原始碼進行編譯,但是該 Makefile 並不能編譯 tensorflow/contrib/lite/examples/label_image 目錄下的Demo,所以需要修改 Makefilelabel_image 的原始碼配置到Makefile中,修改方式可以參考 Makefile 裡對 MINIMAL Demo 的配置。如果你不想自己改,下面是已經修改好的。

# Find where we're running from, so we can store generated files here.
ifeq ($(origin MAKEFILE_DIR), undefined)
    MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
endif

# Try to figure out the host system
HOST_OS :=
ifeq ($(OS),Windows_NT)
    HOST_OS = WINDOWS
else
    UNAME_S := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
            HOST_OS := LINUX
    endif
    ifeq ($(UNAME_S),Darwin)
        HOST_OS := OSX
    endif
endif

ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi)

# Where compiled objects are stored.
OBJDIR := $(MAKEFILE_DIR)/gen/obj/
BINDIR := $(MAKEFILE_DIR)/gen/bin/
LIBDIR := $(MAKEFILE_DIR)/gen/lib/
GENDIR := $(MAKEFILE_DIR)/gen/obj/

# Settings for the host compiler.
CXX := $(CC_PREFIX)gcc
CXXFLAGS := --std=c++11 -O3 -DNDEBUG
CC := $(CC_PREFIX)gcc
CFLAGS := -O3 -DNDEBUG
LDOPTS :=
LDOPTS += -L/usr/local/lib
ARFLAGS := -r

INCLUDES := \
-I. \
-I$(MAKEFILE_DIR)/../../../ \
-I$(MAKEFILE_DIR)/downloads/ \
-I$(MAKEFILE_DIR)/downloads/eigen \
-I$(MAKEFILE_DIR)/downloads/gemmlowp \
-I$(MAKEFILE_DIR)/downloads/neon_2_sse \
-I$(MAKEFILE_DIR)/downloads/farmhash/src \
-I$(MAKEFILE_DIR)/downloads/flatbuffers/include \
-I$(GENDIR)
# This is at the end so any globally-installed frameworks like protobuf don't
# override local versions in the source tree.
INCLUDES += -I/usr/local/include

LIBS := \
-lstdc++ \
-lpthread \
-lm \
-lz

# If we're on Linux, also link in the dl library.
ifeq ($(HOST_OS),LINUX)
    LIBS += -ldl
endif

include $(MAKEFILE_DIR)/ios_makefile.inc
include $(MAKEFILE_DIR)/rpi_makefile.inc

# This library is the main target for this makefile. It will contain a minimal
# runtime that can be linked in to other programs.
LIB_NAME := libtensorflow-lite.a
LIB_PATH := $(LIBDIR)$(LIB_NAME)

# A small example program that shows how to link against the library.
MINIMAL_PATH := $(BINDIR)minimal
LABEL_IMAGE_PATH :=$(BINDIR)label_image

MINIMAL_SRCS := \
tensorflow/contrib/lite/examples/minimal/minimal.cc
MINIMAL_OBJS := $(addprefix $(OBJDIR), \
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(MINIMAL_SRCS))))


LABEL_IMAGE_SRCS := \
tensorflow/contrib/lite/examples/label_image/label_image.cc \
tensorflow/contrib/lite/examples/label_image/bitmap_helpers.cc 
LABEL_IMAGE_OBJS := $(addprefix $(OBJDIR), \
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(LABEL_IMAGE_SRCS))))

# What sources we want to compile, must be kept in sync with the main Bazel
# build files.

CORE_CC_ALL_SRCS := \
$(wildcard tensorflow/contrib/lite/*.cc) \
$(wildcard tensorflow/contrib/lite/kernels/*.cc) \
$(wildcard tensorflow/contrib/lite/kernels/internal/*.cc) \
$(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.cc) \
$(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.cc) \
$(wildcard tensorflow/contrib/lite/*.c) \
$(wildcard tensorflow/contrib/lite/kernels/*.c) \
$(wildcard tensorflow/contrib/lite/kernels/internal/*.c) \
$(wildcard tensorflow/contrib/lite/kernels/internal/optimized/*.c) \
$(wildcard tensorflow/contrib/lite/kernels/internal/reference/*.c) \
$(wildcard tensorflow/contrib/lite/downloads/farmhash/src/farmhash.cc) \
$(wildcard tensorflow/contrib/lite/downloads/fft2d/fftsg.c)
# Remove any duplicates.
CORE_CC_ALL_SRCS := $(sort $(CORE_CC_ALL_SRCS))
CORE_CC_EXCLUDE_SRCS := \
$(wildcard tensorflow/contrib/lite/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*/*test.cc) \
$(wildcard tensorflow/contrib/lite/*/*/*/*test.cc) \
$(wildcard tensorflow/contrib/lite/kernels/test_util.cc) \
$(MINIMAL_SRCS) \
$(LABEL_IMAGE_SRCS)
# Filter out all the excluded files.
TF_LITE_CC_SRCS := $(filter-out $(CORE_CC_EXCLUDE_SRCS), $(CORE_CC_ALL_SRCS))
# File names of the intermediate files target compilation generates.
TF_LITE_CC_OBJS := $(addprefix $(OBJDIR), \
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(TF_LITE_CC_SRCS))))
LIB_OBJS := $(TF_LITE_CC_OBJS)

# For normal manually-created TensorFlow C++ source files.
$(OBJDIR)%.o: %.cc
    @mkdir -p $(dir [email protected])
    $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o [email protected]

# For normal manually-created TensorFlow C++ source files.
$(OBJDIR)%.o: %.c
    @mkdir -p $(dir [email protected])
    $(CC) $(CCFLAGS) $(INCLUDES) -c $< -o [email protected]

# The target that's compiled if there's no command-line arguments.
all: $(LIB_PATH)  $(MINIMAL_PATH) $(LABEL_IMAGE_PATH)

# Gathers together all the objects we've compiled into a single '.a' archive.
$(LIB_PATH): $(LIB_OBJS)
    @mkdir -p $(dir [email protected])
    $(AR) $(ARFLAGS) $(LIB_PATH) $(LIB_OBJS)

$(MINIMAL_PATH): $(MINIMAL_OBJS) $(LIB_PATH)
    @mkdir -p $(dir [email protected])
    $(CXX) $(CXXFLAGS) $(INCLUDES) \
    -o $(MINIMAL_PATH) $(MINIMAL_OBJS) \
    $(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)


$(LABEL_IMAGE_PATH): $(LABEL_IMAGE_OBJS) $(LIB_PATH)
    @mkdir -p $(dir [email protected])
    $(CXX) $(CXXFLAGS) $(INCLUDES) \
    -o $(LABEL_IMAGE_PATH) $(LABEL_IMAGE_OBJS) \
    $(LIBFLAGS) $(LIB_PATH) $(LDFLAGS) $(LIBS)


# Gets rid of all generated files.
clean:
    rm -rf $(MAKEFILE_DIR)/gen

# Gets rid of target files only, leaving the host alone. Also leaves the lib
# directory untouched deliberately, so we can persist multiple architectures
# across builds for iOS and Android.
cleantarget:
    rm -rf $(OBJDIR)
    rm -rf $(BINDIR)

$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d

-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(TF_CC_SRCS)))

修改完成後再次執行 ./tensorflow/contrib/lite/build_rpi_lib.sh ,此時在 ./tensorflow/contrib/lite/gen/bin/rpi_armv8 目錄下會產生編譯好的label_image二進位制檔案。

5.拷貝程式到板子上

準備測試圖片 tensorflow/contrib/lite/examples/label_image/testdata/grace_hopper.bmp ,當然用其他的圖片測試也行。此外,還需要從https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md 地址下載你想要測試的tf lite模型。還需要準備ImageNet的標籤檔案。最後需要傳到板子上的是如下幾個檔案。

grace_hopper.bmp
label_image
labels.txt
mobilenet_v1_1.0_224_quant.tflite/mobilenet_v1_1.0_224.tflite

6.在ARM板子上執行Tensorflow Lite

到此準備工作全部完成了,最後可以在板子上測試Tensorflow Lite的效能了,使用姿勢如下:

 ./label_image -v 1 -m ./mobilenet_v1_1.0_224.tflite  -i ./grace_hopper.bmp -l ./labels.txt

執行效果如下:

average time: 855.91 ms 
0.860174: 653 military uniform
0.0481021: 907 Windsor tie
0.00786706: 466 bulletproof vest
0.00644932: 514 cornet
0.00608028: 543 drumstick

接下來可以再測試下量化後的MobileNet效果:

./label_image -v 4 -m ./mobilenet_v1_1.0_224_quant.tflite  -i ./grace_hopper.bmp -l ./labels.txt

效果如下:

average time: 185.988 ms 
0.427451: 653 military uniform
0.305882: 907 Windsor tie
0.0431373: 668 mortarboard
0.0313726: 458 bow tie
0.0235294: 543 drumstick

上述Demo執行時預設執行緒數為10,若想測試單執行緒效果,只需再新增-t 1引數即可。單執行緒在板子上的效能效果如下,從結果看TF Lite的多執行緒反而沒起到加速的效果(2 3 4執行緒數量我都試過,這個一個跟TF Lite的執行緒優化有關,二是可能跟ARM板子上的大小核相關,單執行緒情況下預設使用的是大核)

./label_image -v 1 -m ./mobilenet_v1_1.0_224.tflite  -i ./grace_hopper.bmp -l ./labels.txt -t 1 

average time: 297.647 ms 
0.860174: 653 military uniform
0.0481024: 907 Windsor tie
0.00786706: 466 bulletproof vest
0.00644934: 514 cornet
0.00608031: 543 drumstick
./label_image -v 4 -m ./mobilenet_v1_1.0_224_quant.tflite  -i ./grace_hopper.bmp -l ./labels.txt -t 1  

average time: 145.173 ms 
0.427451: 653 military uniform
0.305882: 907 Windsor tie
0.0431373: 668 mortarboard
0.0313726: 458 bow tie
0.0235294: 543 drumstick

從效能上看,如果不考慮識別的準確率的話,TF Lite量化後的效能收益還是很不錯的。在同等平臺下,NCNN跑Mobilenet的耗時比TF Lite的非量化版本略快。

相關推薦

ARM板子把玩Tensorflow Lite

前幾天Google的IO大會上釋出的ML Kit,ML Kit為端上部署深度學習模型提供了一套完整的解決方案,本地執行、雲端都支援。裡面本地部署用到的就是Tensorflow lite。 Tensorflow Lite是在Google去年IO大會上發表的,目前

ARM(firefly3399)板子把玩Tensorflow Lite

基本的步驟與連結的這位老兄一致。 https://blog.csdn.net/computerme/article/details/80345065 唯一要改變的就是要指定靜態編譯 -static,

Arm板子移植putty

For building on Unix: - unix/configure is for Unix and GTK. If you don't have GTK, you   should still be able to build the command-line utilities (PSCP,   

使用TensorFlow Lite在Android手機實現影象分類

*本篇文章已授權微信公眾號 guolin_blog (郭霖)獨家釋出 前言 TensorFlow Lite是一款專門針對移動裝置的深度學習框架,移動裝置深度學習框架是部署在手機或者樹莓派等小型移動裝置上的深度學習框架,可以使用訓練好的模型在手機等裝置上完成推理

minigui demo helloworld在arm目標板子的執行

把mg-samples-3.0.12編譯完後,在src目錄下有個helloworld,把它copy到板子執行測試我們的環境是否正確。 因為我的板子沒有usr目錄,所以我自己建立一個,使用nfs把build裡面的檔案掛載到板子上執行。 板子上: 掛載/目

windows10 下安裝tensorflow 並且在jupyter notebook 使用tensorflow

回車 電腦 con 支持 連接 span python cond tps 1.安裝jupyter notebook並配置環境 首先建議大家安裝anaconda,最新版本請到官網下載(點擊下載連接),沒錯,直接點擊下載python3.6版本的(當然選擇做自己電腦相應的

使用VS2012編寫arm-linux的應用程序

蘿蔔 方便 分享 不支持 開發 eat nbsp ips eclips 習慣了window上的集成開發環境,對於Linux上的C/C++開發覺得相當蛋疼。 可能大佬們會批,Linux有vim如何強大的工具,可惜真用不來,蘿蔔青菜吧。 剛接觸Linux時,覺得應用程序的調試,

在Mac安裝tensorflow和virtualenv

port perl source install 變量 python3 ont master path 1.安裝brew,在終端中輸入: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ins

在 Ubuntu 安裝 TensorFlow (官方文檔的翻譯)

tree com docker x86 cup 早期 guid director doc 本指南介紹了如何在 Ubuntu 上安裝 TensorFlow。這些指令也可能對其他 Linux 變體起作用, 但是我們只在Ubuntu 14.04 或更高版本上測試了(我們只支持

Ubuntu安裝TensorFlow(python2.7版)

close red shadow googl 軟件包 -m pandas -- com 筆記內容:Ubuntu上安裝TensorFlow(python2.7版)筆記日期:2018-01-31 Ubuntu上安裝TensorFlow(python2.7版) 我的系統環境:

Windows安裝 TensorFlow

you html pat 網址 body imp 查看 遇到 可能 1、官網及幫助文檔 官網: https://www.tensorflow.org/install/install_windows 中文幫助文檔:https://efeiefei.gitbooks.io/

windows 面的tensorflow-GPU、cuda、cudnn 安裝

atm search 容易 oca api RM run correct 既然 安裝說明 平臺:目前可在Ubuntu、Mac OS、Windows上安裝 版本:提供gpu版本、cpu版本 安裝方式:pip方式、Anaconda方式 Tips: 在Windows上目前支持p

ubuntu 和windows 分別在anaconda安裝tensorflow

3.5 bsp amd rom python版本 sim tun 鏡像站 whl windows下 的anaconda安裝tensorflow: 在Anaconda Prompt中:conda install tensorflow python=3.5一直下載失敗.總結一下

64位Arm Lubuntu編譯Qt MySQLl驅動

                             64位Arm處理器 Lubuntu上編譯Qt MySQL驅動 1.安裝MySQ

ARM平臺藍芽協議棧Bluez的移植使用和配置(寫的狠不錯) .

目錄(?)[-] 相關說明 網站資源 工作環境 編譯 核心 Bluez Lib / Utils 藍芽硬體初始化及基礎服務啟動 何謂硬體初始化 硬體初始化步驟

TensorFlow Lite for Android 初探(附demo)

一. TensorFlow Lite TensorFlow Lite 是用於移動裝置和嵌入式裝置的輕量級解決方案。TensorFlow Lite 支援 Android、iOS 甚至樹莓派等多種平臺。 我們知道大多數的 AI 是在雲端運算的,但

Tensorflow lite up up~

簡介 在桌面PC或是伺服器上使用TensorFlow訓練出來的模型檔案,不能直接用在TFLite上執行,需要使用離線工具先轉成.tflite檔案。筆者發現官方文件中很多細節介紹的都不太明確,在使用過程中需要不斷嘗試。我把自己的嘗試過的步驟分享出來,希望能幫助大家節省時間。 具體說來,tfli

win10安裝tensorflow時報錯 ImportError: DLL load failed: 找不到指定的模組

win10上安裝tensorflow時報錯 ImportError: DLL load failed: 找不到指定的模組 https://visualstudio.microsoft.com/zh-hans/downloads/?rr=https%3A%2F%2Fsocial.msdn.mi

TensorFlow資料彙總 在 Mac OS X 安裝 TensorFlow

1、使用 virtualenv 安裝TensorFlow 按照以下步驟安裝 TensorFlow: 開啟終端(一個 shell),你將在這個終端中執行隨後的步驟 通過以下命令安裝 pip 和 virtualenv: $ sudo easy_install pip $ sudo pip in

在iOS平臺使用TensorFlow教程(下)

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!