I’m trying to convert a TensorFlow model to Core ML format using coremltools, but I’m encountering an error during the conversion process. Here’s is the minimum reproducible example:
import numpy as np
import tensorflow as tf
import coremltools as ct
# Generate some dummy data
X_train = np.random.rand(100, 10) # 100 samples, 10 features
y_train = np.random.rand(100, 1) # 100 samples, 1 target
# Define a simple neural network model
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='linear')
])
# Compile the model
model.compile(optimizer='adam', loss='mse')
# Train the model
model.fit(X_train, y_train, epochs=10)
# Save the TensorFlow model
model.save('simple_nn_model.h5')
# Define the input shape and type for the Core ML model
input_shape = (10,) # Match the input shape of the model
input_name = 'input'
output_name = 'output'
# Convert the TensorFlow model to Core ML format
coreml_model = ct.convert(model,
convert_to="mlprogram",
source='tensorflow')
# Save the Core ML model
coreml_model.save('SimpleNNModel.mlmodel')
However, when I run the conversion code, I get the following error:
AttributeError: 'Sequential' object has no attribute '_get_save_spec'
I’ve ensured that I’m using the latest versions of TensorFlow and coremltools, but the error persists. What could be causing this issue, and how can I resolve it?
Details:
TensorFlow version: 2.17.0
coremltools version: 7.2
Python version: 3.12
Any help would be greatly appreciated!