Convert TFLite (TensorFlow) to MLModel (Apple)

Question:

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?

Asked By: Roi Mulia

||

Answers:

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.

Answered By: Matthijs Hollemans

As of 2023, I think there is an old way to convert tflite to coreml model. First you’ll need to convert it into onnx model. Detailed instruction here: https://github.com/zhenhuaw-me/tflite2onnx. This tool might contain insufficient deep learning operations. If you encouter errors, you might want to try microsoft tool tf2onnx with the flag "–tflite". After that, you can convert it into coreml. Sadly Apple deprecated support for onnx model in coremltool version 6, you’ll need to use version 5 instead. Good luck

Answered By: Duc Tap