Repository navigation
Using .h5 model from Keras 2.6 #20617
Replies: 2 comments
|
Hi, The error is happening because the In particular: TypeError: Object of type function is not JSON serializable usually happens when a custom activation/function/layer is passed incorrectly while loading the model. A few things you can try:
model = keras.models.load_model(
"EfficientPoseI.h5",
custom_objects={
"BilinearWeights": helpers.keras_BilinearWeights,
"Swish": helpers.Swish,
"eswish": helpers.eswish,
"swish1": helpers.swish1,
},
compile=False
) |
|
There is a specific loader issue behind this traceback. Keras 3.7 resolves the saved activation name to your Python function, then tries to JSON-serialize that configuration before calling Try Keras 3.15.1 in your new environment. Its legacy deserializer no longer performs that JSON round-trip. Use the public
This addresses the shown JSON error; it doesn't establish that every custom component in the old file is compatible. If loading reaches a different error, that next traceback would identify the remaining migration work. |
Uh oh!
There was an error while loading. Please reload this page.
I've been working with the models from this repository: https://github.com/daniegr/EfficientPose/tree/master
It provides model and scripts for 3 framework but I mostly only use Keras.
Python and module versions that I use:
Python 3.7
Tensorflow 2.6
Keras 2.6
With the environment setup above, I can load the model from master/models/keras and run model.predict. It works, just with a couple of warning of deprecation. The problem is that I want to migrate it to Python 3.12 and keras 3.7 (for better performance and compatibility with my other scripts). I've looked online for guide to migration but the original project only provided .h5 files, no training or model declaration code and the repo has been inactive for a while.
I'm asking if it is possible to load models from old .h5 files and where should I start? So far I've tried installing newer environment and updating import for
EfficientPose/tree/master/utils/helpers.py.I tried:
from utils import helpersfrom keras._tf_keras.keras.models import load_modelmodel = load_model(f'path\models\\keras\\EfficientPoseI.h5', custom_objects={'BilinearWeights': helpers.keras_BilinearWeights, 'Swish': helpers.Swish(helpers.eswish), 'eswish': helpers.eswish, 'swish1': helpers.swish1})Got error:
File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\saving\saving_api.py", line 196, in load_modelreturn legacy_h5_format.load_model_from_hdf5(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\legacy_h5_format.py", line 133, in load_model_from_hdf5model = saving_utils.model_from_config(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\saving_utils.py", line 85, in model_from_configreturn serialization.deserialize_keras_object(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\serialization.py", line 495, in deserialize_keras_objectdeserialized_obj = cls.from_config(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\models\model.py", line 527, in from_config return functional_from_config(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\models\functional.py", line 546, in functional_from_configprocess_layer(layer_data)File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\models\functional.py", line 514, in process_layerlayer = saving_utils.model_from_config( File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\saving_utils.py", line 85, in model_from_configreturn serialization.deserialize_keras_object(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\serialization.py", line 490, in deserialize_keras_objectcls_config = _find_replace_nested_dict(File "D:\python\Fall Detection Project\EfficientPose312\.venv\Lib\site-packages\keras\src\legacy\saving\serialization.py", line 571, in _find_replace_nested_dictdict_str = json.dumps(config)File "C:\Users\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 231, in dumps return _default_encoder.encode(obj)File "C:\Users\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True)File "C:\Users\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 258, in iterencode return _iterencode(o, 0)File "C:\Users\AppData\Local\Programs\Python\Python312\Lib\json\encoder.py", line 180, in default raise TypeError(f'Object of type {o.__class__.__name__} 'TypeError: Object of type function is not JSON serializableAll reactions