TensorFlow Lite for Microcontrollers C++ 程式庫是 TensorFlow 儲存庫的一部分。其設計宗旨是易於閱讀、易於修改、經過完善測試、易於整合,並與一般 TensorFlow Lite 相容。
以下文件概述 C++ 程式庫的基本結構,並提供建立您專案的相關資訊。
檔案結構
micro
根目錄具有相對簡單的結構。然而,由於它位於龐大的 TensorFlow 儲存庫中,我們建立了指令碼和預先產生的專案檔案,以便在各種嵌入式開發環境中隔離提供相關的原始碼檔案。
主要檔案
使用 TensorFlow Lite for Microcontrollers 直譯器的最重要檔案位於專案的根目錄中,並附帶測試。
[`micro_mutable_op_resolver.h`](https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/micro_mutable_op_resolver.h)
can be used to provide the operations used by the interpreter to run the
model.
micro_error_reporter.h
輸出偵錯資訊。micro_interpreter.h
包含處理和執行模型的程式碼。
請參閱微控制器入門,以逐步瞭解一般用法。
建構系統為特定檔案提供平台專屬的實作。這些檔案位於以平台名稱命名的目錄中,例如 cortex-m
。
還有其他幾個目錄,包括
開始新專案
我們建議使用 *Hello World* 範例作為新專案的範本。您可以按照本節中的說明,取得適用於您所選平台的版本。
使用 Arduino 程式庫
如果您使用 Arduino,*Hello World* 範例會包含在 Arduino_TensorFlowLite
Arduino 程式庫中,您可以手動將其安裝在 Arduino IDE 和 Arduino Create 中。
新增程式庫後,前往 File -> Examples
。您應該會在清單底部附近看到一個名為 TensorFlowLite:hello_world
的範例。選取該範例,然後按一下 hello_world
以載入範例。接著,您可以儲存範例副本,並將其用作您自己專案的基礎。
為其他平台產生專案
TensorFlow Lite for Microcontrollers 能夠產生獨立專案,其中包含所有必要的原始碼檔案,並使用 Makefile
。目前支援的環境包括 Keil、Make 和 Mbed。
若要使用 Make 產生這些專案,請複製 TensorFlow/tflite-micro 儲存庫並執行下列指令:
make -f tensorflow/lite/micro/tools/make/Makefile generate_projects
這會花費幾分鐘,因為它必須下載一些大型工具鏈以用於依附元件。完成後,您應該會看到在類似 gen/linux_x86_64/prj/
的路徑內建立了一些資料夾 (確切路徑取決於您的主機作業系統)。這些資料夾包含產生的專案和原始碼檔案。
執行指令後,您將能夠在 gen/linux_x86_64/prj/hello_world
中找到 *Hello World* 專案。例如, hello_world/keil
會包含 Keil 專案。
執行測試
若要建構程式庫並執行其所有單元測試,請使用下列指令:
make -f tensorflow/lite/micro/tools/make/Makefile test
若要執行個別測試,請使用下列指令,並將 <test_name>
替換為測試名稱:
make -f tensorflow/lite/micro/tools/make/Makefile test_<test_name>
您可以在專案的 Makefile 中找到測試名稱。例如, examples/hello_world/Makefile.inc
會指定 *Hello World* 範例的測試名稱。
建構二進位檔
若要為給定專案 (例如範例應用程式) 建構可執行二進位檔,請使用下列指令,並將 <project_name>
替換為您想要建構的專案:
make -f tensorflow/lite/micro/tools/make/Makefile <project_name>_bin
例如,下列指令將為 *Hello World* 應用程式建構二進位檔:
make -f tensorflow/lite/micro/tools/make/Makefile hello_world_bin
預設情況下,專案會針對主機作業系統進行編譯。若要指定不同的目標架構,請使用 TARGET=
和 TARGET_ARCH=
。下列範例說明如何為一般 cortex-m0 建構 *Hello World* 範例:
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_generic TARGET_ARCH=cortex-m0 hello_world_bin
指定目標時,任何可用的目標專屬原始碼檔案都會取代原始程式碼使用。例如,子目錄 examples/hello_world/cortex_m_generic
包含檔案 constants.cc
和 output_handler.cc
的 SparkFun Edge 實作,當指定目標 cortex_m_generic
時將會使用這些實作。
您可以在專案的 Makefile 中找到專案名稱。例如, examples/hello_world/Makefile.inc
會指定 *Hello World* 範例的二進位檔名稱。
最佳化核心
tensorflow/lite/micro/kernels
根目錄中的參考核心是以純 C/C++ 實作,且不包含平台專屬的硬體最佳化。
最佳化版本的核心會在子目錄中提供。例如, kernels/cmsis-nn
包含數個最佳化核心,可運用 Arm 的 CMSIS-NN 程式庫。
若要使用最佳化核心產生專案,請使用下列指令,並將 <subdirectory_name>
替換為包含最佳化的子目錄名稱:
make -f tensorflow/lite/micro/tools/make/Makefile TAGS=<subdirectory_name> generate_projects
您可以為新的最佳化建立子資料夾,以新增自己的最佳化。我們鼓勵針對新的最佳化實作提交提取要求。
產生 Arduino 程式庫
如果您需要產生程式庫的新版本,可以從 TensorFlow 儲存庫執行下列指令碼:
./tensorflow/lite/micro/tools/ci_build/test_arduino.sh
產生的程式庫可在 gen/arduino_x86_64/prj/tensorflow_lite.zip
中找到。
移植到新裝置
將 TensorFlow Lite for Microcontrollers 移植到新平台和裝置的指南,可在 micro/docs/new_platform_support.md
中找到。