简体   繁体   English

卷积网络:将隐藏层权重传递给其他模型

[英]Convolutional networks: passing hidden layer weights as input to other model

We built a small cnn using keras, tensorflow. 我们使用keras,tensorflow构建了一个小cnn。 We used keras's functional API for that matter. 我们使用keras的功能性API。 We're interested in passing last convolutional layer's weights (the one before the fully connected layers) as an input to other cnn. 我们对传递最后一个卷积层的权重(完全连接的层之前的权重)作为其他cnn的输入感兴趣。

for simplicity I suggest the next simplified code to discuss upon: 为简单起见,我建议讨论下一个简化的代码:

    from keras.utils import plot_model
    from keras.models import Model
    from keras.layers import Input
    from keras.layers import Dense
    from keras.layers.convolutional import Conv2D
    from keras.layers.pooling import MaxPooling2D
    visible = Input(shape=(64,64,1))
    conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    hidden1 = Dense(10, activation='relu')(pool2)
    output = Dense(1, activation='sigmoid')(hidden1)
    model = Model(inputs=visible, outputs=output)
    model.compile(optimizer='Adam',
            loss=['sparse_categorical_crossentropy', None],
            metrics=['accuracy'])
    model.fit(train_dataset,
        train_labels,
        epochs=400,
        batch_size=512,
        validation_data=(valid_dataset, valid_labels),
        verbose=1,
        callbacks=[early_stop])
    # summarize layers
    print(model.summary())
    # plot graph
    plot_model(model, to_file='convolutional_neural_network.png')

question is: how can I pass pool2 layer as an input to some other simple model using keras, so it will train simultaniously with the first model described above? 问题是:如何使用keras将pool2层作为其他简单模型的输入传递,因此它将与上述第一个模型同时进行训练?

One possible way would be to add to your model so that everything is contained in a single model that ends with 2 branches. 一种可能的方法是将其添加到模型中,以使所有内容都包含在以2个分支结尾的单个模型中。 The functional API in keras allows you to define connections between layers however you want, and also provides the infrastructure for having multiple outputs and loss functions. keras中的功能性API允许您根据需要定义层之间的连接,还提供了具有多个输出和损失功能的基础结构。

For example: 例如:

from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
visible = Input(shape=(64,64,1))
conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

#add your second model here 
X = FirstLayer()(pool2) #replace with your actual network layer 
# ... 
output2 = YourSecondOutput()(X) 

hidden1 = Dense(10, activation='relu')(pool2)
output = Dense(1, activation='sigmoid')(hidden1)
model = Model(inputs=visible, outputs=[output, output2]) #list of outputs 
model.compile(optimizer='Adam',
        loss=['sparse_categorical_crossentropy', None],
        metrics=['accuracy'])
model.fit(train_dataset,
    train_labels,
    epochs=400,
    batch_size=512,
    validation_data=(valid_dataset, valid_labels),
    verbose=1,
    callbacks=[early_stop])
# summarize layers
print(model.summary())
# plot graph
plot_model(model, to_file='convolutional_neural_network.png')

Then you'll just need to update your inputs to fit so that you have labels for each output. 然后,您只需要更新输入以适合输入,以便为每个输出添加标签。 You can find more info in the keras documentation on multi input and output models 您可以在keras文档中找到有关多输入和输出模型的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM