我正在使用渐进式学习来建立分类模型。我首先使用vgg16权重并在最后添加一个Dense层来训练模型。
prior = keras.applications.VGG16(
include_top=False,
weights='imagenet',
input_shape=(48, 48, 3)
)
model = Sequential()
model.add(prior)
model.add(Flatten())
model.add(Dense(256, activation='relu', name='Dense_Intermediate'))
model.add(Dropout(0.1, name='Dropout_Regularization'))
model.add(Dense(2, activation='sigmoid', name='Output'))
for cnn_block_layer in model.layers[0].layers:
cnn_block_layer.trainable = False
model.layers[0].trainable = False
我冻结了vgg图层,因此它们的权重不变。训练后,我保存了重量。
model.save('model65x65.h5')
我使用ImageDataGenerator增加了图像的尺寸,并加载了以前保存的模型权重,并定义了一个新模型
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(96, 96, 3), activation='relu', padding='same'))
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(96, 96, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
prior = load_model('./model65x65.h5')
现在我想将VGG16的除了前两层之外的所有层都添加到新模型中,剥离输入层,并使用剥离第一卷积层。
for layer in prior.layers[0].layers[2:]:
model.add(layer)
# re-add the feedforward layers on top
for layer in prior.layers[1:]:
model.add(layer)
# the pretrained CNN layers are already marked non-trainable
# mark off the top layers as well
for layer in prior.layers[-4:]:
layer.trainable = False
由于VGG16中有卷积层,因此当我尝试遍历先前模型时,我总是收到此错误。
AttributeError:“ Conv2D”对象没有属性“ layers” 我该如何遍历Conv2D层?