[开发技巧]·keras如何冻结网络层

2019-06-27 14:09:16 浏览数 (1)

[开发技巧]·keras如何冻结网络层

在使用keras进行进行finetune有时需要冻结一些网络层加速训练

keras中提供冻结单个层的方法:layer.trainable = False

这个应该如何使用?下面给大家一些例子

1.冻结model所有网络层

代码语言:javascript复制
base_model = DenseNet121(include_top=False, weights="imagenet",input_shape=(224, 224, 3))
for layer in base_model.layers:
    layer.trainable = False

2.冻结model某些网络层

在keras中除了从model.layers取得layer,我们还可以通过model.get_layer(layer_name)获取。

代码语言:javascript复制
base_model = VGG19(weights='imagenet')
base_model.get_layer('block4_pool').trainable = False

你可能会疑问,我不知道layer_name该怎么办呢?答案是通过model.summary()输出一下,

如下所示,最左面一列就是layer_name(注意是括号外面的><)

代码语言:javascript复制
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer)            (None, 224, 224, 3)  0
__________________________________________________________________________________________________
NASNet (Model)                  (None, 7, 7, 1056)   4269716     input_1[0][0]
__________________________________________________________________________________________________
resnet50 (Model)                (None, 7, 7, 2048)   23587712    input_1[0][0]
__________________________________________________________________________________________________
densenet121 (Model)             (None, 7, 7, 1024)   7037504     input_1[0][0]
__________________________________________________________________________________________________
global_average_pooling2d_1 (Glo (None, 1056)         0           NASNet[1][0]
__________________________________________________________________________________________________
global_average_pooling2d_2 (Glo (None, 2048)         0           resnet50[1][0]
__________________________________________________________________________________________________
global_average_pooling2d_3 (Glo (None, 1024)         0           densenet121[1][0]
__________________________________________________________________________________________________
concatenate_5 (Concatenate)     (None, 4128)         0           global_average_pooling2d_1[0][0]
                                                                 global_average_pooling2d_2[0][0]
                                                                 global_average_pooling2d_3[0][0]
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 4128)         0           concatenate_5[0][0]
__________________________________________________________________________________________________
classifier (Dense)              (None, 200)          825800      dropout_1[0][0]
==================================================================================================
Total params: 35,720,732
Trainable params: 825,800
Non-trainable params: 34,894,932
__________________________________________________________________________________________________
None

hope this helps

0 人点赞