Classify images with the pretrained ImageNet head.
Image classification
Keras · Class
ResNet50 builds the 50-layer ResNet image model as a Keras model. It supports ImageNet or custom weights, optional classification layers, global pooling, and configurable input settings.
ResNet50 constructs the 50-layer ResNet architecture as a Keras model. It can load ImageNet weights, optionally remove the classification head, apply global pooling to convolutional features, and use a custom input tensor or shape.
keras.applications.ResNet50(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation="softmax", name="resnet50")| Name | Type | Description |
|---|---|---|
include_top | boolean | Whether to include the fully connected classification layer. |
weights | string or None | Weight initialization: None, "imagenet", or a compatible weights-file path. |
input_tensor | tensor or None | Optional tensor to use as the model input. |
input_shape | tuple or None | Optional input shape for the model. |
pooling | string or None | Optional global pooling mode when the top is omitted; typically "avg" or "max". |
classes | integer | Number of output classes when the top is included. |
classifier_activation | string, callable, or None | Activation for the top classifier, such as "softmax"; None returns logits. |
name | string | Model name. |
A Keras model instance.
import keras
import numpy as np
model = keras.applications.ResNet50(
weights="imagenet",
include_top=False,
pooling="avg",
)
images = np.random.random((2, 224, 224, 3)).astype("float32")
images = keras.applications.resnet.preprocess_input(images)
features = model.predict(images, verbose=0)
print(features.shape)Builds an ImageNet-initialized ResNet50 without its classifier and produces one pooled feature vector per image.
Image classification
Transfer learning
Feature extraction
ResNet50 instantiates the ResNet50 architecture and returns a Keras model. By default it includes the top classifier, uses ImageNet weights, and produces 1,000-class predictions.
Follow-up: How would you adapt it for transfer learning? Use include_top=False, choose an appropriate input shape, and add a task-specific head.
Set include_top=False to omit the fully connected classifier. The resulting convolutional network can be used for feature extraction or as a backbone.
Follow-up: How can spatial features become a fixed-size vector? Set pooling to "avg" or "max" after removing the top.
The weights argument accepts None for random initialization, "imagenet" for pretrained ImageNet weights, or a path to compatible weights. When using pretrained ResNet weights, preprocess inputs with keras.applications.resnet.preprocess_input.
Follow-up: What must be true when supplying custom weights? The weights must be compatible with the selected architecture and model configuration.
keras.applications.resnet.preprocess_inputkeras.applications.resnet.decode_predictionskeras.applications.ResNet101keras.applications.ResNet152