apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.38k stars 631 forks source link

tf.keras.layers.LSTM in iOS 13? #1086

Open sdjenkins123 opened 3 years ago

sdjenkins123 commented 3 years ago

❓Question

I am attempting to convert a TensorFlow 2 model (tensorflow == 2.3.1) into a CoreML .mlmodel using coremltools (coremltools == 4.1). The goal is to have this model be usable in iOS 13.

To enforce this OS constraint, I used coremltools Unified Conversion API with a _minimum_deploymenttarget parameter set:

coremltools.convert(tensorflow_model, minimum_deployment_target=coremltools.target.iOS13)

Doing this, however, does not produce an .mlmodel and leads to the following error message: “ValueError: Provided deployment target requires model to be of version 4 but converted model has version 5 suitable for later releases

My understanding is that, to run on iOS 13, an .mlmodel cannot be of spec version 5. My understanding is also that the Unified Conversion API will produce an .mlmodel of the lowest spec version possible, given the operations in the TensorFlow model and parameter values (such as _minimum_deploymenttarget). Thus, in order to run my model on iOS 13, I believe that I’m looking for the result of the conversion to be an .mlmodel of spec version 4.

In an attempt to pinpoint which portion of my TensorFlow model was bumping the converted model’s spec version from 4 to 5, I started: building larger and larger versions of my intitial model in Tensorflow, converting them to CoreML, and inspecting the spec version of the resulting .mlmodel.

The below code produces an .mlmodel of spec version 4 (what I’m after):

import tensorflow as tf

layer1 = tf.keras.Input(shape=(82,))
layer2 = tf.keras.layers.Embedding(input_dim=100, output_dim=10, input_length=82)(layer1)
layer3 = tf.keras.layers.Dense(3, activation='softmax')(layer2)

model = tf.keras.Model(inputs=layer1, outputs=layer3)

ctmodel = ct.convert(model)
ctmodel.get_spec()

With the simple addition of an LSTM layer, however, the spec version of the resulting .mlmodel is bumped up to version 5:

import tensorflow as tf

layer1 = tf.keras.Input(shape=(82,))
layer2 = tf.keras.layers.Embedding(input_dim=100, output_dim=10, input_length=82)(layer1)
layer3 = tf.keras.layers.LSTM(units=200, return_sequences=False)(layer2)
layer4 = tf.keras.layers.Dense(3, activation='softmax')(layer3)

model = tf.keras.Model(inputs=layer1, outputs=layer4)

ctmodel = ct.convert(model)
ctmodel.get_spec()

Does this mean that a converted TensorFlow 2 model using LSTM layers cannot be run in iOS 13? Or, is there another way that I should be adding these LSTM layers into my model to enable iOS 13 capability? Thanks for the help!

System Information

tensorflow == 2.3.1 coremltools == 4.1

vsayyagari commented 3 years ago

I am in the exact same situation. I have an LSTM model in Keras. It converts fine to coreml model, but it requires me to use Xcode 12 (iOS 14). However, I need to run the model on iOS13

nestyme commented 2 years ago

hey! Any updates about that issue? Also same story :(