[Python] Layer count mismatch when loading weights from file. Model expected 106 layers, found...

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have this network (from SimClR paper):

    base_model = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3))
    base_model.trainable = True
    #inputs = Input((224, 224, 3))
    inputs = Input((224, 224, 3))
    h = base_model(inputs, training=True)
    h = GlobalAveragePooling2D()(h)

    projection_1 = Dense(256)(h)
    projection_1 = Activation("relu")(projection_1)
    projection_2 = Dense(128)(projection_1)
    projection_2 = Activation("relu")(projection_2)
    projection_3 = Dense(50)(projection_2)

    resnet_simclr = Model(inputs, projection_3)


    thane after training the model, I saved it:

    resnet_simclr.save_weights(filename)


    However, after trying to load the weight in ResNet50 like this:

    inputs = tf.keras.layers.Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name="input_image")
    Resmodel = tf.keras.applications.ResNet50(input_tensor=inputs, weights=weights_path, include_top=False)


    I got this error:

    ---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    <ipython-input-92-cb642379c2ae> in <module>()
    3 from keras.utils.vis_utils import plot_model
    4 inputs = tf.keras.layers.Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3), name="input_image")
    ----> 5 Resmodel = tf.keras.applications.ResNet50(input_tensor=inputs, weights=weights_path, include_top=False) # weights="imagenet"
    6 #Resmodel.load_weights(weights_path)
    7 Resmodel.summary()

    3 frames
    /usr/local/lib/python3.7/dist-packages/keras/saving/hdf5_format.py in load_weights_from_hdf5_group(f, model)
    718 if len(layer_names) != len(filtered_layers):
    719 raise ValueError(
    --> 720 f'Layer count mismatch when loading weights from file. '
    721 f'Model expected {len(filtered_layers)} layers, found '
    722 f'{len(layer_names)} saved layers.')

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


    EDIT

    The issue I'm trying to do transfer learning (down stream) from RestNet50 with have also 3 projection layer to RestNet50 ( part of Unet backbone) but this Rasnet do not have project head like the trained model. I'm not sure how to fix that.

    Continue reading...

Compartilhe esta Página