【问题标题】:How to add a Pooling layer on a keras model?如何在 keras 模型上添加池化层?
【发布时间】:2023-03-12 06:47:02
【问题描述】:

我正在使用谷歌的 tensorflow 和 colab notbook 加载神经网络。我移除了输出层的全连接层,并添加了另一个只有一个神经元的全连接层,我冻结了另一层。我正在使用tf.keras.application.MobileNetV2,我正在使用mledu-datasets/cats_and_dogs。我只想训练这个添加的输出层,但我得到了一个“错误”。我想我必须使用

添加一个池化层

我的代码如下:

model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(IMG_HEIGHT, IMG_WIDTH ,3), alpha=1.0, include_top=False, weights='imagenet', input_tensor=None , pooling='max', classes=2)
model.summary()
penultimate_layer = model.layers[-2]  # layer that you want to connect your new FC layer to 
new_top_layer = tf.keras.layers.Dense(1)(penultimate_layer.output) # create new FC layer and connect it to the rest of the model
new_model = tf.keras.models.Model(model.input, new_top_layer)  # define your new model


ultima_layer = new_model.layers[-1]
new_new_top_layer = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)


new_new_model = tf.keras.models.Model(new_model.input, new_new_top_layer)

最后,在最后一层之前冻结所有层的权重:

for layer in new_model.layers[:-2]:
    layer.trainable = False
new_model.layers[-1].trainable = True

培训:

new_model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])


history = new_model.fit_generator(
    train_data_gen,
    steps_per_epoch = total_train // batch_size,
    epochs = epochs,
    validation_data = val_data_gen,
    validation_steps = total_val // batch_size
)

我收到以下错误

AttributeError                            Traceback (most recent call last)

<ipython-input-18-05a947aac1cd> in <module>()
      8 ultima_layer = new_model.layers[-1]
      9 new_new_top_layer = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)
---> 10 new_new_model = tf.keras.models.Model(new_model.input, new_new_top_layer)
     11 
     12 # tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)

5 frames

/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
    208     if getattr(tensor, '_keras_history', None) is not None:
    209       continue
--> 210     op = tensor.op  # The Op that created this Tensor.
    211     if op not in processed_ops:
    212       # Recursively set `_keras_history`.

AttributeError: 'AveragePooling2D' object has no attribute 'op'

谢谢

【问题讨论】:

    标签: python-3.x tensorflow keras neural-network


    【解决方案1】:

    这可能会有所帮助。我在像这样构成新模型之前添加了 PoolingLayer,但没有收到您看到的错误。我希望这会有所帮助:

    new_top_layer = tf.keras.layers.Dense(1)(penultimate_layer.output) # create new FC layer and connect it to the rest of the model
    new_new_top_layer = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)(new_top_layer)
    new_model = tf.keras.models.Model(inputs=model.input, outputs=new_new_top_layer)  # define your new model
    

    【讨论】:

      【解决方案2】:

      您可以在实例化 MobileNetV2 时传递 pooling='avg' 参数,以便在最后一层获得全局平均池值(因为您的模型不包括顶层)。由于这是一个二元分类问题,您的最后一层/输出层应该有一个具有单节点和 sigmoid 激活函数的密集层。因此,您可以添加带有单个节点的最后/输出Dense 层,并按如下方式提供基本模型的输出。

      model = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(IMG_HEIGHT, IMG_WIDTH, 3), alpha=1.0, include_top=False, weights='imagenet', input_tensor=None , pooling='avg', classes=2)
      # model.summary()
      penultimate_layer = model.layers[-1]  # layer that you want to connect your new FC layer to
      new_top_layer = tf.keras.layers.Dense(1, activation='sigmoid')(penultimate_layer.output) # create new FC layer and connect it to the rest of the model
      new_model = tf.keras.models.Model(model.input, new_top_layer)  # define your new model
      print(new_model.summary())
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-04-20
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多