簡體   English   中英

Keras Transfer-Learning 設置 layers.trainable 為 True 沒有效果

[英]Keras Transfer-Learning setting layers.trainable to True has no effect

我想使用 tf.keras (tensorflow 2.3) f.netune efficien.net 但我無法正確更改圖層的訓練狀態。 我的 model 看起來像這樣:

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, 512, 3)]     0         
_________________________________________________________________
sequential (Sequential)      (None, 512, 512, 3)       0         
_________________________________________________________________
efficientnetb3 (Functional)  (None, 16, 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

到目前為止,一切似乎都有效。 我訓練我的 model 2 個時期,然后我想開始微調 efficien.net 基礎。 因此我打電話

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

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

我重新編譯並再次打印摘要,發現不可訓練權重的數量保持不變。 此外,貼合不會帶來比保持冷凍更好的效果。

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

Ps:我也試過efficien.net3.trainable = True但這也沒有效果。

難道這與我同時使用順序和功能 model 這一事實有關嗎?

對我來說,問題是對 model 的一部分使用順序 API。當我更改為順序時,我的 model.sumary() 顯示了所有子層,並且可以將其中一些設置為可訓練的,而另一些則不能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM