Search code examples
pythontensorflowtensorflow2.0coremlcoremltools

Convert TFLite (TensorFlow) to MLModel (Apple)


I'm trying to convert TFLite Face Mesh model to MLModel (Apple).

TFLite model description: https://drive.google.com/file/d/1VFC_wIpw4O7xBOiTgUldl79d9LA-LsnA/view

TFLite actual .tflite file: https://github.com/google/mediapipe/blob/master/mediapipe/models/face_landmark.tflite

Looking at CoreMLTools provided by Apple (https://coremltools.readme.io/docs/introductory-quickstart) seems like it's possible, but all the samples codes demonstrate conversation from Keras and not from TFLite (although it's clearly supported):

enter image description here

How does one convert TFLite model to MLModel model?


Solution

  • As far as I know, there is no direct conversion from TFLite to Core ML. Someone could create such a converter but apparently no one has.

    Two options:

    1. Do it yourself. There is a Python API to read the TFLite file (flatbuffers) and an API to write Core ML files (NeuralNetworkBuilder in coremltools). Go through the layers of the TFLite model one-by-one, and add them to the NeuralNetworkBuilder, then save as a .mlmodel file.

    2. Let TFLite do this for you. When you use the CoreMLDelegate in TFLite, it actually performs the model conversion on-the-fly and saves a .mlmodel file (or the compiled version, .mlmodelc). Then it uses Core ML to run this model. You can write some code to load the model with TFLite using the CoreMLDelegate, then grab the .mlmodel file that this created from the app bundle and use that.