-
Notifications
You must be signed in to change notification settings - Fork 74k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting Model to TFLite using Tensorflow 2.16.1 fails - works w/ 2.15.0 #63987
Comments
It seems like you might have a mix of |
@gsexton, |
@tilakrayal The model is built via: model = tf.keras.Sequential([
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dense(2048, activation='relu'),
tf.keras.layers.Dense(2048, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
loss=tf.keras.losses.binary_crossentropy,
optimizer=tf.keras.optimizers.Adam(learning_rate=0.003),
metrics=[
tf.keras.metrics.BinaryAccuracy(name='accuracy'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall')
]
)
history = model.fit(X_train, y_train, epochs=NUM_TRAINING_EPOCHS) the convert is: converter = tf.lite.TFLiteConverter.from_keras_model(best_model) so it seems like this should be OK. My first effort for conversion was: tf.saved_model.save(best_model,save_path)
converter = tf.lite.TFLiteConverter.from_saved_model(save_path)
tflite_model = converter.convert() That too gives:
@Aloqeely I'm not sure I follow what to do with this: Also, please try to execute the model code with the tf-keras, as the tensorflow v2.16 by default uses keras 3.0 version. Thank you! I think it is already doing what you suggest. Using tf.keras. |
Here is a complete simple program and data set that replicates the problem. You can alternate paths by |
Hi @gsexton, I have reproduced the code with TF 2.15 and TF 2.16. With 2.15 it's working fine as expected. As you mentioned, it's getting aborted with TF2.16.1 at TFLite conversion . Right now, TF2.16 has an issue with Keras 3.0. As a workaround install Keras 2 as follows. Thank You |
I have tested with the tf_keras installed, and the problem is resolved. |
I also experienced this bug, and it did not disappear with #!/usr/bin/env python3
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, DepthwiseConv2D, Conv2D, Flatten, Dense
# Define a simple model with depthwise convolutions
model = Sequential()
model.add(Input(shape=(64, 64, 3)))
model.add(DepthwiseConv2D(kernel_size=(3, 3), activation="relu"))
model.add(Conv2D(64, (1, 1), activation="relu"))
model.add(Flatten())
model.add(Dense(100, activation="relu"))
model.add(Dense(10, activation="softmax"))
# Compile the model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
# Define the representative dataset generator
def representative_dataset_gen():
for _ in range(100):
# Here you should provide a sample from your actual dataset
# For illustration, we'll use random data
# Yield a batch of input data (in this case, a single sample)
yield [tf.random.normal((1, 64, 64, 3))]
# Set the TensorFlow Lite converter
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Set the optimization to default for int8 quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# Define the representative dataset for quantization
converter.representative_dataset = representative_dataset_gen
# Restrict the target spec to int8 for full integer quantization
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# Instruct the converter to make the input and output layer as integer
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Convert the model
tflite_model = converter.convert()
# Save the model to a file
with open("model.tflite", "wb") as f:
f.write(tflite_model)
print("Model has been successfully converted to TFLite and saved as 'model.tflite'.") This crashes with the error:
|
Is there any chance we can fix this issue at the root cause? We found the same problem when saving a keras model with |
I have the same problem; In TF2.16 converting a .keras model to .tflite gives |
Same issue. |
Same issue here. Try: pip install -U tf_keras # Keras 2 but didn't work. Saving to tflite... |
Have you made sure to set the env variable before importing the tflow module? this fixed it for me |
Do you have an example how to do it? @filipang |
1. System information
2. Code
3. Failure after conversion
5. (optional) Any other info / logs
This issue was reported on #62610 originally.
I've tested this on Linux AMD 64, and on Mac OS 14.4 ARM and the same error occurs.
Per the comment from @adamantivm, I tried using Tensorflow 2.15.0 on Linux AMD64, and it worked as expected.
I tried setting the env variable for MLIR_CRASH... and running, but no output was found.
The text was updated successfully, but these errors were encountered: