使用Keras(1.2.2),我正在加载一个最后一层为的序列模型:
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
然后,我想弹出最后一层,添加另一个完全连接的图层,然后重新添加分类图层。
model = load_model('model1.h5')
layer1 = model.layers.pop() # Copy activation_6 layer
layer2 = model.layers.pop() # Copy classification layer (dense_2)
model.add(Dense(512, name='dense_3'))
model.add(Activation('softmax', name='activation_7'))
model.add(layer2)
model.add(layer1)
print(model.summary())
正如您所看到的,我的dense_3和activation_7未连接到网络(摘要()中的空值与“已连接到”)。我在文档中找不到任何解释如何解决此问题的内容。有什么想法吗?
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130 activation_5[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
====================================================================================================
按照下面的答案,我在打印model.summary()
之前编译了模型,但由于某些原因,图层没有正确弹出,如摘要所示:最后一层的连接错误:
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632 activation_6[0][0]
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0 dense_3[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130 activation_5[0][0]
activation_7[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
dense_2[1][0]
====================================================================================================
但它应该是
dense_1 (Dense) (None, 512) 131584 flatten_1[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 512) 0 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 512) 5632 activation_5[0][0]
____________________________________________________________________________________________________
activation_7 (Activation) (None, 512) 0 dense_3[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 10) 5130
activation_7[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 10) 0 dense_2[0][0]
====================================================================================================
答案 0 :(得分:4)
删除图层时,需要重新编译模型才能使图层生效。
所以使用
model.compile(loss=...,optimizer=..., ...)
在打印摘要之前,它应该正确地集成更改。
修改:
您尝试做的事实上是顺序模式实际上非常复杂。对于您的顺序模型,我可以提出解决方案(如果有更好的请告诉我):
model = load_model('model1.h5')
layer1 = model.layers.pop() # Copy activation_6 layer
layer2 = model.layers.pop() # Copy classification layer (dense_2)
model.add(Dense(512, name='dense_3'))
model.add(Activation('softmax', name='activation_7'))
# get layer1 config
layer1_config = layer1.get_config()
layer2_config = layer2.get_config()
# change the name of the layers otherwise it complains
layer1_config['name'] = layer1_config['name'] + '_new'
layer2_config['name'] = layer2_config['name'] + '_new'
# import the magic function
from keras.utils.layer_utils import layer_from_config
# re-add new layers from the config of the old ones
model.add(layer_from_config({'class_name':type(l2), 'config':layer2_config}))
model.add(layer_from_config({'class_name':type(l1), 'config':layer1_config}))
model.compile(...)
print(model.summary())
黑客的存在是你的图层具有我无法改变的layer1.input
和layer1.output
属性。
一种方法是使用Functionnal API模型。这允许您定义进入的层和层中的内容。
首先你需要定义你的pop()函数,每次弹出一个图层时都要正确地重新链接图层,该函数来自this github issue:
def pop_layer(model):
if not model.outputs:
raise Exception('Sequential model cannot be popped: model is empty.')
popped_layer = model.layers.pop()
if not model.layers:
model.outputs = []
model.inbound_nodes = []
model.outbound_nodes = []
else:
model.layers[-1].outbound_nodes = []
model.outputs = [model.layers[-1].output]
model.built = False
return popped_layer
它只删除最后一层的每个输出链接,并将模型的输出更改为新的最后一层。现在你可以在:
中使用它model = load_model('model1.h5')
layer1 = model.layers.pop() # Copy activation_6 layer
layer2 = model.layers.pop() # Copy classification layer (dense_2)
# take model.outputs and feed a Dense layer
h = Dense(512,name='dense_3')(model.outputs)
h = Activation('relu', name=('activation_7')(h)
# apply
h = layer2(h)
output = layer1(h)
model = Model(input=model.input, output=output)
model.compile(...)
model.summary()
可能有更好的解决方案,但这就是我要做的。
我希望这会有所帮助。
答案 1 :(得分:0)
出于某种原因,我需要在添加新图层之前使用模型构建具有弹出图层的模型,以使工作正常。
conda list keras
# Name Version Build Channel
keras 2.1.5 py36_0 conda-forge
以下是代码段:
def pop_layer(model):
if not model.outputs:
raise Exception('Sequential model cannot be popped: model is empty.')
model.layers.pop()
if not model.layers:
model.outputs = []
model.inbound_nodes = []
model.outbound_nodes = []
else:
model.layers[-1].outbound_nodes = []
model.outputs = [model.layers[-1].output]
model.built = False
def get_model():
#Fully convolutional part of VGG16
model = VGG16(include_top=False, weights='imagenet')
#Remove last max pooling layer
pop_layer(model)
#Freeze pretrained layers
for layer in model.layers:
layer.trainable = False
model = Model(inputs=model.inputs, outputs=model.outputs)
print('len(model.layers)', len(model.layers)) #
print(model.summary()) #
x = GlobalAveragePooling2D()(model.output)
head = Dense(N_CLASS, activation='softmax')(x)
model = Model(inputs=model.inputs, outputs=head)
model.compile(optimizer=Adadelta(), loss='categorical_crossentropy', metrics=['accuracy'])
print('len(model.layers)', len(model.layers)) #
print(model.summary()) #
return model
答案 2 :(得分:0)
我正在使用以下功能,并且适用于我的代码:
for layer in model1.layers[:22]:
model.add(layer)