Skip to content
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

Cannot convert model to tflite #65529

Closed
mdomaradzki-cosmose opened this issue Apr 12, 2024 · 5 comments
Closed

Cannot convert model to tflite #65529

mdomaradzki-cosmose opened this issue Apr 12, 2024 · 5 comments
Assignees
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues TF 2.16 TFLiteConverter For issues related to TFLite converter

Comments

@mdomaradzki-cosmose
Copy link

1. System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 22.04
  • TensorFlow installation (pip package or built from source): pip
  • TensorFlow library (version, if pip package or github SHA, if built from source): 2.16.1

2. Code

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import tensorflow as tf

x, y = make_classification(random_state=42)
train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.2, stratify=y)
print(train_x.shape, test_x.shape, train_y.shape, test_y.shape)

model = tf.keras.models.Sequential([
    tf.keras.Input(shape=(20,)),
    tf.keras.layers.Dense(units=2, activation='softmax')
])

model.compile(optimizer=tf.keras.optimizers.SGD(), loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
model.fit(x=train_x, y=train_y, epochs=50)
model.evaluate(test_x, test_y)
model.save("model.h5")

converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

3. Failure after conversion

If the conversion is successful, but the generated model is wrong, then state what is wrong:

I tried to convert the model to tflite using two options. Using the above code and from terminal.
When I do this from code I get 'INFO:tensorflow:Assets written to: /tmp/tmph7onx9_d/assets' and then the code never ends (at least during two hours it didn't finish).
When I try to do this from terminal it crushes with following message:

Traceback (most recent call last):
  File "/home/xxx/miniconda3/envs/exp/bin/tflite_convert", line 8, in <module>
    sys.exit(main())
  File "/home/xxx/miniconda3/envs/exp/lib/python3.10/site-packages/tensorflow/lite/python/tflite_convert.py", line 692, in main
    app.run(main=run_main, argv=sys.argv[:1])
  File "/home/xxx/miniconda3/envs/exp/lib/python3.10/site-packages/absl/app.py", line 308, in run
    _run_main(main, args)
  File "/home/xxx/miniconda3/envs/exp/lib/python3.10/site-packages/absl/app.py", line 254, in _run_main
    sys.exit(main(argv))
  File "/home/xxx/miniconda3/envs/exp/lib/python3.10/site-packages/tensorflow/lite/python/tflite_convert.py", line 675, in run_main
    _convert_tf2_model(tflite_flags)
  File "/home/xxx/miniconda3/envs/exp/lib/python3.10/site-packages/tensorflow/lite/python/tflite_convert.py", line 281, in _convert_tf2_model
    model = keras_deps.get_load_model_function()(flags.keras_model_file)
TypeError: 'NoneType' object is not callable

4. (optional) RNN conversion support

If converting TF RNN to TFLite fused RNN ops, please prefix [RNN] in the title.

5. (optional) Any other info / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

@mdomaradzki-cosmose mdomaradzki-cosmose added the TFLiteConverter For issues related to TFLite converter label Apr 12, 2024
@Venkat6871 Venkat6871 added TF 2.16 comp:lite TF Lite related issues subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues labels Apr 15, 2024
@Venkat6871
Copy link

Hi @mdomaradzki-cosmose ,
Sorry for the delay, @LakshmiKalaKadali the issue Was able to reproduce the issue on Colab using TF v2.15 , 2.16 , and TF-nightly ,Please find the gist here for reference.

Thank you!

@mdomaradzki-cosmose
Copy link
Author

Thanks for your response

@LakshmiKalaKadali
Copy link
Contributor

Hi @mdomaradzki-cosmose,

The error is due to Keras 3. As a workaround install Keras 2 as follows.

    pip install -U tf_keras # Keras 2
    import os
    os.environ["TF_USE_LEGACY_KERAS"] = "1" 

Here is the gist with TF 2.16 and keras2 workaround. The TFLite model is successfully converted.

Thank You

@LakshmiKalaKadali LakshmiKalaKadali added the stat:awaiting response Status - Awaiting response from author label Apr 16, 2024
@mdomaradzki-cosmose
Copy link
Author

mdomaradzki-cosmose commented Apr 17, 2024

Thank you, It works!

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Status - Awaiting response from author label Apr 17, 2024
@LakshmiKalaKadali
Copy link
Contributor

Hi @mdomaradzki-cosmose,

Glad to know, the issue is resolved. Please feel free to close the issue.

Thank You

@LakshmiKalaKadali LakshmiKalaKadali added the stat:awaiting response Status - Awaiting response from author label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:lite TF Lite related issues stat:awaiting response Status - Awaiting response from author subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues TF 2.16 TFLiteConverter For issues related to TFLite converter
Projects
None yet
Development

No branches or pull requests

3 participants