20210722 伴伴學:朱琪陪你在 AmebaD 上運行 TensorFlow Lite 模型

TensorFlow 和 TensorFlow Lite 的對比

TensorFlow

TensorFlow 是一個由 Google 開發的開源軟件庫,用於執行機器學習任務。

TensorFlow Lite

TensorFlow Lite,是輕量化的 TensorFlow,方便將模型方便轉移到小型的Embedded平台

  • TensorFlow Lite for Microcontrollers
    屬於 TensorFlow Lite 中的一個功能,允許我們將輕量的模型移植到微控制器上運行“推理”, 只是使用模型進行預測、分類或決策,但是不包括訓練模型。

Google Colaboratory

特點

可使用遠端 compiler python
Colaboratory (簡稱為「Colab」) 可讓你在瀏覽器上撰寫及執行 Python
且具備下列優點:

  • 不必進行任何設定
  • 免費使用 GPU
  • 輕鬆共用

如下圖所示:在 PC 上建立模型,再轉移到Embedded system平台 (Ex: Ameba)

當模型移植到Embedded system平台后,就可以使用模型來預測它沒有見到過的數據。比如,當一個模型可以判斷照片中是否有貓的存在時,可以將一張在訓練模型時沒有用到過的圖片展示給嵌入式系統中的模型裏,模型通過神經網絡的計算就可以辨別出這張新的圖片中是否有貓:

馬爸補充
Deep learngin for Data
放置 Embedded system 在前端,
抓取資料、學習,精簡後的資料再轉傳至雲。

TFL Hello World 模型介紹

  • 輸入(x):
    • 任意0 - 2pi 之間的數字
  • “我們期待的”輸出(y):
    • sin(x)

程式碼

train_hello_world_model.ipynb

Python 環境設定

  • Configure Defaults
  • Setup Environment
  • Import Dependencies

數據的準備

首先,隨機生成所需要的數據集(data set), 然後對這些數據集進行分類:

  • Split the Data 數據分類
    1. Training 訓練集 60%
    2. Validation 驗證集 20%
    3. Testing 測試集 20%

訓練模型

采用神經網絡的形式來訓練,因爲神經網路(Neuron Network)是類比人類神經元連接的一種預測方法,所以這裏先引入神經細胞的概念

神經細胞

神經細胞也有和其它細胞不同的地方:

  • 神經細胞會從本體處長出觸手狀的組織, 稱為軸突(axons)和樹突(dendrites). 樹突負責將資訊帶回細胞; 而軸突則是負責將訊息傳遞出去.
  • 神經細胞會利用化學和電訊號與其它細胞溝通
  • 神經細胞有特化的組織(如 ,突觸), 以及化學物質(如 神經傳導物質).

類神經網路
如果要只用一句話不是十分精確地說明什麼是深度學習,可以把深度學習形容成一種「比較深」的類神經網路,並搭配了各式各樣特別的類神經網路階層,如卷積神經網路、遞歸神經網路等等。所以一些深度學習架構也常被稱為深度神經網路(Deep neural network, DNN)。
類神經網路是一種模仿生物神經系統的數學模型。在類神經網路中,通常會有數個階層,每個階層中會有數十到數百個神經元(neuron),神經元會將上一層神經元的輸入加總後,進行活化函數(Activation function)的轉換,當成神經元的輸出。每個神經元會跟下一層的神經元有特殊的連接關係,使上一層神經元的輸出值經過權重計算(weight)後傳遞給下一層的神經元。
為了模擬生物的神經網路,活化函數通常是一種非線性的轉換。傳統的活化函數為Sigmoid函數或雙曲正切函數(hyperbolic tan, tanh),但是在深度神經網路中,Sigmoid函數的學習效果比較差,常會使用ReLU函數(Rectified linear unit)。
類神經網路的架構指的就是階層數量、每層中的神經元數量、各層之間神經元的連接方式、及活化函數的類型等設定。這些參數設定都是在使用類神經網路前需要由人力設定好的,參數設定的好壞也是大大影響到類神經網路的效能表現。類神經網路的學習和訓練過程就是試著找到最佳的權重設定。 Reference

神經元增加,Training會越準確,趨近於 Training model。
但神經元增加,相對的越耗Embedded system resource & performance.

Amebad TFL Hello World 範例

訓練模型

可自己訓練後修改這兩份檔案

Github:Ambiot/ambd
下載library

Ameba TFL 麥克風範例

(僅適用於 RTL8722 series:include Codec)

LED INDICATOR:

  • 白色(LEDW)-聆聽、處理中
  • 紅色(LEDR)-No
  • 綠色(LEDG)-Yes
  • 黃色(LEDB)-can't detect/Heard unknown

類神經網路 搶8722

//ameba_command_responder.cpp /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 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. ==============================================================================*/ #if defined(ARDUINO) && !defined(RTL8722DM) #define ARDUINO_EXCLUDE_CODE #endif // defined(ARDUINO) && !defined(RTL8722DM) #ifndef ARDUINO_EXCLUDE_CODE #include "command_responder.h" #include "Arduino.h" // 支援兩種板的腳位 #if defined(BOARD_RTL8722DM_MINI) #define LEDR 3 #define LEDG 4 #define LEDB LED_B //小板上的燈 #define LEDW LED_G #else #define LEDR 8 #define LEDG 9 #define LEDB 10 #define LEDW 11 #endif // Toggles the built-in LED every inference, and lights a colored LED depending // on which word was detected. void RespondToCommand(tflite::ErrorReporter* error_reporter, int32_t current_time, const char* found_command, uint8_t score, bool is_new_command) { static bool is_initialized = false; if (!is_initialized) { pinMode(LEDW, OUTPUT); pinMode(LEDR, OUTPUT); pinMode(LEDG, OUTPUT); pinMode(LEDB, OUTPUT); // Ensure the LED is off by default. digitalWrite(LEDR, LOW); digitalWrite(LEDG, LOW); digitalWrite(LEDB, LOW); is_initialized = true; } static int32_t last_command_time = 0; static int count = 0; static int certainty = 220; if (is_new_command) { TF_LITE_REPORT_ERROR(error_reporter, "Heard %s (%d) @%dms", found_command, score, current_time); // If we hear a command, light up the appropriate LED if (found_command[0] == 'y') { last_command_time = current_time; digitalWrite(LEDG, HIGH); // Green for yes } if (found_command[0] == 'n') { last_command_time = current_time; digitalWrite(LEDR, HIGH); // Red for no } if (found_command[0] == 'u') { last_command_time = current_time; digitalWrite(LEDB, HIGH); // Blue for unknown } } // If last_command_time is non-zero but was >3 seconds ago, zero it // and switch off the LED. if (last_command_time != 0) { if (last_command_time < (current_time - 3000)) { last_command_time = 0; digitalWrite(LEDW, LOW); digitalWrite(LEDR, LOW); digitalWrite(LEDG, LOW); digitalWrite(LEDB, LOW); } // If it is non-zero but <3 seconds ago, do nothing. return; } // Otherwise, toggle the LED every time an inference is performed. ++count; if (count & 1) { digitalWrite(LEDW, HIGH); } else { digitalWrite(LEDW, LOW); } } #endif // ARDUINO_EXCLUDE_CODE

tensorflow lite 模型不能反推


教學平台:google machine learning crash course
Machine Learning Crash Course - Google

Machine Learning Crash Course -> Start Crash Course
ML最可愛理解就是training過程及出來的model,google作了可視化

分類器-分出是狗、貓、大象、大鵰
回歸- train出回歸fx,供計算


好伯 分享 Teachable Machine


模型副檔名.tfl .c .h
.c 跟 .h 要compile


參考資料

  1. TinyML: Getting Started with TensorFlow Lite for Microcontrollers
  2. Intro to TinyML Part 1: Training a Model for Arduino in TensorFlow
  3. Python Colab Train Hello World

Native compiler vs Cross compiler

native compiler是都一樣
ex: 在 x86 PC上 用 VC compiler code 在 x86 PC 上跑.
cross compiler是都不一樣
ex: 在 x86 PC上 用 Keil C compiler code 在 AVR/ARM 上跑.

  1. Native Compiler :
    Native compiler are compilers that generates code for the same Platform on which it runs. It converts high language into computer’s native language. For example Turbo C or GCC compiler

  2. Cross compiler :
    A Cross compiler is a compiler that generates executable code for a platform other than one on which the compiler is running. For example a compiler that running on Linux/x86 box is building a program which will run on a separate Arduino/ARM.

Native Compiler Cross Compiler
Translates program for same hardware/platform/machine on it is running. Translates program for different hardware/platform/machine other than the platform which it is running.
It is used to build programs for same system/machine & OS it is installed. It is used to build programs for other system/machine like AVR/ARM.
It is dependent on System/machine and OS It is independent of System/machine and OS
It can generate executable file like .exe It can generate raw code .hex
TurboC or GCC is native Compiler. Keil is a cross compiler.

Select a repo