Deep Learning with Keras

By LiterallyTheOne

8: Fine-tuning

Deep Learning with Keras Tutorial, Fine-tuning

Introduction

  • Previous Tutorial: Callbacks
  • This Tutorial: Fine-tuning
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Fine-tuning

  • Deep learning technique
  • Adapt to the new dataset
  • Similar to Transfer learning
  • Unfreeze some of the last layers
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Fine-tuning example

base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3))

for layer in base_model.layers[:-4]:
    layer.trainable = False

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Fine-tuning example result

print(base_model.summary(show_trainable=True))

"""
--------
output: 

  ...
├───────────────────┼─────────────────┼───────────┼────────────────┼───────┤
| block_16_project  │ (None, 7, 7,    │   307,200 │ block_16_dept… │   N   │
│ (Conv2D)          │ 320)            │           │                │       │
├───────────────────┼─────────────────┼───────────┼────────────────┼───────┤
| block_16_project… │ (None, 7, 7,    │     1,280 │ block_16_proj… │   Y   │
│ (BatchNormalizat… │ 320)            │           │                │       │
├───────────────────┼─────────────────┼───────────┼────────────────┼───────┤
│ Conv_1 (Conv2D)   │ (None, 7, 7,    │   409,600 │ block_16_proj… │   Y   │
│                   │ 1280)           │           │                │       │
├───────────────────┼─────────────────┼───────────┼────────────────┼───────┤
│ Conv_1_bn         │ (None, 7, 7,    │     5,120 │ Conv_1[0][0]   │   Y   │
│ (BatchNormalizat… │ 1280)           │           │                │       │
├───────────────────┼─────────────────┼───────────┼────────────────┼───────┤
│ out_relu (ReLU)   │ (None, 7, 7,    │         0 │ Conv_1_bn[0][… │   -   │
│                   │ 1280)           │           │                │       │
└───────────────────┴─────────────────┴───────────┴────────────────┴───────┘

Total params: 2,257,984 (8.61 MB)
Trainable params: 412,800 (1.57 MB)
Non-trainable params: 1,845,184 (7.04 MB)

"""

By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Underfitting

  • Not training well on training data
  • Model
    • Not learning the pattern
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Underfitting: simple model

  • Reason:
    • Model is too simple
  • Solution:
    • Choose a more complex model
    • More trainable layers
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Underfitting: Regularization

  • Reason:
    • To much regularization
  • Solution:
    • Choose correct regularization techniques
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Underfitting: input features

  • Reason:
    • Incorrect input features
    • Example: House pice -> not having the size
  • Solution:
    • Gather the correct features
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Overfitting

  • Training: good
  • Validation and Test: bad
  • Model
    • Understood the pattern
    • Including the noise
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Overfitting: Complex model

  • Reason:
    • Model is too complex
  • Solution:
    • Simpler model
    • lower trainable layers
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Overfitting: Over training

  • Reason:
    • Model is trained for so long
  • Solution:
    • EarlyStopping
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Overfitting: Regularization

  • Reason
    • Not enough Regularization
  • Solution:
    • Add the correct Regularization techniques
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

Generalize our classifier

model = keras.Sequential(
    [
        ...,
        layers.GlobalAveragePooling2D(),
        layers.Dropout(0.5),
        layers.Dense(128, activation="relu"),
        layers.Dropout(0.5),
        layers.Dense(4, activation="softmax"),
    ]
)
By Ramin Zarebidoky (LiterallyTheOne)
Deep Learning with Keras Tutorial, Fine-tuning

By Ramin Zarebidoky (LiterallyTheOne)