keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.6k stars 19.41k forks source link

Incompatibility of TensorFlow/Keras Model Weights between Versions 2.15.0 and V3 #20083

Closed estebvac closed 1 week ago

estebvac commented 1 month ago

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:

model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_139/beta
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_139/gamma
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_139/moving_mean
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_139/moving_variance
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/conv2d_142/kernel
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_140/beta
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_140/gamma
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_140/moving_mean
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_140/moving_variance
model_weights/Module/Model/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/conv2d_143/kernel

For Keras 2.15.0:

Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_57/beta:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_57/gamma:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_57/moving_mean:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/batch_normalization_57/moving_variance:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_1x1/conv2d_57/kernel:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_58/beta:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_58/gamma:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_58/moving_mean:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/batch_normalization_58/moving_variance:0
Module/Module/CrossStagePartialBlock_1024_4/CrossStagePartialBlock_1024_4_0/conv_3x3/conv2d_58/kernel: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.

Siddharth-Latthe-07 commented 1 month 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:-

  1. Install Required Libraries
  2. Define the Conversion Script Create a Python script to handle the conversion. sample snippet:-
    
    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():

Create the model_weights group in the new file

        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.")

Replace 'old_model.h5' with your original model file path

Replace 'new_model.h5' with your desired output model file path

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

Load the converted model

model = load_model('new_model.h5')


Hope, this helps Thanks
SamanehSaadat commented 1 month ago

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?

github-actions[bot] commented 3 weeks ago

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.

github-actions[bot] commented 1 week ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 1 week ago

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