错误:您正在尝试将包含 436 层的权重文件加载到具有 437 层的模型中

问题描述

我正在尝试在 kaggle 上运行带有 nosiy 学生权重的高效网络 B7 模型,但出现错误

You are trying to load a weight file containing 436 layers into a model with 437 layers.

我的代码

model_path = '../input/keras-efficientnet-noisy-students/efficientnet-b7_noisy-student_notop.h5'  
n_labels = labels.shape[1]
with strategy.scope():
    model = tf.keras.Sequential([
        efn.EfficientNetB7(
            input_shape=(size,size,3),weights=model_path,include_top=False,drop_connect_rate=0.5),tf.keras.layers.GlobalAveragePooling2D(),tf.keras.layers.Dense(n_labels,activation='sigmoid')
    ])
    model.compile(
        optimizer='adam',loss='binary_crossentropy',metrics=[tf.keras.metrics.AUC(multi_label=True)])
    model.summary()

解决方法

在您的情况下,预训练模型层数与您创建的模型层数不匹配

如果你想要迁移学习,那么使用这个

tf.keras.applications.EfficientNetB0(
    include_top=False,weights="model_path",input_shape=(size,size,3),pooling=None,classes='Here how much classes present in your data    
)

如果你想扩展这个模型功能,那么你也可以将这个模型输出传递给你的新层,比如 (model_name.output)