Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/zh-cn/lite/models/convert/convert_models.md
25118 views

转换 TensorFlow 模型

本页介绍如何使用 TensorFlow Lite 转换器将 TensorFlow 模型转换为 TensorFlow Lite 模型(由文件扩展名 .tflite 标识的经过优化的 FlatBuffer 格式)。

注:本指南假设您已安装 TensorFlow 2.x,并在 TensorFlow 2.x 中训练了模型。如果您的模型在 TensorFlow 1.x 中进行了训练,请考虑迁移到 TensorFlow 2.x。要识别已安装的 TensorFlow 版本,请运行 print(tf.__version__)

转换工作流

下图说明了转换模型的高级工作流:

TFLite 转换器工作流

图 1. 转换器工作流。

可以使用下面的其中一个选项来转换模型:

  1. Python API推荐):这允许您将转换集成到开发流水线中,应用优化,添加元数据,以及许多其他简化转换过程的任务。

  2. 命令行:这仅支持基础模型转换。

注:如果您在模型转换过程中遇到任何问题,请创建 GitHub 议题

Python API

辅助代码:要了解有关 TensorFlow Lite Converter API 的更多信息,请运行 print(help(tf.lite.TFLiteConverter))

使用 tf.lite.TFLiteConverter 转换 TensorFlow 模型。TensorFlow 模型使用 SavedModel 格式存储,并使用高级 tf.keras.* API(Keras 模型)或低级 tf.* API(从中生成具体函数)。因此,您有以下三个选项(以下几个部分提供了示例):

  • tf.lite.TFLiteConverter.from_saved_model()推荐):转换 SavedModel

  • tf.lite.TFLiteConverter.from_keras_model():转换 Keras 模型。

  • tf.lite.TFLiteConverter.from_concrete_functions():转换具体函数

转换 SavedModel(推荐)

以下示例展示了如何将 SavedModel 转换为 TensorFlow Lite 模型。

import tensorflow as tf # Convert the model converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory tflite_model = converter.convert() # Save the model. with open('model.tflite', 'wb') as f: f.write(tflite_model)

转换 Keras 模型

以下示例展示了如何将 Keras 模型转换为 TensorFlow Lite 模型。

import tensorflow as tf # Create a model using high-level tf.keras.* APIs model = tf.keras.models.Sequential([ tf.keras.layers.Dense(units=1, input_shape=[1]), tf.keras.layers.Dense(units=16, activation='relu'), tf.keras.layers.Dense(units=1) ]) model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model model.fit(x=[-1, 0, 1], y=[-3, -1, 1], epochs=5) # train the model # (to generate a SavedModel) tf.saved_model.save(model, "saved_model_keras_dir") # Convert the model. converter = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert() # Save the model. with open('model.tflite', 'wb') as f: f.write(tflite_model)

转换具体函数

以下示例展示了如何将个具体函数转换为 TensorFlow Lite 模型。

import tensorflow as tf # Create a model using low-level tf.* APIs class Squared(tf.Module): @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)]) def __call__(self, x): return tf.square(x) model = Squared() # (ro run your model) result = Squared(5.0) # This prints "25.0" # (to generate a SavedModel) tf.saved_model.save(model, "saved_model_tf_dir") concrete_func = model.__call__.get_concrete_function() # Convert the model. converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func], model) tflite_model = converter.convert() # Save the model. with open('model.tflite', 'wb') as f: f.write(tflite_model)

其他功能

  • 应用优化。常用的优化是训练后量化,它可以在最小准确率损失的情况下进一步减少模型延迟和大小。

  • 添加元数据,在设备端部署模型时,可以更轻松地创建特定于平台的封装器代码。

转换错误

以下是常见的转换错误及其解决方案:

命令行工具

:如果可能,强烈建议您使用上面列出的 Python API

如果您已从 pip 安装了 TensorFlow 2.x,请使用 tflite_convert 命令。要查看所有可用标志,请使用以下命令:

$ tflite_convert --help `--output_file`. Type: string. Full path of the output file. `--saved_model_dir`. Type: string. Full path to the SavedModel directory. `--keras_model_file`. Type: string. Full path to the Keras H5 model file. `--enable_v1_converter`. Type: bool. (default False) Enables the converter and flags used in TF 1.x instead of TF 2.x. You are required to provide the `--output_file` flag and either the `--saved_model_dir` or `--keras_model_file` flag.

如果您已下载 TensorFlow 2.x 源文件,并且希望在不构建和安装软件包的情况下从该源文件运行转换器,您可以在命令中将 'tflite_convert' 替换为 'bazel run tensorflow/lite/python:tflite_convert --'。

转换 SavedModel

tflite_convert \ --saved_model_dir=/tmp/mobilenet_saved_model \ --output_file=/tmp/mobilenet.tflite

转换 Keras H5 模型

tflite_convert \ --keras_model_file=/tmp/mobilenet_keras_model.h5 \ --output_file=/tmp/mobilenet.tflite

后续步骤

使用 TensorFlow Lite 解释器在客户端设备(例如移动设备、嵌入式设备)上运行推断。