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

Tensorflow.js Converter: Usage of save_format when converting to graph model causes error on Keras 3 #8229

Open
JRF63 opened this issue Mar 30, 2024 · 1 comment

Comments

@JRF63
Copy link

JRF63 commented Mar 30, 2024

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): WSL2
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: No
  • TensorFlow.js installed from (npm or script link): Not applicable
  • TensorFlow.js version (use command below): Not applicable
  • Browser version: Not applicable
  • Tensorflow.js Converter Version: 4.17

Describe the current behavior

tensorflow 2.16.1 requires keras>=3.0.0, but I think passing the save_format argument is a hard error in keras>=3.0.0 if the extension is not .h5/.hdf5/.keras (current code is using a .savedmodel).

Describe the expected behavior

Avoid using save_format.

Standalone code to reproduce the issue

from tensorflow_model_optimization.python.core.keras.compat import keras

model = keras.Sequential(
    [
        keras.layers.InputLayer(input_shape=(28, 28)),
        keras.layers.Reshape(target_shape=(28, 28, 1)),
        keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation="relu"),
        keras.layers.MaxPooling2D(pool_size=(2, 2)),
        keras.layers.Flatten(),
        keras.layers.Dense(10),
    ]
)

model.compile(
    optimizer="adam",
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=["accuracy"],
)

model.save("test.keras")

then

tensorflowjs_converter --input_format=keras --output_format=tfjs_graph_model --quantize_uint8=* test.keras webmodel

Other info / logs

Traceback (most recent call last):
  File "/home/user/tensorflow-spreads/venv/bin/tensorflowjs_converter", line 8, in <module>
    sys.exit(pip_main())
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 958, in pip_main
    main([' '.join(sys.argv[1:])])
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 962, in main
    convert(argv[0].split(' '))
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 948, in convert
    _dispatch_converter(input_format, output_format, args, quantization_dtype_map,
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 634, in _dispatch_converter
    dispatch_keras_h5_to_tfjs_graph_model_conversion(
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/tensorflowjs/converters/converter.py", line 227, in dispatch_keras_h5_to_tfjs_graph_model_conversion
    model.save(temp_savedmodel_dir, include_optimizer=False, save_format='tf')
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/user/tensorflow-spreads/venv/lib/python3.10/site-packages/keras/src/saving/saving_api.py", line 66, in save_model
    raise ValueError(
ValueError: The `save_format` argument is deprecated in Keras 3. Please remove this argument and pass a file path with either `.keras` or `.h5` extension.Received: save_format=tf

Setting the env var export TF_USE_LEGACY_KERAS=1 avoids this problem.

@JRF63 JRF63 added the type:bug Something isn't working label Mar 30, 2024
@gaikwadrahul8 gaikwadrahul8 self-assigned this Apr 1, 2024
@gaikwadrahul8
Copy link
Contributor

Hi, @JRF63

I apologize for the delayed response and as far I know the TensorFlow.js converter currently does not support converting Keras models saved in the new format .keras . it's still relatively new and hasn't been fully integrated with the TensorFlow.js converter yet. TensorFlow.js developer team is actively developing the tfjs-converter and support for the new .keras format might be added in future releases.

Thank you for bringing this issue to our attention! I've been able to reproduce the problem you described in the issue template. At the moment while we wait for the tfjs-converter to support the new .keras format, you can use a temporary workaround by simply saving your model in the .h5 format for TensorFlow.js conversion. I've tested this approach and it seems like working as expected.

For your reference I've created a gist containing your code snippet that demonstrates saving the model in .h5 format and it's working as expected, please refer this gist-file

Thank you for your understanding and patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants