Skip to content

Keras - Applications

The tensorflow.keras.applications module provides convenient access to deep learning models with pre-trained weights. These models, typically trained on large datasets like ImageNet, are invaluable for various tasks:

  • Prediction: Use the models directly to classify new images.
  • Feature Extraction: Use the convolutional base of a model (without the final classification layers) to extract meaningful features from images, which can then be used as input for another model.
  • Transfer Learning / Fine-tuning: Adapt a pre-trained model to a new, related task with a smaller dataset. This involves unfreezing some of the later layers and retraining them on your specific data, leveraging the learned features from the original training.

A trained model consists of two parts: the model architecture (layers and their connections) and the model weights (parameters learned during training). The weights files are typically large and are downloaded automatically the first time you instantiate a model with weights='imagenet'.

Some popular pre-trained models available in tf.keras.applications include:

  • VGG16, VGG19
  • ResNet50, ResNet101, ResNet152 (and ResNetRS variants)
  • InceptionV3
  • InceptionResNetV2
  • MobileNet, MobileNetV2, MobileNetV3Small, MobileNetV3Large
  • DenseNet121, DenseNet169, DenseNet201
  • NASNetLarge, NASNetMobile
  • EfficientNetB0-B7, EfficientNetV2B0-B3, EfficientNetV2S, M, L
  • Xception

Loading these models is straightforward using their respective functions within tensorflow.keras.applications. The weights='imagenet' argument automatically fetches the weights learned from the ImageNet dataset.

import tensorflow as tf
# Example: Load VGG16 with ImageNet weights
# The weights file will be downloaded on first use
vgg_model = tf.keras.applications.VGG16(weights='imagenet')
# Example: Load MobileNetV2 with ImageNet weights
mobilenet_model = tf.keras.applications.MobileNetV2(weights='imagenet')
# Example: Load ResNet50
resnet_model = tf.keras.applications.ResNet50(weights='imagenet')
# Example: Load InceptionV3
inception_model = tf.keras.applications.InceptionV3(weights='imagenet')

Common arguments when loading models:

  • weights: Source of weights (‘imagenet’ or None, or path to a weights file). None initializes weights randomly.
  • include_top: Whether to include the final fully-connected classification layer. Set to False for feature extraction or transfer learning where you’ll add your own classifier.
  • input_shape: Optional shape tuple (e.g., (224, 224, 3)) if include_top is False. If not specified, the model can handle inputs of arbitrary sizes.

Once loaded, these models can be used directly for tasks like prediction or serve as a base for more complex applications, as explored in later chapters.