首页 文章

加载重量后如何在keras中添加和删除新图层?

提问于
浏览
17

我正在努力做转学习;为此我想删除神经网络的最后两层并添加另外两层 . 这是一个示例代码,它也输出相同的错误 .

from keras.models import Sequential
from keras.layers import Input,Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dropout, Activation
from keras.layers.pooling import GlobalAveragePooling2D
from keras.models import Model

in_img = Input(shape=(3, 32, 32))
x = Convolution2D(12, 3, 3, subsample=(2, 2), border_mode='valid', name='conv1')(in_img)
x = Activation('relu', name='relu_conv1')(x)
x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x)
x = Convolution2D(3, 1, 1, border_mode='valid', name='conv2')(x)
x = Activation('relu', name='relu_conv2')(x)
x = GlobalAveragePooling2D()(x)
o = Activation('softmax', name='loss')(x)
model = Model(input=in_img, output=[o])
model.compile(loss="categorical_crossentropy", optimizer="adam")
#model.load_weights('model_weights.h5', by_name=True)
model.summary()

model.layers.pop()
model.layers.pop()
model.summary()
model.add(MaxPooling2D())
model.add(Activation('sigmoid', name='loss'))

我使用 pop() 删除了图层,但是当我尝试添加其输出此错误时

AttributeError:'Model'对象没有属性'add'

我知道错误最可能的原因是 model.add() 的使用不当 . 我应该使用什么其他语法?

EDIT:

我试图在keras中删除/添加图层但是在加载外部权重后不允许添加它 .

from keras.models import Sequential
from keras.layers import Input,Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dropout, Activation
from keras.layers.pooling import GlobalAveragePooling2D
from keras.models import Model
in_img = Input(shape=(3, 32, 32))

def gen_model():
    in_img = Input(shape=(3, 32, 32))
    x = Convolution2D(12, 3, 3, subsample=(2, 2), border_mode='valid', name='conv1')(in_img)
    x = Activation('relu', name='relu_conv1')(x)
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x)
    x = Convolution2D(3, 1, 1, border_mode='valid', name='conv2')(x)
    x = Activation('relu', name='relu_conv2')(x)
    x = GlobalAveragePooling2D()(x)
    o = Activation('softmax', name='loss')(x)
    model = Model(input=in_img, output=[o])
    return model

#parent model
model=gen_model()
model.compile(loss="categorical_crossentropy", optimizer="adam")
model.summary()

#saving model weights
model.save('model_weights.h5')

#loading weights to second model
model2=gen_model()
model2.compile(loss="categorical_crossentropy", optimizer="adam")
model2.load_weights('model_weights.h5', by_name=True)

model2.layers.pop()
model2.layers.pop()
model2.summary()

#editing layers in the second model and saving as third model
x = MaxPooling2D()(model2.layers[-1].output)
o = Activation('sigmoid', name='loss')(x)
model3 = Model(input=in_img, output=[o])

它显示此错误

RuntimeError: Graph disconnected: cannot obtain value for tensor input_4 at layer "input_4". The following previous layers were accessed without issue: []

2 回答

  • 32

    您可以获取最后一个模型的 output 并创建一个新模型 . 较低的层保持不变 .

    model.summary()
    model.layers.pop()
    model.layers.pop()
    model.summary()
    
    x = MaxPooling2D()(model.layers[-1].output)
    o = Activation('sigmoid', name='loss')(x)
    
    model2 = Model(input=in_img, output=[o])
    model2.summary()
    

    检查How to use models from keras.applications for transfer learnig?

    Update on Edit:

    新错误是因为您正在尝试在全局 in_img 上创建新模型,而该模型实际上并未在先前的模型创建中使用..您实际上是在定义本地 in_img . 所以全局 in_img 显然没有连接到符号图中的上层 . 它与加载重量无关 .

    为了更好地解决此问题,您应该使用 model.input 来引用输入 .

    model3 = Model(input=model2.input, output=[o])

  • 2

    另一种方法

    from keras.models import Model
    
    layer_name = 'relu_conv2'
    model2= Model(inputs=model1.input, outputs=model1.get_layer(layer_name).output)
    

相关问题