【问题标题】:How to give input to the middle layer in keras如何在keras中向中间层提供输入
【发布时间】:2018-10-09 22:31:30
【问题描述】:

我想在弹出最后一层然后添加 2 个 FC(Dense) 层后使用预训练的 vgg-19。我想为这些 FC 层之一提供一个二进制变量(男性或女性)的输入。我怎样才能实现它。

【问题讨论】:

    标签: keras keras-layer


    【解决方案1】:

    您需要一个双输入单输出网络,其中每个输入都有自己的特征提取,并且在预测最终输出之前融合了两个特征。

    下面是一个例子。

    import keras
    from keras.applications.vgg19 import VGG19
    from keras.layers import Input, Dense, Concatenate
    from keras.models import Model
    
    #-------------------------------------------------------------------------------
    # Define your new inputs
    # Here I pretend that your new task is a classification task over 100 classes
    #-------------------------------------------------------------------------------
    gender_input = Input(shape=(1,), name='gender_input')
    image_input  = Input(shape=(224,224,3), name='image_input')
    num_classes  = 100
    #-------------------------------------------------------------------------------
    # define your pretrained feature extraction
    # you may do something different than below
    # but the point here is to have a pretrained model for `featex`
    # so you may define it differently
    #-------------------------------------------------------------------------------
    pretrained   = VGG19()
    featex       = Model( pretrained.input, pretrained.layers[-2].output, name='vgg_pop_last' )
    # consider to freeze all weights in featex to speed-up training
    image_feat   = featex( image_input )
    #-------------------------------------------------------------------------------
    # From here, you may play with different network architectures
    # Below is just one example
    #-------------------------------------------------------------------------------
    image_feat   = Dense(128, activation='relu', name='image_fc')( image_feat )
    gender_feat  = Dense(16, activation='relu', name='gender_fc')( gender_input )
    # mix information from both information
    # note: concatenation is only one of plausible way to mix information
    concat_feat  = Concatenate(axis=-1,name='concat_fc')([image_feat, gender_feat])
    # perform final prediction
    target       = Dense(num_classes, activation='softmax', name='pred_class')( concat_feat )
    #-------------------------------------------------------------------------------
    # Here is your new model which contains two inputs and one new target
    #-------------------------------------------------------------------------------
    model = Model( inputs=[image_input, gender_input], outputs=target, name='myModel')
    
    print model.summary()
    

    【讨论】:

      【解决方案2】:

      如果你想重用第5层的old_model的层作为新模型输出,你需要像这样重建模型,关键是定义一个输入,并将其传递到第 5 层。它可能看起来像这样: FC = ... # your FC layers old_model = models.load_model(MODEL_DIR) old_model.layers.pop(-1) # Get rid of the classification layer for i in range(4): # Get rid of the first 4 layer old_model.layers.pop(0) # care pop index while iterating input = Input(shape=(INPUT_DIM,)) new_model = Model(inputs=input, outputs=FC(old_model(input)))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        相关资源
        最近更新 更多