How to reuse VGG19 for image classification in Keras?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Reusing VGG19 in Keras is a standard transfer-learning workflow: load the pretrained convolutional base, attach a classifier head for your labels, train the head, and optionally fine-tune some upper convolution blocks. The important part is not just calling VGG19. You need the right input size, the correct preprocessing function, and a training plan that avoids destroying the pretrained weights too early.
Load VGG19 as a Feature Extractor
Keras ships VGG19 through tensorflow.keras.applications. For most reuse scenarios, start with include_top=False so you keep the convolutional feature extractor and replace the original ImageNet classifier.
This gives you a model that uses VGG19’s learned image features but predicts five custom classes instead of the original ImageNet categories.
Use the Correct Input Preparation
VGG19 expects images shaped like ImageNet inputs: three color channels, resized to 224 x 224, and preprocessed with the VGG19-specific transform. That preprocessing is not optional if you want the pretrained weights to behave as intended.
A simple tf.data pipeline looks like this:
You can preprocess inside the model with a Lambda layer, as shown earlier, or map preprocess_input into the dataset itself. What matters is consistency.
Train the New Classifier Head First
The safe first step is to freeze the convolutional base and train only the new dense layers. That lets the classifier learn your labels without immediately changing the pretrained filters.
This stage is often enough when the new dataset is small and visually similar to ImageNet-style images. If validation accuracy improves quickly, do not assume fine-tuning is automatically necessary.
Fine-Tune Only the Upper Layers
If the baseline head training plateaus, unfreeze part of VGG19 and continue with a smaller learning rate. Do not unfreeze everything at once unless you have enough data and a clear reason.
Using a small learning rate is important. Fine-tuning is about gentle adaptation, not reinitializing the network through noisy updates.
Choose a Head That Matches the Task
The final layer depends on the problem:
- use
Dense(num_classes, activation="softmax")for single-label multi-class classification - use
Dense(1, activation="sigmoid")for binary classification - use
Dense(num_labels, activation="sigmoid")for multi-label classification
Likewise, choose the loss accordingly. A mismatch between output layer and loss is one of the fastest ways to get poor training behavior.
When VGG19 Is and Is Not a Good Fit
VGG19 is still useful as a teaching model and as a dependable transfer-learning baseline, but it is large and relatively slow compared with newer architectures. If inference speed or deployment size matters, models such as EfficientNet or MobileNet are often better defaults.
Still, VGG19 remains easy to understand because its architecture is straightforward. That makes it a good starting point for transfer learning when clarity matters more than squeezing out maximum efficiency.
Common Pitfalls
One frequent mistake is skipping preprocess_input and feeding raw pixel values directly into the pretrained model. That breaks the assumptions behind the ImageNet weights.
Another problem is unfreezing the whole model immediately on a small dataset. That often leads to overfitting or unstable training rather than better performance.
Developers also sometimes leave include_top=True, which keeps the ImageNet classifier attached. That makes the model much less reusable for a custom label set.
Finally, VGG19 expects 224 x 224 x 3 input by default. Passing grayscale or mismatched image sizes without adapting the pipeline will cause errors or poor results.
Summary
- Reuse VGG19 in Keras by loading the pretrained base with
include_top=False. - Resize images correctly and apply
preprocess_inputso the pretrained weights see the expected input format. - Freeze the base model first and train a custom classifier head.
- Fine-tune only upper layers with a low learning rate when the baseline plateaus.
- Pick output layers and losses that match the exact classification problem you are solving.

