我在图像数据集上训练了 KERAS 模型 (EfficientNetB5),并将带有新层的新模型保存到 .keras 文件中。但是,我不断收到输入错误。
# Load efficientnet model without the top layer
efficientnetbasemodel = tf.keras.applications.EfficientNetB5(include_top=False,input_shape=(400, 400, 3))
# Freeze the base model layers
efficientnetbasemodel.trainable = False
#add new layers for training
name="efficientnet"
efficientnetmodel=tf.keras.Sequential([tf.keras.Input(shape=(None, None, 3), name="input_layer"),data_augmentation,efficientnetbasemodel,tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(128, activation='relu'),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(len(class_names), activation='softmax')], name=name)
# Compile the model
efficientnetmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
efficientnetmodel.fit(train_data,epochs=10,validation_data=val_data)
efficientnetmodel.save("efficientnetmodel.keras")
当我尝试再次加载模型时
from tensorflow.keras.models import load_model
# Load the model
model = load_model('efficientnetmodel.keras')
我收到此错误。
ValueError: Layer \"dense_8\" expects 1 input(s), but it received 2 input tensors. Inputs received: [<KerasTensor shape=(None, None, None, 2048), dtype=float32, sparse=False, name=keras_tensor_7687>, <KerasTensor shape=(None, None, None, 2048), dtype=float32, sparse=False, name=keras_tensor_7688>]" }
您需要更改模型更改的 input_shape 值。因此,您需要将代码从 input_shape=(400, 400, 3) 更改为“input_shape=(2048, none, none, 1)”。为此,您需要根据我之前指定的图像尺寸来调整图像的形状。然后在构建模型时更改 input_shape 值