TensorFlow Lite for Microcontrollers的Hello World example編譯與執行

延續前面的文章,接下來都是以” Moved TensorFlow Lite Micro out of experimental folder”的原始碼架構來說明如何編譯TensorFlow Lite for MicrocontrollersHello World example。本次說明並不包含Arduino的部份,因其是使用官方的Arduino_TensorFlowLite程式庫,會用另外一篇文章進行說明。本文都是在Ubuntu 16.04.6 LTS (Xenial Xerus) 64-bit PC (AMD64) desktop系統上進行,並使用Anaconda Distributionx的虛擬環境來管理不同Microcontroller之編譯環境(除了ESP-IDF必需安裝到Ubuntu原生環境外)。首先,先安裝SparkFun Serial Basic Breakout所需使用的USB Serial Port驅動程式,修改/etc/modprobe.d/blacklist.conf加入這一行:

blacklist ch341

完成後重新開機。接著參照這篇文章的方式安裝CH341驅動程式:

git clone https://github.com/juliagoda/CH341SER.git

cd CH341SER/

make

sudo make load

cd ..

接下來到Anaconda網站下載Anaconda 2019.10 for Linux Installer Python 3.7 version,並進行安裝;其中Do you wish the installer to initialize Anaconda3 by running conda init? [yes|no]選no,安裝完成後建立一個Anaconda3.sh,內容如下:

export LANG='UTC-8'
export LC_ALL='en_US.UTF-8'

eval "$(/home/yilintung/anaconda3/bin/conda shell.bash hook)"

用以下命令將Anaconda3.sh變成可執行的Shell Script :

chmod 0744 Anaconda3.sh

用以下命令進入Anaconda環境:

source Anaconda3.sh

接下來,先進行SparkFun Edge的部份,首先建立Anaconda虛擬環境並安裝必要套件:

conda create -n sparkfun-tensorflow python=3.6

conda activate sparkfun-tensorflow

conda install -c anaconda make 

conda install -c anaconda git

conda install -c anaconda curl

pip install pycrypto pyserial

用以下命令複製一份TensorFlow原始碼並進入其目錄:

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

cd tensorflow/

用以下命令進行編譯與執行檔簽署:

make -f tensorflow/lite/micro/tools/make/Makefile TARGET=sparkfun_edge hello_world_bin

cp tensorflow/lite/micro/tools/make/downloads/AmbiqSuite-Rel2.0.0/tools/apollo3_scripts/keys_info0.py \
tensorflow/lite/micro/tools/make/downloads/AmbiqSuite-Rel2.0.0/tools/apollo3_scripts/keys_info.py

python3 tensorflow/lite/micro/tools/make/downloads/AmbiqSuite-Rel2.0.0/tools/apollo3_scripts/create_cust_image_blob.py \
--bin tensorflow/lite/micro/tools/make/gen/sparkfun_edge_cortex-m4/bin/hello_world.bin \
--load-address 0xC000 \
--magic-num 0xCB \
-o main_nonsecure_ota \
--version 0x0

python3 tensorflow/lite/micro/tools/make/downloads/AmbiqSuite-Rel2.0.0/tools/apollo3_scripts/create_cust_wireupdate_blob.py \
--load-address 0x20000 \
--bin main_nonsecure_ota.bin \
-i 6 \
-o main_nonsecure_wire \
--options 0x1

接上SparkFun Edge開發板到主機上,用以下程序燒錄執行檔到板子上:

export DEVICENAME=/dev/ttyUSB0

export BAUD_RATE=921600

# 確保您的SparkFun Edge已連接到SparkFun USB-C Serial Basic,並透過USB連接到您的電腦 
# 仍然按著標記為14按鈕,按一下標記RST按鈕以重置電路板 
# 持續按著標記為14按鈕,電腦執行以下命令

python3 tensorflow/lite/micro/tools/make/downloads/AmbiqSuite-Rel2.0.0/tools/apollo3_scripts/uart_wired_update.py \
-b ${BAUD_RATE} ${DEVICENAME} \
-r 1 \
-f main_nonsecure_wire.bin \
-i 6

就可看到執行結果,確認後將SparkFun Edge開發板從主機移出。接下來進行STM32F746的部份,延續前面的執行環境,用以下命令產生出Mbed專案檔:

make -f tensorflow/lite/micro/tools/make/Makefile TARGET=mbed TAGS="CMSIS disco_f746ng" generate_hello_world_mbed_project

conda deactivate

接著建立Mbed開發環境,使用以下命令進行:

conda create -n mbed python=2.7

conda activate mbed

conda install git

conda install -c anaconda mercurial

# Src. : https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads

tar jxvf ~/Downloads/gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2 -C ~/Projects

pip install mbed-cli

mbed --version

mbed config -G GCC_ARM_PATH /home/yilintung/Projects/gcc-arm-none-eabi-7-2018-q2-update/bin

mbed config --list

用以下命令設定Mbed專案,並進行編譯:

cd tensorflow/lite/micro/tools/make/gen/mbed_cortex-m4/prj/hello_world/mbed

mbed config root .

mbed deploy

python -c 'import fileinput, glob;
for filename in glob.glob("mbed-os/tools/profiles/*.json"):
  for line in fileinput.input(filename, inplace=True):
    print line.replace("\"-std=gnu++98\"","\"-std=c++11\", \"-fpermissive\"")'

cd -

cp tensorflow/lite/micro/tools/make/downloads/cmsis/CMSIS/DSP/Include/arm_math.h tensorflow/lite/micro/tools/make/gen/mbed_cortex-m4/prj/hello_world/mbed/mbed-os/cmsis/TARGET_CORTEX_M/

cd -

mbed compile -m DISCO_F746NG -t GCC_ARM

完成後接上DISCO-F746NG開發板到主機上,並用以下命令將執行檔發佈到開發板,接著離開Mbed編譯環境:

mbed compile -m DISCO_F746NG -t GCC_ARM

cp ./BUILD/DISCO_F746NG/GCC_ARM/mbed.bin /media/yilintung/DIS_F746NG/

cd -

conda deactivate

就可看到執行結果,確認後將DISCO-F746NG開發板從主機移出。接下來進行ESP32的部份,在此是使用的是ESP-EYE開發板;延續前面的執行環境,用以下命令產出ESP32專案並在其產生後離開Anaconda環境:

conda activate sparkfun-tensorflow

pip install six

make -f tensorflow/lite/micro/tools/make/Makefile TARGET=esp generate_hello_world_esp_project

exit

接著開新的終端機,使用以下命令安裝ESP-IDF:

sudo apt-get install git wget flex bison gperf python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools cmake ninja-build ccache

mkdir ~/esp

cd ~/esp

git clone --recursive https://github.com/espressif/esp-idf.git

cd ~/esp/esp-idf

./install.sh

. $HOME/esp/esp-idf/export.sh

完成後切換到TensorFlow原始碼目錄,再用以下命令切換到ESP32專案所在的目錄:

cd tensorflow/lite/micro/tools/make/gen/esp_xtensa-esp32/prj/hello_world/esp-idf

用以下命令進行編譯:

idf.py build

完成後,將ESP-EYE開發板接上,用以下命令將執行檔載入至開發板與確認執行結果:

idf.py --port /dev/ttyUSB0 flash monitor

上述程序的筆記文字檔在此

發表迴響

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