如何加载微调的Keras模型

我正在按照this教程尝试使用VGG16模型进行微调,我训练了模型并使用.h5

保存了model.save_weights文件
vgg_conv = VGG16(include_top=False,weights='imagenet',input_shape=(image_size,image_size,3))

    # Freeze the layers except the last 4 layers
    for layer in vgg_conv.layers[:-4]:
        layer.trainable = False

    model = Sequential()
    model.add(vgg_conv)
    model.add(flatten())
    model.add(Dense(256,activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(11,activation='softmax'))

然后我尝试使用以下内容重建架构和负载权重

def create_model(self):
    model = Sequential()
    vgg_model = VGG16(include_top=False,input_shape=(150,150,3))
    model.add(vgg_model)
    model.add(flatten())
    model.add(Dense(256,activation='softmax'))
    model.load_weights(self.top_model_weights_path) # throws error
    return model

但是它会引发此错误

ValueError: Cannot feed value of shape (512,512,3,3) for Tensor 'Placeholder:0',which has shape '(3,64)'

我在做什么错了?

jxdlzz 回答:如何加载微调的Keras模型

我不确定如何解释该错误,但您可以在微调后尝试model.save("model.h5")将模型架构和权重保存在一起。

要加载模型,您可以输入

model = load_model('model.h5')
# summarize model.
model.summary()

我认为这具有不必重建模型的好处,并且只需一行即可完成相同的目的。

,

问题来自两个模型之间的可训练差异。如果您冻结了create_model函数中的最后4个层,它将起作用。

但是正如Igna所说,model.save和model.load_model更简单。

本文链接:https://www.f2er.com/3166882.html

大家都在问