Skip to content

Keras - Pre-Trained Models

This chapter provides a brief overview of some commonly used pre-trained models available in tf.keras.applications. These models have been trained on the large ImageNet dataset (1.4 million labeled images, 1000 classes) and can be readily used for various computer vision tasks.

Remember the key arguments when loading these models:

  • weights: Typically 'imagenet' to load pre-trained weights, or None for random initialization.
  • include_top: True to include the final classification layer (for direct ImageNet classification), False to exclude it (for feature extraction or transfer learning).
  • input_shape: Optional tuple (e.g., (224, 224, 3)) specifying the expected input size, required if include_top=False and you need a fixed size.
  • classes: Number of output classes (only relevant if include_top=True and weights=None).

Each model also has a corresponding preprocess_input function within its module (e.g., tf.keras.applications.vgg16.preprocess_input) that should be used to prepare images before feeding them to the model.

Developed by the Visual Geometry Group at Oxford. Characterized by its simplicity, using only 3x3 convolutional filters stacked sequentially, followed by max-pooling layers. VGG19 is a deeper version of VGG16.

  • Default Input Size: 224x224
  • Strengths: Simple, uniform architecture. Good feature extractor.
  • Weaknesses: Very large number of parameters, computationally expensive.
import tensorflow as tf
# Load VGG16
vgg16_model = tf.keras.applications.VGG16(weights='imagenet', include_top=True)
# Load VGG16 for feature extraction
vgg16_base = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

ResNet (e.g., ResNet50, ResNet101, ResNet152V2)

Section titled “ResNet (e.g., ResNet50, ResNet101, ResNet152V2)”

Residual Networks introduced the concept of ‘skip connections’ or ‘residual blocks’. These allow gradients to flow more easily through deeper networks, enabling the training of models with hundreds or even thousands of layers without performance degradation.

  • Default Input Size: 224x224
  • Strengths: Can train very deep networks, strong performance, widely used baseline.
  • Weaknesses: Can still be computationally intensive depending on the depth.
import tensorflow as tf
# Load ResNet50V2 (V2 generally preferred over V1)
resnet_model = tf.keras.applications.ResNet50V2(weights='imagenet', include_top=True)

Designed specifically for mobile and embedded vision applications where computational resources are limited. They use depthwise separable convolutions to significantly reduce the number of parameters and computations while maintaining good accuracy.

  • Default Input Size: 224x224
  • Strengths: Lightweight, fast, efficient, good for mobile devices.
  • Weaknesses: May have slightly lower accuracy than larger models on complex tasks.
  • Parameter alpha: Controls the width of the network (number of filters). alpha < 1.0 reduces size/computation, alpha > 1.0 increases them.
import tensorflow as tf
# Load MobileNetV2
mobilenet_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=True)
# Load a smaller version using alpha
smaller_mobilenet = tf.keras.applications.MobileNetV2(weights='imagenet', alpha=0.5, include_top=False)

Inception (e.g., InceptionV3, InceptionResNetV2)

Section titled “Inception (e.g., InceptionV3, InceptionResNetV2)”

The Inception architecture (also known as GoogLeNet) uses ‘Inception modules’ which perform convolutions with different filter sizes (1x1, 3x3, 5x5) in parallel and concatenate their outputs. This allows the network to capture features at various scales efficiently. InceptionResNetV2 combines Inception modules with residual connections.

  • Default Input Size: 299x299
  • Strengths: Good performance with relatively fewer parameters compared to VGG.
  • Weaknesses: Architecture is more complex.
import tensorflow as tf
# Load InceptionV3
inception_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=True)
# Load InceptionResNetV2
inc_resnet_model = tf.keras.applications.InceptionResNetV2(weights='imagenet', include_top=True)

EfficientNet models achieve state-of-the-art accuracy with significantly fewer parameters and computations. They use a technique called ‘compound scaling’ to uniformly scale the network’s depth, width, and resolution based on available resources. EfficientNetV2 represents further improvements.

  • Default Input Size: Varies (e.g., 224x224 for B0, up to 600x600 for B7)
  • Strengths: Excellent accuracy-to-efficiency ratio.
  • Weaknesses: Can still be large/slow at higher scales (B5+).
import tensorflow as tf
# Load EfficientNetB0
efficientnet_b0 = tf.keras.applications.EfficientNetB0(weights='imagenet', include_top=True)
# Load EfficientNetV2S
efficientnet_v2s = tf.keras.applications.EfficientNetV2S(weights='imagenet', include_top=True)

The best model depends on your specific needs:

  • High Accuracy Needed (Resources Available): ResNet, InceptionResNetV2, EfficientNet (higher B numbers).
  • Good Balance (Accuracy/Efficiency): ResNet50, InceptionV3, EfficientNet (lower B numbers).
  • Resource Constrained (Mobile/Edge): MobileNetV2/V3, EfficientNetB0.
  • Feature Extraction Baseline: VGG16, ResNet50 are often good starting points.

Using these pre-trained models, especially for transfer learning or feature extraction, can save significant training time and computational cost, allowing you to achieve high performance even with limited custom data.