Tensorflow - 如何解冻特定层?

Tensorflow - how to unfreeze only specific layer?

我正在使用这样的预训练模型:

base_model = keras.applications.Xception(
    weights='imagenet',
    input_shape=(150,150,3),
    include_top=False
)

然后我冻结所有图层:

base_model.trainable = False

现在我只想解冻,比如说最下层 当我这样做时 base_model.summary() 那是底部的内容:

所以,假设我想解冻 block14_sepconv2 层。 我愿意:

my_layer = base_model.get_layer('block14_sepconv2')
my_layer.trainable = True

并且 summary() 仍然表明 Trainable params: 0 我做错了什么?如何只解冻最底层的几层?

有趣的是,当我首先做 base_model.trainable = True,然后我从顶部开始冻结层时,可训练的参数数量实际上发生了变化。但这对我来说并不直观,而且主要不是不可理解。

这是解冻 特定图层的一种方法。我们选择相同的模型和一些层(例如 block14_sepconv2)。目的是解冻这些层并使其余层冻结

from tensorflow import keras

base_model = keras.applications.Xception(
    weights='imagenet',
    input_shape=(150,150,3),
    include_top=False
)

# free all layer except the desired layers
# which is in [ ... ]
for layer in base_model.layers:
    if layer.name not in ['block14_sepconv2', 'block13_sepconv1']:
        layer.trainable = False
    
    if layer.trainable:
        print(layer.name)

block14_sepconv2
block13_sepconv1

计算可训练和不可训练变量。

import tensorflow.keras.backend as K
import numpy as np 

trainable_count = np.sum([K.count_params(w) \
                          for w in base_model.trainable_weights])
non_trainable_count = np.sum([K.count_params(w) \
                              for w in base_model.non_trainable_weights])
print('Total params: {:,}'.format(trainable_count + non_trainable_count))
print('Trainable params: {:,}'.format(trainable_count))
print('Non-trainable params: {:,}'.format(non_trainable_count))
Total params: 20,861,480
Trainable params: 3,696,088
Non-trainable params: 17,165,392

仅供参考,不要忘记在每次冻结或解冻图层时重新编译模型 (model.compile(...))。