Image classification
Run 1,000-class ImageNet inference using pretrained weights and the original classifier.
Keras · Class
Builds the VGG16 convolutional network with optional ImageNet weights and classifier. It supports standard classification or feature extraction with custom inputs and pooling.
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.
keras.applications.VGG16(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation="softmax", name="vgg16")| Name | Type | Description |
|---|---|---|
include_top | bool | Whether to include the three fully connected classifier layers. |
weights | None, str | None, "imagenet", or a path to a weights file. |
input_tensor | KerasTensor | Optional Keras input tensor. |
input_shape | tuple | Optional three-channel shape, required when include_top=False; spatial dimensions must be at least 32. |
pooling | str or None | Pooling for feature extraction: None, "avg", or "max". |
classes | int | Number of classifier classes. |
classifier_activation | str or None | Classifier activation, normally "softmax"; may be None only with random weights. |
name | str | Model name. |
A Keras model instance representing VGG16.
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.classes specifies the classifier output count when the top is used.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.
Run 1,000-class ImageNet inference using pretrained weights and the original classifier.
Remove the dense head and use convolutional outputs as inputs to another model.
Initialize the architecture with random weights and train it for a new task.
Load a saved compatible weights file for an adapted deployment or experiment.
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".
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.
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_inputkeras.applications.vgg16.decode_predictionskeras.applications.VGG19