瞭解 C++ 程式庫

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.

如需一般使用方式的逐步操作說明,請參閱「開始使用微控制器」。

建構系統為特定檔案提供平台專屬的實作。這些這些檔案位於含有平台名稱的目錄中,例如 cortex-m

還提供其他幾種目錄,包括:

  • kernel,包含作業實作及其相關程式碼。
  • tools,包含建構工具及其輸出內容。
  • examples,其中包含程式碼範例。

建立新專案

建議您使用 Hello World 範例做為新專案的範本。您可以依本節說明操作,取得所選平台適用的版本。

使用 Arduino 程式庫

如果使用 Arduino,Arduino_TensorFlowLite Arduino 程式庫內含 Hello World 範例,您可以在 Arduino IDE 和 Arduino Create 中手動安裝這個程式庫。

新增程式庫後,請前往 File -> Examples。清單底部附近應該會顯示名為 TensorFlowLite:hello_world 的範例。選取這個 API 並按一下 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>

您可以在專案的 Makefiles 中找到測試名稱。例如,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.ccoutput_handler.cc 檔案的 SparkFun Edge 實作項目,並在指定目標 cortex_m_generic 時使用。

您可以在專案的 Makefiles 中找到專案名稱。例如,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