我正在使用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,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,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'
谢谢