在SparkFun Edge開發板上驗證TensorFlow Lite for Microcontrollers

TensorFlow Lite for Microcontrollers是TensorFlow新增項目,其目的就是在Microcontroller(MCU)上運作TensorFlow Lite;官方對應的是SparkFun Edge開發板,用以展示關鍵字語音辨識。本文主要是以這篇AI on a microcontroller with TensorFlow Lite and SparkFun Edge文章,說明軟體開發驗證的部份。附帶一題,相似的專案可以參考先前文章,由於TensorFlow Lite for Microcontrollers還在試驗階段,多數人實測後發現運作遲頓,可參考這篇討論

本次實作開發環境為macOS Mojave 10.14.5(with Xcode 10.2.1),並已安裝Anaconda for macOS Python 3.7 version。首先建立虛擬環境,並於完成後進入該虛擬環境:

conda create -n sparkfun-tensorflow python=3.6

conda activate sparkfun-tensorflow

參照 3.Set up your software : Download the TensorFlow repo 的段落,執行以下命令:

cd ~

curl -o tf.zip https://codeload.github.com/tensorflow/tensorflow/zip/aa47072ff4e2b7735b0e0ef9ef52f68ffbf7ef54

unzip tf.zip

cd tensorflow-aa47072ff4e2b7735b0e0ef9ef52f68ffbf7ef54/

參照 3.Set up your software : Download Python dependencies 的段落,執行以下命令:

pip install pycrypto pyserial

# macOS XCode的make程式太舊,在虛擬環境中安裝make程式

conda install -c anaconda make 

參照 4.Build and prepare the binary : Build the binary 的段落,執行以下命令:

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

tensorflow/lite/experimental/micro/tools/make/downloads/gcc_embedded/bin/arm-none-eabi-objcopy tensorflow/lite/experimental/micro/tools/make/gen/sparkfun_edge_cortex-m4/bin/micro_speech tensorflow/lite/experimental/micro/tools/make/gen/sparkfun_edge_cortex-m4/bin/micro_speech.bin -O binary

test -f tensorflow/lite/experimental/micro/tools/make/gen/sparkfun_edge_cortex-m4/bin/micro_speech.bin &&  echo "Binary was successfully created" || echo "Binary is missing"

參照 4.Build and prepare the binary : Prepare the binary 的段落,執行以下命令:

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

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

python tensorflow/lite/experimental/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 

接下來先更新CH341SER驅動程式,否則後面執行都會出錯,相關的討論參考這篇文章。硬體配置如下所示:

完成後參照 5. Get ready to flash the binary 的段落,執行以下命令:

ls -l /dev/cu*

export DEVICENAME=/dev/cu.wchusbserial14220

參照 6. Flash the binary 的段落,執行以下命令:

export BAUD_RATE=921600

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

python tensorflow/lite/experimental/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

# 按著標記為14按鈕直到看到"Sending Data Packet of length 8180"時放開

這樣就完成燒錄。就可進行關鍵字語音辨識之測試,詳細請參考 2. Set up your hardware 段落之內容。

發表迴響

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