我正在使用tensorflow和来自Google的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'
谢谢
答案 0 :(得分: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
答案 1 :(得分:0)
您可以在实例化pooling='avg'
时传递MobileNetV2
参数,以便获得最后一层的全局平均池值(因为模型不包括顶层)。由于这是一个二进制分类问题,因此您的最后一个/输出层应该具有一个具有单节点和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())
希望这会有所帮助。