How to freeze certain layers in models obtained from keras.applications

I am currrently trainning to use transfer learning on ResNet152 obtained from Keras Applications:

tf.keras.applications.ResNet152(
    weights=imagenet,
    input_shape=(400,250,3)
)

I know to freeze all the layers I need to set the trainable attribute to False, but right now I need to freeze certain layers. More specifically, I need to unfreeze the last three layers of this model but freeze the rest. So how do I do that?

Topic finetuning transfer-learning keras

Category Data Science


you can freeze all the layer with model.trainable = False and unfreeze the last three layers with :

for layer in model.layers[-3:]:
    layer.trainable = True

the model.layers contain a list of all the ordered layer that compose the model.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.