Tobitor
Tobitor

Reputation: 1508

model.trainable = False - weights frozen and untrainable?

I am using EfficientNet model (https://keras.io/api/applications/efficientnet/#efficientnetb0-function) with weights from ImageNet and I want to use a customized top, so I stated top = False. I am now wondering if the weights of the EfficientNet are frozen and they are not getting retrained (that is what I want) when I use the following code:

efnB0_model = efn.EfficientNetB0(include_top=False, weights="imagenet", input_shape=(224, 224, 3))
efnB0_model.trainable = False

Or do I have to use another code?

Thanks a lot!

Upvotes: 1

Views: 254

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36684

What you did works, but people generally do it layer by layer instead, because you might eventually decide to unfreeze certain layers:

for layer in model.layers:
    layer.trainable = False

model.layers returns a list, so you can also unfreeze just the last few layers:

for layer in model.layers[-10:]:
    layer.trainable = False

You can verify what can be trained with

model.trainable_variables
[]

In this case, nothing.

Upvotes: 2

Related Questions