我加载保存的模型,出于微调的原因,我将分类层添加到加载模型的输出中,所以这就是我写的:
def create_keras_model():
model = tf.keras.models.load_model('model.h5', compile=False)
resnet_output = model.output
layer1 = tf.keras.layers.GlobalAveragePooling2D()(resnet_output)
layer2 = tf.keras.layers.Dense(units=256, use_bias=False, name='nonlinear')(layer1)
model_output = tf.keras.layers.Dense(units=2, use_bias=False, name='output', activation='relu')(layer2)
model = tf.keras.Model(model.input, model_output)
return model
但我发现这个错误:
ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128]
谁能帮助我并告诉我这个错误是什么以及我该如何解决这个问题。谢谢!