Keras Transfer-Learning 设置 layers.trainable 为 True 没有效果

问题描述

我想使用 tf.keras (tensorflow 2.3) 微调高效网络,但我无法正确更改层的训练状态。我的模型看起来像这样:

data_augmentation_layers = tf.keras.Sequential([
 keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),keras.layers.experimental.preprocessing.Randomrotation(0.8)])

efficientnet = EfficientNetB3(weights="imagenet",include_top=False,input_shape=(*img_size,3))

#Setting to not trainable as described in the standard keras FAQ
efficientnet.trainable = False

inputs = keras.layers.Input(shape=(*img_size,3))
augmented = augmentation_layers(inputs)
base = efficientnet(augmented,training=False)
pooling = keras.layers.GlobalAveragePooling2D()(base)
outputs = keras.layers.Dense(5,activation="softmax")(pooling)

model = keras.Model(inputs=inputs,outputs=outputs)

model.compile(loss="categorical_crossentropy",optimizer=keras_opt,metrics=["categorical_accuracy"])

这样做是为了让我在自定义顶部的随机重量不会尽快破坏重量。

    Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None,512,3)]     0         
_________________________________________________________________
sequential (Sequential)      (None,3)       0         
_________________________________________________________________
efficientnetb3 (Functional)  (None,16,1536)      10783535  
_________________________________________________________________
global_average_pooling2d (Gl (None,1536)              0         
_________________________________________________________________
dense (Dense)                (None,5)                 7685      
=================================================================
Total params: 10,791,220
Trainable params: 7,685
Non-trainable params: 10,783,535

到目前为止,一切似乎都正常。我训练我的模型 2 个时期,然后我想开始微调有效网络基础。因此我称

for l in model.get_layer("efficientnetb3").layers:
  if not isinstance(l,keras.layers.Batchnormalization):
    l.trainable = True

model.compile(loss="categorical_crossentropy",metrics=["categorical_accuracy"])

我重新编译并再次打印摘要,以查看不可训练权重的数量保持不变。同样,拟合并不会带来比保持冷冻更好的效果

 dense (Dense)                (None,5)                 7685      
    =================================================================
    Total params: 10,220
    Trainable params: 7,685
    Non-trainable params: 10,535

Ps:我也试过 efficientnet3.trainable = True 但这也没有效果

这可能与我同时使用顺序模型和函数模型有关吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)