Problem in Loading h5 file of TF huggingface model fine tuned by keras

1 – I loaded the huggingface model layoutlmv3 :

model = TFAutoModel.from_pretrained("microsoft/layoutlmv3-base")

2 – I used keras Model subclass to make adding other layers to the “model” possible :

class lmv3(tf.keras.Model) : 
    
    def __init__(self,lm):
        super().__init__()
        self.lm = lm
        
    def call(self,inputs):
        
        x= self.lm(inputs).last_hidden_state
       
        return x
    def get_config(self):
        # Return a dictionary of configuration for the layer
        return {'lm': self.lm.get_config()}

    @classmethod
    def from_config(cls, config):
        # Create a new instance of the layer using the Hugging Face model configuration
        lm = TFAutoModel.from_config(config['lm'])
        return cls(lm=lm)

3 – I made my own sequential model using the hgf model :

mm =lmv3(model)
sequential_model = tf.keras.Sequential([
    mm,
    tf.keras.layers.GlobalMaxPooling1D(),  # Add other layers as needed
    tf.keras.layers.Reshape((-1,)),
    tf.keras.layers.Dropout(0.2),
   tf.keras.layers.Dense(128),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(50),
tf.keras.layers.Dropout(0.2),

    tf.keras.layers.Dense(5, activation='softmax')
])

4- sequential model compiling :

from tensorflow_addons.optimizers import AdamW
optimizer = AdamW(learning_rate=1e-4,weight_decay=1e-5)
#tf.keras.optimizers.Adam(0.0001)
sequential_model.compile(optimizer,
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])

5 -fit model :

from tensorflow_addons.optimizers import AdamW
optimizer = AdamW(learning_rate=1e-4,weight_decay=1e-5)
#tf.keras.optimizers.Adam(0.0001)
sequential_model.compile(optimizer,
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])

the training done successfully
6 – save the model:

sequential_model.save("tflayoutlmlite1.h5")

the model is saved

but when load it

tf.keras.models.load_model("/kaggle/working/tflayoutlmlite1.h5")

i get this error :

 --------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[161], line 1
----> 1 tf.keras.models.load_model("/kaggle/working/tflayoutlmlite1.h5")

File /opt/conda/lib/python3.10/site-packages/keras/saving/saving_api.py:212, in load_model(filepath, custom_objects, compile, safe_mode, **kwargs)
    204     return saving_lib.load_model(
    205         filepath,
    206         custom_objects=custom_objects,
    207         compile=compile,
    208         safe_mode=safe_mode,
    209     )
    211 # Legacy case.
--> 212 return legacy_sm_saving_lib.load_model(
    213     filepath, custom_objects=custom_objects, compile=compile, **kwargs
    214 )

File /opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /opt/conda/lib/python3.10/site-packages/keras/saving/legacy/hdf5_format.py:808, in load_weights_from_hdf5_group(f, model)
    806 layer_names = filtered_layer_names
    807 if len(layer_names) != len(filtered_layers):
--> 808     raise ValueError(
    809         "Layer count mismatch when loading weights from file. "
    810         f"Model expected {len(filtered_layers)} layers, found "
    811         f"{len(layer_names)} saved layers."
    812     )
    814 # We batch weight value assignments in a single backend call
    815 # which provides a speedup in TensorFlow.
    816 weight_value_tuples = []

ValueError: Layer count mismatch when loading weights from file. Model expected 0 layers, found 4 saved layers.

the people who had experience with transfer learning and hgf pleas help

Leave a Comment