简体   繁体   English

如何使用预训练的 model 的第一层来提取 Keras model 内部的特征(功能 API)

[英]How to use the first layers of a pretrained model to extract features inside a Keras model (Functional API)

I would like to use the first layers of a pre-trained model --say in Xception up and including the add_5 layer to extract features from an input.我想在 Xception 中使用预训练的 model 的第一层,并包括 add_5 层从输入中提取特征。 Then pass the output of the add_5 layer to a dense layer that will be trainable.然后将 add_5 层的 output 传递到可训练的密集层。

How can I implement this idea?我怎样才能实现这个想法?

Generally you need to reuse layers from one model, to pass them as an input to the rest layers and to create a Model object with input and output of the combined model specified. Generally you need to reuse layers from one model, to pass them as an input to the rest layers and to create a Model object with input and output of the combined model specified. For example alexnet.py from https://github.com/FHainzl/Visualizing_Understanding_CNN_Implementation.git .例如来自https://github.com/FHainzl/Visualizing_Understanding_CNN_Implementation.git的 alexnet.py。

They have他们有

from keras.models import Model

from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D

def alexnet_model():
    inputs = Input(shape=(3, 227, 227))
    conv_1 = Conv2D(96, 11, strides=4, activation='relu', name='conv_1')(inputs)
    …
    prediction = Activation("softmax", name="softmax")(dense_3)
    m = Model(input=inputs, output=prediction)
    return m

and then they take this returned model, the desired intermediate layer and make a model that returns this layer's outputs:然后他们将返回的 model(所需的中间层)作为返回该层输出的 model:

def _sub_model(self):
    highest_layer_name = 'conv_{}'.format(self.highest_layer_num)
    highest_layer = self.base_model.get_layer(highest_layer_name)
    return Model(inputs=self.base_model.input,
                 outputs=highest_layer.output)

You will need similar thing,你会需要类似的东西,

highest_layer = self.base_model.get_layer('add_5')

then continue it like然后继续它

my_dense = Dense(... name=’my_dense’)(highest_layer.output)
…

and finish with并完成

return Model(inputs=self.base_model.input,
             outputs=my_prediction)

Since highest_layer is a layer (graph node), not a connection, returning result (graph arc), you'll need to add .output to highest_layer .由于最高层是层(图形节点),而不是连接,返回结果(图形弧),您需要将.output添加到highest_layer

Not sure how exactly to combine models if the upper one is also ready.如果上面的模型也准备好了,不知道如何准确地组合模型。 Maybe something like也许像

model_2_lowest_layer = model_2.get_layer(lowest_layer_name)
upper_part_model = Model(inputs= model_2_lowest_layer.input,
                         outputs=model_2.output)
upper_part = upper_part_model()(highest_layer.output)
return Model(inputs=self.base_model.input,
             outputs=upper_part)

暂无
暂无

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

相关问题 如何在keras的功能性api中向预训练模型的底部添加图层? - How to add layers to the bottom of a pretrained model in keras' functional api? 如何在 Keras 功能 API 中创建具有多个共享层的 model? - How to create a model with multiple shared layers in Keras Functional API? 如何重用keras函数模型的层 - How to reuse the layers of keras functional model 如何使用功能 API 模型实现 CNN 并解决 keras 层中的“_keras_shape”错误 - How do I implement CNN using Functional API model and resolve '_keras_shape' error in keras layers 如何使用预训练的 keras 模型作为模型添加函数的参数? - How to use a pretrained keras model to be a parameter to a Model's add function? 将层列表添加到 keras 模型计算图(功能 API) - Adding a list of layers to a keras model computation graph (functional API) keras 功能 API model 仅传递.network 的输入和输出时如何知道层 - How does the keras functional API model knows about layers when are only passing inputs and outputs of the network 如何在训练 Keras 功能 API model 时打印不同激活层的准确性? (张量流Python) - How to print accuracy of different activation layers while training a Keras functional API model? (Tensorflow Python) 如何在自定义 Keras 模型中使用 BatchNormalization 层 - How to use BatchNormalization layers in customize Keras Model 如何使用功能 keras API 在预训练的非序列 model 中的激活层之后插入丢失层? - How to insert dropout layers after activation layers in a pre-trained non-sequential model using functional keras API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM