伴伴學 Accomdemy
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: 活動共筆 --- # 20210722 伴伴學:朱琪陪你在 AmebaD 上運行 TensorFlow Lite 模型 [toc] # TensorFlow 和 TensorFlow Lite 的對比 ### TensorFlow [TensorFlow](https://www.tensorflow.org/lite?hl=zh-tw) 是一個由 Google 開發的開源軟件庫,用於執行機器學習任務。 ### TensorFlow Lite [TensorFlow Lite](https://www.tensorflow.org/lite),是輕量化的 TensorFlow,方便將模型方便轉移到小型的Embedded平台 - TensorFlow Lite for Microcontrollers 屬於 TensorFlow Lite 中的一個功能,允許我們將輕量的模型移植到微控制器上運行“推理”, 只是使用模型進行預測、分類或決策,但是不包括訓練模型。 --- # Google Colaboratory ## 特點 可使用遠端 [![](https://i.imgur.com/KXWlYgK.png)compiler python](https://colab.research.google.com/notebooks/intro.ipynb?hl=zh) Colaboratory (簡稱為「Colab」) 可讓你在瀏覽器上撰寫及執行 Python 且具備下列優點: - 不必進行任何設定 - 免費使用 GPU - 輕鬆共用 如下圖所示:在 PC 上建立模型,再轉移到Embedded system平台 (Ex: Ameba) ![](https://i.imgur.com/BbODdpi.jpg) 當模型移植到Embedded system平台后,就可以使用模型來預測它沒有見到過的數據。比如,當一個模型可以判斷照片中是否有貓的存在時,可以將一張在訓練模型時沒有用到過的圖片展示給嵌入式系統中的模型裏,模型通過神經網絡的計算就可以辨別出這張新的圖片中是否有貓: ![](https://i.imgur.com/2v4Gjqr.png) >馬爸補充 Deep learngin for Data 放置 Embedded system 在前端, 抓取資料、學習,精簡後的資料再轉傳至雲。 # TFL Hello World 模型介紹 ![](https://i.imgur.com/aTyfcyI.png) * 輸入(x): * 任意0 - 2pi 之間的數字 * “我們期待的”輸出(y): * sin(x) ## 程式碼 [train_hello_world_model.ipynb](https://colab.research.google.com/github/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/hello_world/train/train_hello_world_model.ipynb) ![](https://i.imgur.com/SeZfsoY.png) ### 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). 樹突負責將資訊帶回細胞; 而軸突則是負責將訊息傳遞出去. * 神經細胞會利用化學和電訊號與其它細胞溝通 * 神經細胞有特化的組織(如 ,突觸), 以及化學物質(如 神經傳導物質). ![](https://i.imgur.com/ybeExmQ.png) ![](https://i.imgur.com/IATditu.png) >類神經網路 如果要只用一句話不是十分精確地說明什麼是深度學習,可以把深度學習形容成一種「比較深」的類神經網路,並搭配了各式各樣特別的類神經網路階層,如卷積神經網路、遞歸神經網路等等。所以一些深度學習架構也常被稱為深度神經網路(Deep neural network, DNN)。 類神經網路是一種模仿生物神經系統的數學模型。在類神經網路中,通常會有數個階層,每個階層中會有數十到數百個神經元(neuron),神經元會將上一層神經元的輸入加總後,進行活化函數(Activation function)的轉換,當成神經元的輸出。每個神經元會跟下一層的神經元有特殊的連接關係,使上一層神經元的輸出值經過權重計算(weight)後傳遞給下一層的神經元。 為了模擬生物的神經網路,活化函數通常是一種非線性的轉換。傳統的活化函數為Sigmoid函數或雙曲正切函數(hyperbolic tan, tanh),但是在深度神經網路中,Sigmoid函數的學習效果比較差,常會使用ReLU函數(Rectified linear unit)。 類神經網路的架構指的就是階層數量、每層中的神經元數量、各層之間神經元的連接方式、及活化函數的類型等設定。這些參數設定都是在使用類神經網路前需要由人力設定好的,參數設定的好壞也是大大影響到類神經網路的效能表現。類神經網路的學習和訓練過程就是試著找到最佳的權重設定。 [Reference](https://www.cc.ntu.edu.tw/chinese/epaper/0038/20160920_3805.html) 神經元增加,Training會越準確,趨近於 Training model。 但神經元增加,相對的越耗Embedded system resource & performance. # [Amebad TFL Hello World 範例](https://www.amebaiot.com/en/amebad-arduino-audio-tensorflow-basic/) ## 訓練模型 可自己訓練後修改這兩份檔案 ![](https://i.imgur.com/XiAsqHH.png) Github:Ambiot/ambd 下載library # [Ameba TFL 麥克風範例](https://www.amebaiot.com/en/amebad-arduino-audio-tensorflow-microspeech/) (僅適用於 RTL8722 series:include Codec) LED INDICATOR: * 白色(LEDW)-聆聽、處理中 * 紅色(LEDR)-No * 綠色(LEDG)-Yes * 黃色(LEDB)-can't detect/Heard unknown 類神經網路 搶8722 :::spoiler ```cpp= //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](https://developers.google.cn/machine-learning/crash-course/) Machine Learning Crash Course -> Start Crash Course ML最可愛理解就是training過程及出來的model,google作了可視化 分類器-分出是狗、貓、大象、大鵰.... 回歸- train出回歸fx,供計算 --- >好伯 分享 [Teachable Machine](https://teachablemachine.withgoogle.com/) --- 模型副檔名.tfl .c .h .c 跟 .h 要compile --- # 參考資料 1. [TinyML: Getting Started with TensorFlow Lite for Microcontrollers](https://www.digikey.com/en/maker/projects/tinyml-getting-started-with-tensorflow-lite-for-microcontrollers/c0cdd850f5004b098d263400aa294023) 2. [Intro to TinyML Part 1: Training a Model for Arduino in TensorFlow](https://www.digikey.com/en/maker/projects/intro-to-tinyml-part-1-training-a-model-for-arduino-in-tensorflow/8f1fc8c0b83d417ab521c48864d2a8ec) 3. [Python Colab Train Hello World](https://colab.research.google.com/github/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/hello_world/train/train_hello_world_model.ipynb#scrollTo=5PYwRFppd-WB) --- ### 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.| ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully