Closed estebvac closed 1 week ago
To address the backward compatibility issue between Keras v2.15.0 and Keras v3.3.3, you can create a script to adjust the naming conventions and re-index the weights accordingly. some of the steps hat might help you:-
import h5py
def convert_h5_format_v2_to_v3(old_h5_path, new_h5_path): with h5py.File(old_h5_path, 'r') as old_h5, h5py.File(new_h5_path, 'w') as new_h5: for key in old_h5.keys():
new_h5.create_group('model_weights')
# Copying datasets with new naming conventions
for layer in old_h5[key]:
old_layer_group = old_h5[key][layer]
new_layer_group = new_h5['model_weights'].create_group(layer)
for param in old_layer_group:
new_param_name = param.replace(':0', '')
new_layer_group.create_dataset(new_param_name, data=old_layer_group[param])
print(f"Converted {old_h5_path} to {new_h5_path} successfully.")
convert_h5_format_v2_to_v3('old_model.h5', 'new_model.h5')
3. Load the Converted Model in Keras v3.3.3
After converting the model weights, you should be able to load the model in Keras v3.3.3 without issues, like for example:-
from tensorflow.keras.models import load_model
model = load_model('new_model.h5')
Hope, this helps Thanks
Hi @estebvac!
Could you load these models in Keras 2, save them in .keras
format and then load the .keras
format models in Keras 3?
This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.
This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.
Hi, I have a significant number of models trained using TensorFlow 2.15.0 and Keras 2.15.0, saved in HDF5 format. Upon attempting to reuse these models with Keras 3.3.3, I discovered that the models are not backward compatible due to differences in naming conventions and structure of the HDF5 files.
Observation Upon exploring the HDF5 files with both versions, I observed major differences in naming conventions between Keras v2 and Keras v3. Here is a small overview.
For Keras 3.3.3:
For Keras 2.15.0:
Besides the differences that can be easily seen, and easy to change with
h5py
The indexing of the layers and parameters seems different.
Could you please provide guidance on how to properly convert or re-index these weights from Keras v2.15.0 to Keras v3.3.3? Is there any documentation or tool available to handle this backward compatibility issue?
Thanks in advance for your assistance.