tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

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

Open JRF63 opened this issue 1 year ago • 2 comments

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 avatar Mar 30 '24 17:03 JRF63

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.

gaikwadrahul8 avatar Apr 01 '24 13:04 gaikwadrahul8