Skip to content

bug: model saving and loading does not work with Keras 3.x model format #4921

@rmarquis

Description

@rmarquis

Describe the bug

Since Keras 3 (released Novembre 28, 2023), Keras model saving and loading API changed and now requires a filepath for the model ending iwith .keras as opposed to a folder. The .keras format is simply a compressed folder.

Error

As such, the bentoml.keras.save_model and bentoml.keras.load_model API are not working as expected anymore, since they try to save and load from a folder path.

ValueError: Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=/tmp/tmp394vyky1bentoml_model_my_keras_model/.

As far as I can tell, BentoML's Keras framework layer API have been broken since Keras 3 release.

Fix

Starting with Keras 3.4.0 (released June 25, 2024), Keras allows to pass a new zipped boolean parameter which allows the older behaviour (see keras-team/keras@e0eaac4).

This can be used to restore the compatibility with BentoML when saving and loading Keras models.

To reproduce

Save a keras model with BentoML in its model store, using a recent version of Keras (3.x) or Tensorflow.

    # Save the model using BentoML to its model store
    # https://docs.bentoml.com/en/latest/reference/frameworks/keras.html#bentoml.keras.save_model
    bentoml.keras.save_model(
        "my_keras_model",
        model,
        include_optimizer=True,
        custom_objects={
            "preprocess": preprocess,
            "postprocess": postprocess,
        },
    )

Traceback:

Traceback (most recent call last):
  File "/home/remy/my_project/src/train.py", line 134, in <module>
    main()
  File "/home/remy/my_project/src/train.py", line 111, in main
    bentoml.keras.save_model(
  File "/home/remy/my_project/.venv/lib/python3.11/site-packages/bentoml/_internal/frameworks/keras.py", line 272, in save_model
    model.save(bento_model.path, include_optimizer=include_optimizer, **kwargs)
  File "/home/remy/my_project/.venv/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/remy/my_project/.venv/lib/python3.11/site-packages/keras/src/saving/saving_api.py", line 114, in save_model
    raise ValueError(
ValueError: Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=/tmp/tmp394vyky1bentoml_model_my_keras_model/.

Expected behavior

Model is correctly saved with bentoml.keras.save_model.

Environment

bentoml: 1.3.2
keras: 3.5.0 (transitively through tensorflow 2.17)
python: 3.11
os: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions