Path: blob/master/site/zh-cn/lite/guide/authoring.ipynb
25118 views
Copyright 2021 The TensorFlow Authors.
TFLite 创作工具
TensorFlow Lite Authoring API 提供了一种方式来维护与 TensorFlow Lite 兼容的 tf.function
模型。
安装
TensorFlow 到 TensorFlow Lite 的兼容性问题
如果您想在设备端使用您的 TF 模型,您需要将其转换为 TFLite 模型,以便从 TFLite 解释器使用它。在转换过程中,您可能会遇到兼容性错误,因为 TFLite 内置运算集不支持 TensorFlow 运算。
这是一个令人讨厌的问题。如何能够更早地检测到该问题呢,比如在模型创作时间?
请注意,以下代码将在调用 converter.convert()
时失败。
简单的目标感知创作用法
我们引入了 Authoring API 来检测模型创作期间的 TensorFlow Lite 兼容性问题。
您只需添加 @tf.lite.experimental.authoring.compatible
修饰器来封装您的 tf.function
模型,以检查 TFLite 兼容性。
之后,当您评估模型时,将自动检查兼容性。
如果发现任何 TensorFlow Lite 兼容性问题,它将显示 COMPATIBILITY WARNING
或 COMPATIBILITY ERROR
以及有问题的运算的确切位置。在本例中,它显示了 tf.unction 模型中 tf.Cosh
运算的位置。
您还可以使用 <function_name>.get_compatibility_log()
方法检查兼容性日志。
引发不兼容性异常
您可以为 @tf.lite.experimental.authoring.compatible
修饰器提供一个选项。当您尝试评估经过修饰的模型时,raise_exception
选项会引发异常。
指定 "Select TF ops" 用法
如果您已了解 Select Tf op 用法,可以通过设置 converter_target_spec
将其用于 Authoring API。它与您将在 tf.lite.TFLiteConverter API 中使用的对象相同。
检查 GPU 兼容性
如果您希望确保您的模型与 TensorFlow Lite 的 GPU 委托兼容,您可以设置 tf.lite.TargetSpec 的 experimental_supported_backends
。
以下示例展示如何确保您的模型的 GPU 委托兼容性。请注意,此模型存在兼容性问题,因为它使用带有 tf.slice 算子和不受支持的 tf.cosh 算子的二维张量。您将看到两个带有位置信息的 COMPATIBILITY WARNING
。