如何在keras模型上添加Pooling层



我正在使用谷歌的tensorflow和colab notbook加载一个神经网络。我去掉了输出层的完全连接层,添加了另一个仅与一个神经元完全连接的层,并冻结了另一层。我正在使用tf.keras.application.MobileNetV2mledu-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'

感谢

这可能会有所帮助。我在组成这样的新模型之前添加了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

您可以在实例化MobileNetV2时传递pooling='avg'参数,以便在最后一层中获得全局平均池值(因为您的模型排除了顶层(。由于这是一个二进制分类问题,您的最后/输出层应该有一个具有单节点和s形激活函数的密集层。因此,您可以添加具有单个节点的最后一个/输出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())

希望这会有所帮助。

最新更新