【问题标题】:How to feed a Conv2d layer output as input for a Keras model?如何将 Conv2d 层输出作为 Keras 模型的输入?
【发布时间】:2021-06-18 02:19:01
【问题描述】:

如何在 Keras 模型之上添加一层 Conv2D? 我的输入形状为 (299,299,15),为了使用预训练权重 (imagenet),输入通道必须为 3,因此我的想法是添加一个 conv2d 层,将通道从 15 更改为 3。

image = Input(shape=(299, 299, 15))
x = Conv2D(3, kernel_size=(8,8), strides=(2,2), activation='relu')(image)
model1 = Model(inputs=image, outputs=x)

model2 = InceptionResNetV2(include_top=False, weights = 'imagenet', input_tensor=None, input_shape=(299,299,3))

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    试试

    image = Input(shape=(299, 299, 15))
    x = Conv2D(3, kernel_size=(8,8), strides=(2,2), activation='relu')(image)
    model1 = Model(inputs=image, outputs=x)
    x=model1.output
    x=tf.keras.applications.InceptionResNetV2(include_top=False, weights = 'imagenet', input_tensor=None)(x)
    model2=Model(inputs=image, outputs=x)
    print(model2.summary())
    

    您可能希望将 pooling='max' 参数添加到 InceptionResNetV2 参数。这将导致输出是一个一维向量,您可以将其输入到 Dense 层中。 模型摘要是

    Model: "functional_3"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_1 (InputLayer)         [(None, 299, 299, 15)]    0         
    _________________________________________________________________
    conv2d (Conv2D)              (None, 146, 146, 3)       2883      
    _________________________________________________________________
    inception_resnet_v2 (Functio (None, None, None, 1536)  54336736  
    =================================================================
    Total params: 54,339,619
    Trainable params: 54,279,075
    Non-trainable params: 60,544
    

    【讨论】:

      【解决方案2】:

      这将首先创建一个模型,该模型以x_input=(229,229,15) 作为输入并执行卷积以将通道减少到 3。然后将该模型的输出馈送到base_ model (InceptionResNetV2) 并添加一些层,例如作为GlobalAveragePoolingDense 层。最终模型以x_input为第一层,Dense预测10个类的层为输出层。

      import tensorflow as tf
      from tensorflow.keras.models import Model
      from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
      
      # define input
      x_input = tf.keras.layers.Input(shape=(229, 229, 15))
      # convolve to go from 15 channels to 3
      x_conv = tf.keras.layers.Conv2D(3,1)(x_input)
      # model that performs convolution
      conv_model = Model(inputs=x_input, outputs=x_conv)
      # storing the model output, which will be later used as input for the base model
      conv_output=conv_model.output
      
      # defining base model
      base_model = tf.keras.applications.InceptionResNetV2(
          weights='imagenet',
          include_top=False
      )(conv_output)
      
      # add a global spatial average pooling layer
      x = GlobalAveragePooling2D()(base_model)
      # let's add a fully-connected layer
      x = Dense(1024, activation='relu')(x)
      # and a logistic layer -- let's say we have 10 classes
      predictions = Dense(10, activation='softmax')(x)
      
      # this is the model we will train
      model = Model(inputs=x_input, outputs=predictions)
      
      model.summary()
      

      View Model Summary

      【讨论】:

        猜你喜欢
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        • 1970-01-01
        • 2019-03-21
        • 2020-04-29
        • 2018-03-16
        相关资源
        最近更新 更多