【问题标题】:ValueError: You are trying to load a weight file containing 14 layers into a model with 3 layersValueError:您正在尝试将包含 14 层的权重文件加载到具有 3 层的模型中
【发布时间】:2019-06-12 16:50:47
【问题描述】:

(我无法通过之前提出的问题找到解决方案。) 我使用 VGG16 来测试我的数据。我的班级编号是 2,我使用 this page 来冻结卷积层并训练顶层。代码如下:

from keras.applications import VGG16
model = VGG16(include_top=False,classes=2,input_shape(224,224,3),weights='imagenet')

然后我创建了 top_model,它将成为我的 Vgg16 的顶级模型:

top_model=Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(4096,activation='relu'))
top_model.add(Dense(4096,activation='relu'))
top_model.add(Dense(2,activation='softmax'))
model= Model(inputs=model.input, output=top_model(model.output))

然后我冻结了一些层并编译模型:

for layer in model.layers[:19]:
 layer.trainable=False

model.compile(loss='binary_crossentropy',optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
           metrics=['accuracy'])

经过一些数据增强过程,我训练了模型并保存了这样的权重:

model.fit_generator(trainGenerator,samples_per_epoch=numTraining//batchSize,
                  epochs=numEpoch,
                  validation_data=valGenerator,
                  validation_steps=numValid//batchSize)

model.save_weights('top3_weights.h5') 

之后,我的权重被保存,我更改了代码的第二部分,以便能够使用我的数据测试整个模型:

model = VGG16(include_top=False,classes=2,input_shape=(224,224,3),weights='imagenet')

top_model=Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(4096,activation='relu'))
top_model.add(Dense(4096,activation='relu'))
top_model.add(Dense(2,activation='softmax'))
top_model.load_weights(r'C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\top3_weights.h5') #(this line is added)
model= Model(inputs=model.input, output=top_model(model.output)) 

最后当我尝试用代码总结模型时:

print(model.summary())

我收到了错误和输出:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\Umit Kilic\Komodomyfiles\umit\test_vgg16.py", line 38, in <module>
    top_model.load_weights(r'C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\top3_weights.h5')
  File "C:\Users\Umit Kilic\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1166, in load_weights
f, self.layers, reshape=reshape)
  File "C:\Users\Umit Kilic\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\saving.py", line 1030, in load_weights_from_hdf5_group
str(len(filtered_layers)) + ' layers.')
ValueError: You are trying to load a weight file containing 14 layers into a model with 3 layers.

有什么帮助吗?

【问题讨论】:

    标签: python tensorflow keras valueerror pre-trained-model


    【解决方案1】:

    model包含完整模型,即VGG加top_model的堆叠模型。当你保存它的权重时,它有 14 层。您无法将其加载到 top_model,因为 VGG 权重也在其中,但您可以在新的 model 中加载。

    model = VGG16(include_top=False,classes=2,input_shape=(224,224,3),weights='imagenet')
    top_model=Sequential()
    top_model.add(Flatten(input_shape=model.output_shape[1:])) 
    top_model.add(Dense(4096,activation='relu'))
    top_model.add(Dense(4096,activation='relu'))
    top_model.add(Dense(2,activation='softmax'))
    model= Model(inputs=model.input, output=top_model(model.output))
    
    model.load_weights(r'C:\\Users\\Umit Kilic\\Komodomyfiles\\umit\\top3_weights.h5') #(this line is added)
    

    【讨论】:

    • 谢谢,现在可以使用了。但我无法理解其中的逻辑。除了最后三层(全连接层)之外,“模型”已经拥有所有层和权重(imagenet 权重)。所以,我训练最后三层并创建文件('top3_weights.h5')应该只保存最后三层的权重。如果我们考虑答案,top3_weights.h5 文件必须存储所有层的权重(用于 conv 层的 imagenet + 最后三层的新创建的权重)。对于新创建的权重文件,哪一个是正确的?
    • 权重文件有所有,我相信如果你只想保存你训练过的层你必须调用top_layer.save_weights而不是model.save_weights。请注意,即使它们不可训练,也会保存所有层权重。
    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2019-01-03
    • 2018-11-27
    • 2019-04-10
    相关资源
    最近更新 更多