将 TFLite (TensorFlow) 转换为 MLModel (Apple)

Convert TFLite (TensorFlow) to MLModel (Apple)

我正在尝试将 TFLite 人脸网格模型转换为 MLModel (Apple)。

TFLite模型说明: https://drive.google.com/file/d/1VFC_wIpw4O7xBOiTgUldl79d9LA-LsnA/view

TFLite实际.tflite文件: https://github.com/google/mediapipe/blob/master/mediapipe/models/face_landmark.tflite

查看 Apple (https://coremltools.readme.io/docs/introductory-quickstart) 提供的 CoreMLTools 似乎是可行的,但所有示例代码都演示了来自 Keras 而不是来自 TFLite 的对话(尽管它得到明确支持) :

如何将 TFLite 模型转换为 MLModel 模型?

据我所知,没有从 TFLite 到 Core ML 的直接转换。有人可以创建这样的转换器,但显然没有人创建过。

两个选项:

  1. 自己动手吧。有一个 Python API 用于读取 TFLite 文件(平面缓冲区)和一个 API 用于写入 Core ML 文件(coremltools 中的 NeuralNetworkBuilder)。遍历 TFLite 模型的层 one-by-one,并将它们添加到 NeuralNetworkBuilder,然后另存为 .mlmodel 文件。

  2. 让 TFLite 为您做这件事。当您在 TFLite 中使用 CoreMLDelegate 时,它​​实际上执行模型转换 on-the-fly 并保存一个 .mlmodel 文件(或编译版本,.mlmodelc)。然后它使用 Core ML 来 运行 这个模型。您可以编写一些代码,使用 CoreMLDelegate 使用 TFLite 加载模型,然后获取它从应用程序包创建的 .mlmodel 文件并使用它。