24
loading...
This website collects cookies to deliver better user experience
Bottleneck layer is the layer that represents the input in the lowest dimensionality space.
vgg_model = tf.keras.application.VGG19(
# last layer to be loaded is the bottleneck layer
include_top=False,
weights='imagenet',
input_shape=((150, 150, 3))
)
# 0 trainable parameters in the pre-trained model
# Feature extraction
vgg_model.trainable = False
feature_batch = vgg_model(image_batch)
global_avg_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_avg = global_avg_layer(feature_batch)
prediction_layer = tf.keras.layers.Dense(8, activation='softmax')
prediction_batch = prediction_layer(feature_batch_avg)
specialized_model = keras.Sequential([
vgg_model,
global_avg_layer,
prediction_layer
])
hub_layer = hub.KerasLayer(
"url_to_model",
input_shape=[],
dtype=tf.string,
# Fine-tuning
trainable=True
model = keras.Sequential([
hub_layer,
keras.layers.Dense(32, activation='relu'),
keras.layers.Dense(1, activation='sigmoid')
])
vgg_model.trainable
to False
. This is feature extraction. Here we only train the custom layers after the bottleneck layer.trainable
to True
. This is considered to be fine-tuning the weights of the pre-trained model. We can either update weights for all layers or some layers of the pre-trained model.Feature extraction | Fine-tuning |
---|---|
Smaller dataset (100 to 1000 examples) | Larger dataset (~ 1 million examples) |
Prediction task is different than that of the pre-trained model | Prediction task is same as or similar to that of the pre-trained model |
Preferred when budget is low | Preferred when budget is high as training time and computation cost will be higher with updating weights of pre-trained model |