I'm in the unfortunate situation that my colleague left behind several .keras files, and some load fine, but many do not. I've read in various places that the api for load_model has changed and is not backward compatible. I therefore am stuck, and I need to load these files to compare to previous benchmarks. I've tried several different tensorflow/keras versions (see below), all on ubuntu (various version). Please help!

The .keras files I'm dealing with are zip files, and this is what I see with 'unzip -l':

model.keras
  Length      Date    Time    Name
---------  ---------- -----   ----
       64  1980-01-01 00:00   metadata.json
     8432  1980-01-01 00:00   config.json
379578692  2025-06-26 22:58   model.weights.h5
---------                     -------
379587188                     3 files

metadata.json contains this:

{"keras_version": "2.13.1", "date_saved": "2025-06-26@18:35:35"}

Attempt 1:

20.04.1-Ubuntu Python 3.8.10

 tf.__version__ #same for tf.keras.__version__
'2.11.0'
>>> tf.keras.models.load_model("model.keras")
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/ubuntu/.local/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
     raise e.with_traceback(filtered_tb) from None
   File "/home/ubuntu/.local/lib/python3.8/site-packages/h5py/_hl/files.py", line 567, in __init__
     fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
   File "/home/ubuntu/.local/lib/python3.8/site-packages/h5py/_hl/files.py", line 231, in make_fid
     fid = h5f.open(name, flags, fapl=fapl)
   File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
   File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
   File "h5py/h5f.pyx", line 106, in h5py.h5f.open
 OSError: Unable to open file (file signature not found)

Attempt 2/3:

Two attempts with the same minor revision of tensorflow (2.13), both produce identical Tracebacks--also I get the same error with the official docker release 2.13.0:

Ubuntu 22.04.2, Python 3.10.12, tensorflow 2.13.0 AND Ubuntu 20.04.1, Python 3.8.10, tensorflow 2.13.1

Here, tf.keras.__version__ produces an AttributeError

>>> tf.keras.models.load_model("model.keras")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/saving/saving_api.py", line 230, in load_model
    return saving_lib.load_model(
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/saving/saving_lib.py", line 275, in load_model
    raise e
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/saving/saving_lib.py", line 240, in load_model
    model = deserialize_keras_object(
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/saving/serialization_lib.py", line 704, in deserialize_keras_object
    instance = cls.from_config(inner_config)
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/engine/training.py", line 3227, in from_config
    inputs, outputs, layers = functional.reconstruct_from_config(
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/engine/functional.py", line 1502, in reconstruct_from_config
    if process_node(layer, node_data):
  File "/data/home/warren/.local/lib/python3.10/site-packages/keras/src/engine/functional.py", line 1442, in process_node
    output_tensors = layer(input_tensors, **kwargs)
TypeError: 'str' object is not callable

Attempt 4:

I tried the lastest tensorflow release with venv: 24.04.1-Ubuntu Python 3.12.3

>>> tf.__version__
'2.20.0'
>>> tf.keras.__version__
'3.11.3'
>>> tf.keras.models.load_model("model.keras")
Traceback (most recent call last):
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py", line 820, in _retrieve_class_or_fn
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'keras.src.engine'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/saving_api.py", line 189, in load_model
    return saving_lib.load_model(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/saving_lib.py", line 365, in load_model
    return _load_model_from_fileobj(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/saving_lib.py", line 442, in _load_model_from_fileobj
    model = _model_from_config(
            ^^^^^^^^^^^^^^^^^^^
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/saving_lib.py", line 431, in _model_from_config
    model = deserialize_keras_object(
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py", line 709, in deserialize_keras_object
    cls = _retrieve_class_or_fn(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/warren/tf/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py", line 830, in _retrieve_class_or_fn
    raise TypeError(
TypeError: Could not deserialize class 'Functional' because its parent module keras.src.engine.functional cannot be imported. Full object config: {...}

For the last line, I omitted the full dict because it's about 2 full terminal screens.

Also note that workaround here does not work https://github.com/keras-team/keras/issues/14265

Comment From: sonali-kumari1

Hi @wtandrews - Thanks for the detailed logs! Here are few things to consider based on your attemps:

  • For OSError: Unable to open file (file signature not found),You are using Tensorflow 2.11.0 to load.keras file while the metadata suggests Keras version 2.13.1. Please use Tensorflow/Keras 2.13.x(to match the metadata), also check if you have proper permissions and ensure the correct installation of h5py.

  • For TypeError: 'str' object is not callable, Since you tried with versions 2.13.0 and 2.13.1, Please check for any conflicting packages and try reinstalling in a fresh virtual environment. Inspecting the config.json may help.

  • ModuleNotFoundError: No module named 'keras.src.engine' with Tensorflow 2.20.0and Keras 3.11.3 is expected because internal model structure changed in Keras 3.x and it might not be compatible with model saved using keras 2.13.1.

Let me know if it helps!

Comment From: wtandrews

Thank you for the response @sonali-kumari1 . Luckily, my colleague just told me that I was missing this package, and I verified that in the 2.13.0 docker, after installing and importing the module, the load_model works: https://www.tensorflow.org/ranking/tutorials/quickstart

I am still wondering if there was a way I could have found this missing dependency without knowing the answer. Of course your suggestion to inspect config.json makes sense. There are many class names here, for example, "TFOpLambda", "GlorotUniform", etc. And, yes, one of them is "tensorflow_ranking>Bilinear". But again, without knowing that, would I just have to search for each one?

Comment From: sonali-kumari1

@wtandrews Glad to hear that you were able to load the model.

Sometimes finding missing dependencies can be tricky. As you mentioned, there were many class names(TFOpLambda, GlorotUniform, tensorflow_ranking>Bilinearetc in your case) in config.json, this can suggest custom layers or dependencies on external libraries. You can start by searching for unique or unfamiliar class names specially those with module prefixes like tensorflow_ranking> to identify required packages.

Error tracebacks can also help by pointing to missing modules or classes during model loading. It is generally a good idea to maintain a requirement.txt file to document all the dependencies for reproducibility. Thanks!

Comment From: wtandrews

Agreed, a requirements.txt is the right way for a developer to document the dependencies. On the other hand, some developers are not this organized, and maintaining package dependencies can be (unfortunately) a lot of work. In cases where this fails, error messages which point to missing packages would save a lot of development time and frustration. Eg, I initially thought that h5py was the culprit since that package produced a similar exception in another tf version. Thanks again @sonali-kumari1 !

Comment From: google-ml-butler[bot]

Are you satisfied with the resolution of your issue? Yes No