coremltools does not export LSTM state?

36 views Asked by At

I am trying to convert an LSTM based model from tensorflow to CoreML to be used in a MacOS application. Despite of looking at hundreds of examples in documentation, and spending two days through all possible references to a similar problem, I can't find a solution. Here is a compact example that replicates the problem.

First we define a very simple LSTM model and export it to CoreML using coremltools:

import os
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras
import coremltools 

model = keras.Sequential()
model.add(layers.Input(dtype="float32", batch_input_shape=(1,1,1)))
model.add(layers.LSTM(3,))
model.add(layers.Dense(1))
model.summary()


coreml_model_file = './test.mlmodel'

mlmodel = coremltools.convert(model,source="tensorflow")
mlmodel.save(coreml_model_file)

tensorflow version is 2.13.0, coremltools version is 6.3.0, python version 3.11. Tried with tensorflow 2.12.0, does not change a thing.

Dragging .mlmodel to Xcode performs perfect import. But I get the following as model specification: SCREENSHOT OF MY MODEL

Documentation (https://developer.apple.com/documentation/coreml/making_predictions_with_a_sequence_of_inputs?language=objc) suggests that the states of the LSTM layer should be exported as additional inputs and outputs, so the network can be applied to a new arbitrary sequence. Whatever I do I cannot get statein/stateout to appear in CoreML model.

What am I doing wrong? How do I export an LSTM model properly?

Tried different versions of tensorflow 2, coremltools. Stateful LSTM does not export at all. Changing input shape to (1, None, 1) does not help either.

0

There are 0 answers