keras-io icon indicating copy to clipboard operation
keras-io copied to clipboard

Update "Image classification via fine-tuning with EfficientNet" to respect blocks when fine-tuning

Open TylerADavis opened this issue 3 years ago • 0 comments

The tutorial Image classification via fine-tuning with EfficientNet demonstrates how EfficientNet can be fine tuned to improve classification accuracy. In this example, the last 20 layers are unfrozen and tuned. However, the end section Tips for fine tuning EfficientNet makes the statement

Each block needs to be all turned on or off. This is because the architecture includes a shortcut from the first layer to the last layer for each block. Not respecting blocks also significantly harms the final performance.

Despite this statement, the tutorial itself does not respect blocks or demonstrate how to unfreeze block by block.

I am willing to contribute an update to this tutorial, in the form of the below function change, that would unfreeze the last block by name. However, before doing so I wanted to check and see whether the results I got from this change were within the expected range, or if there were any additional best practices for fine-tuning efficient net that the tutorial should be following. Additionally, was the observation that EfficientNet should be unfrozen block by block first made somewhere that we can cite it?

New unfreeze function:

def unfreeze_model(model):
    # We unfreeze the 7th block and all subsequent layers while leaving BatchNorm layers frozen
    for i, layer in enumerate(model.layers):
        if layer.name.startswith('block7'):
            break
    freeze_from = i
    for layer in model.layers[freeze_from:]:
        if not isinstance(layer, layers.BatchNormalization):
            layer.trainable = True

    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
    model.compile(
        optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]
    )

Taking this approach for the tutorial improves final accuracy from 0.7864 to 0.7916.

If these results look good, I'll go ahead and open a PR. Thanks!

TylerADavis avatar Jul 22 '22 16:07 TylerADavis