apple / apple/coremltools

tf.keras.layers.LSTM in iOS 13?

Open
#1,086 2 comments 0 reactions 0 assignees View on GitHub
LSTM/RNN question
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## ❓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_deployment_target_ 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_deployment_target_). 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

Contributor guide

Open the contributing guide

Research direction

Run the supplied TensorFlow 2.3.1 and coremltools 4.1 reproducer, comparing conversion with and without the tf.keras.layers.LSTM layer and checking the resulting model specification versions. Trace the LSTM conversion path and iOS 13 deployment-target validation; done means confirming supported behavior or documenting the required compatibility change.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, python, tensorflow
Domain
machine-learning, mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.