Skip to main content

将模型转换为 Google Coral 的 Edge TPU TFlite 格式

note

本文档由 AI 翻译。如您发现内容有误或有改进建议,欢迎通过页面下方的评论区,或在以下 Issue 页面中告诉我们:https://github.com/Seeed-Studio/wiki-documents/issues

简介

Coral M.2 Accelerator 是一款带有双 Edge TPU 的 M.2 模块,可通过 M.2 E-key 插槽为现有系统和产品提供 Edge TPU 协处理器。TensorflowPytorch 是最流行的深度学习框架。因此,为了使用 Edge TPU,我们需要将模型编译为 Edge TPU 格式。

本文档将指导您完成模型编译和在 Google Coral TPU 上运行的过程,使您能够利用其高性能机器学习应用的能力。

准备硬件

Raspberry Pi 5 8GBRaspberry Pi M.2 HAT+Coral M.2 Accelerator B+M key

安装硬件

pir

转换模型

note

在开始之前,请确保您已按照安装指南将 Google Coral TPU 安装到 Pi 5。

note

所有过程均已在 Python 3.11.9 上测试。

安装 Tensorflow

pip install tensorflow

检查 tflite_converter

tflite_convert -h

结果应如下所示:

2024-07-23 10:41:03.750087: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-07-23 10:41:04.276520: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
usage: tflite_convert [-h] --output_file OUTPUT_FILE [--saved_model_dir SAVED_MODEL_DIR | --keras_model_file KERAS_MODEL_FILE] [--saved_model_tag_set SAVED_MODEL_TAG_SET]
[--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY] [--enable_v1_converter] [--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]]
[--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]]

Command line tool to run TensorFlow Lite Converter.

optional arguments:
-h, --help show this help message and exit
--output_file OUTPUT_FILE
Full filepath of the output file.
--saved_model_dir SAVED_MODEL_DIR
Full path of the directory containing the SavedModel.
--keras_model_file KERAS_MODEL_FILE
Full filepath of HDF5 file containing tf.Keras model.
--saved_model_tag_set SAVED_MODEL_TAG_SET
Comma-separated set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags must be present. In order to pass in an empty tag set, pass in "". (default "serve")
--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY
Key identifying the SignatureDef containing inputs and outputs. (default DEFAULT_SERVING_SIGNATURE_DEF_KEY)
--enable_v1_converter
Enables the TensorFlow V1 converter in 2.0
--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]
Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)
--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]
Experimental flag, subject to change. Enables MLIR-based quantizer instead of flatbuffer conversion. (default True)

将 Tensorflow 模型转换为 TFlite 模型

tflite_convert --saved_model_dir=YOUR_MODEL_PATH --output_file=YOUR_MODEL_NAME.tflite

将 TFlite 模型转换为 Edge TPU 模型

note

在将 tflite 模型转换为 edge tpu 模型之前,您应该优化您的模型,请查看优化 Tensorflow 模型

安装 edgetpu 编译器

curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

sudo apt-get update

sudo apt-get install edgetpu-compiler

将 TFlite 模型转换为 Edge TPU 模型

edgetpu_compiler YOUR_MODEL_NAME.tflite

然后您应该会得到一个名为 YOUR_MODEL_NAME_edgetpu.tflite 的新文件。

技术支持与产品讨论

感谢您选择我们的产品!我们致力于为您提供多种支持,确保您使用我们的产品时体验顺畅。我们提供多种沟通渠道,以满足不同的偏好和需求。

Loading Comments...