Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

applications.VGG16

Builds the VGG16 convolutional network with optional ImageNet weights and classifier. It supports standard classification or feature extraction with custom inputs and pooling.

How it works

VGG16 creates the 16-layer VGG convolutional network, optionally including its fully connected ImageNet classifier. Set weights to ImageNet or a file, choose an input tensor or compatible shape, and optionally replace the classifier with global pooling; preprocess inputs with the VGG16 utility before inference.

Syntax

keras.applications.VGG16(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation="softmax", name="vgg16")

Parameters

NameTypeDescription
include_topboolWhether to include the three fully connected classifier layers.
weightsNone, strNone, "imagenet", or a path to a weights file.
input_tensorKerasTensorOptional Keras input tensor.
input_shapetupleOptional three-channel shape, required when include_top=False; spatial dimensions must be at least 32.
poolingstr or NonePooling for feature extraction: None, "avg", or "max".
classesintNumber of classifier classes.
classifier_activationstr or NoneClassifier activation, normally "softmax"; may be None only with random weights.
namestrModel name.

Returns

A Keras model instance representing VGG16.

Key points

  • The function builds and returns a VGG16 model.
  • The standard spatial input resolution is 224×224.
  • include_top controls whether the original three dense classifier layers are present.
  • weights accepts random initialization, ImageNet weights, or a weights-file path.
  • input_tensor can connect an existing Keras input tensor to the model.
  • When the classifier is omitted, pooling can be disabled or set to global average or max pooling.
  • classes specifies the classifier output count when the top is used.

Common mistakes

  • Raw RGB image arrays are not ready for the model; apply the VGG16 preprocessing function first.
  • When retaining the classifier, use the required 224×224 three-channel input shape.
  • Custom shapes must have three channels and spatial dimensions of at least 32.

Example: Extract VGG16 features

import keras
import numpy as np

model = keras.applications.VGG16(
    weights="imagenet", include_top=False, pooling="avg"
)
x = np.random.randint(0, 256, (1, 224, 224, 3), dtype="uint8")
x = keras.applications.vgg16.preprocess_input(x.astype("float32"))
features = model.predict(x, verbose=0)
print(features.shape)

Builds an ImageNet-pretrained VGG16 without its classifier, preprocesses one 224×224 RGB image, and extracts a pooled feature vector.

When to use it

Image classification

Run 1,000-class ImageNet inference using pretrained weights and the original classifier.

Feature extraction

Remove the dense head and use convolutional outputs as inputs to another model.

Custom training

Initialize the architecture with random weights and train it for a new task.

Weights restoration

Load a saved compatible weights file for an adapted deployment or experiment.

Interview questions

QWhat does keras.applications.VGG16() construct by default?

VGG16 returns a model containing the VGG16 architecture. By default it includes the three fully connected layers and uses ImageNet weights with a softmax classifier.

Follow-up: How would you turn it into a feature extractor? Set include_top=False and optionally choose pooling="avg" or pooling="max".

QHow do include_top and input_shape interact?

include_top=False removes the classifier, allowing a custom input_shape subject to the three-channel and minimum-size constraints. With the top included, the expected spatial input is 224×224.

Follow-up: Why might pooling be useful? It converts the final convolutional output into a fixed-size representation without adding the original dense classifier.

QWhat preprocessing does VGG16 require?

The VGG16 preprocessing utility changes RGB images to BGR and zero-centers each channel using ImageNet statistics, without scaling values. This preprocessing should happen before the model receives the inputs.

Follow-up: What is a practical consequence? Feeding ordinary RGB tensors directly can produce poor predictions because their channel order and centering do not match training.

  • keras.applications.vgg16.preprocess_input
  • keras.applications.vgg16.decode_predictions
  • keras.applications.VGG19

Source: VGG16 and VGG19 models. Checked 2026-10-15.