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 3 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
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:
Ubuntu 22.04.2 Python 3.10.12
>>> tf.__version__
'2.13.0'
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 3:
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.