[Python] TypeError: Unrecognized keyword arguments: ['batch_shape'] when loading a Keras model

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a face recognition project where I trained a model using TensorFlow/Keras and saved it as face_recog_vggface.h5. While attempting to load the model using tf.keras.models.load_model(), I encounter the following error:

    TypeError Traceback (most recent call last)
    Cell In[20], line 3
    1 # Load the trained model
    2 model_path = 'face_recog_vggface.h5'
    ----> 3 model = tf.keras.models.load_model(model_path, custom_objects={'triplet_loss': triplet_loss_function})

    File ~/.local/lib/python3.8/site-packages/keras/src/saving/saving_api.py:238, in load_model(filepath, custom_objects, compile, safe_mode, **kwargs)
    230 return saving_lib.load_model(
    231 filepath,
    232 custom_objects=custom_objects,
    233 compile=compile,
    234 safe_mode=safe_mode,
    235 )
    237 # Legacy case.
    --> 238 return legacy_sm_saving_lib.load_model(
    239 filepath, custom_objects=custom_objects, compile=compile, **kwargs
    240 )

    File ~/.local/lib/python3.8/site-packages/keras/src/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 ~/.local/lib/python3.8/site-packages/keras/src/engine/base_layer.py:870, in Layer.from_config(cls, config)
    868 return cls(**config)
    869 except Exception as e:
    --> 870 raise TypeError(
    871 f"Error when deserializing class '{cls.__name__}' using "
    872 f"config={config}.\n\nException encountered: {e}"
    873 )

    TypeError: Error when deserializing class 'InputLayer' using config={'batch_shape': [None, 224, 224, 3], 'dtype': 'float32', 'sparse': False, 'name': 'input_layer'}.

    Exception encountered: Unrecognized keyword arguments: ['batch_shape']


    # Model Definition
    from tensorflow.keras.applications.vgg16 import VGG16
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import GlobalAveragePooling2D, Dense

    def build_model():
    base_model = VGG16(include_top=False, input_shape=(224, 224, 3))
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(128, activation='relu')(x)
    model = Model(inputs=base_model.input, outputs=x)
    return model

    model = build_model()
    model.save('face_recog_vggface.h5')

    # Model Loading
    from tensorflow.keras.models import load_model
    model = load_model('face_recog_vggface.h5', custom_objects={'triplet_loss': triplet_loss})



    I saved the model after training it as face_recog_vggface.h5 and it shows the error as seen above when I try loading it why? I used tensorflow==2.13 to load it same with the version of keras, which is still 2.13.

    Continue reading...

Compartilhe esta Página