TensorFlow Pre-trained Models

Pre-trained models are neural networks that were already trained on massive datasets by research teams with enormous computing resources. TensorFlow provides dozens of these models through the tf.keras.applications module, ready to use immediately. Each model arrives with its learned weights, its architecture, and often its preprocessing function. You can use them as feature extractors, as starting points for fine-tuning, or as complete classifiers for the 1,000 ImageNet categories they were trained on.

The Library Analogy

Imagine a university library with millions of carefully organized books. Researchers spent decades building and cataloguing the collection. You can walk in and use the entire collection on day one rather than spending decades building your own. Pre-trained models are this library — decades of computer vision research encoded into weights you can download in minutes.

Available Pre-trained Models in tf.keras.applications

Model             Size      Top-1 Accuracy  Parameters  Best For
──────────────────────────────────────────────────────────────────────────
MobileNetV2       14 MB     71.8%           3.4M        Mobile, embedded
MobileNetV3Small  10 MB     67.4%           2.5M        Ultra-compact devices
EfficientNetB0    29 MB     77.1%           5.3M        General purpose
EfficientNetB4    75 MB     82.6%           19M         High accuracy
ResNet50          98 MB     74.9%           25M         Research baseline
ResNet152         232 MB    76.6%           60M         Maximum ResNet accuracy
VGG16             528 MB    71.3%           138M        Simple architecture
InceptionV3       92 MB     77.9%           23M         Multi-scale features
Xception          88 MB     79.0%           22M         ImageNet leader
NASNetLarge       343 MB    82.5%           88M         Top accuracy
──────────────────────────────────────────────────────────────────────────
Top-1 Accuracy = % of ImageNet test images predicted correctly (top prediction)

Loading a Pre-trained Model

import tensorflow as tf

# Load MobileNetV2 with ImageNet weights
# include_top=True: includes the final 1000-class classification layer
base_model = tf.keras.applications.MobileNetV2(
    input_shape=(224, 224, 3),
    include_top=True,
    weights='imagenet'
)

# Predict on a new image (for the 1000 ImageNet classes)
import numpy as np
img = tf.keras.utils.load_img('cat.jpg', target_size=(224, 224))
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)

# Preprocess (each model has its own normalization range)
img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)

predictions = base_model.predict(img_array)
decoded = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=5)

for class_id, class_name, confidence in decoded[0]:
    print(f"{class_name:20s}: {confidence:.2%}")
# tabby_cat          : 52.3%
# tiger_cat          : 23.1%
# Egyptian_cat       : 11.4%
# Persian_cat        :  4.2%
# lynx               :  2.1%

Preprocessing Functions

Each model was trained with a specific pixel normalization scheme. Use the matching preprocessing function or your model will produce nonsense predictions.

Model Family         Preprocessing Function                    Output Range
────────────────────────────────────────────────────────────────────────────
MobileNetV2/V3       mobilenet_v2.preprocess_input()          [-1, 1]
EfficientNet B0-B7   efficientnet.preprocess_input()          [0, 255] (no change!)
ResNet50/101/152     resnet.preprocess_input()                BGR, mean-subtracted
VGG16/VGG19          vgg16.preprocess_input()                 BGR, mean-subtracted
InceptionV3          inception_v3.preprocess_input()          [-1, 1]
Xception             xception.preprocess_input()              [-1, 1]
────────────────────────────────────────────────────────────────────────────
# Always match the preprocessing to the model
img_preprocessed = tf.keras.applications.efficientnet.preprocess_input(img_array)
img_preprocessed = tf.keras.applications.resnet.preprocess_input(img_array)
img_preprocessed = tf.keras.applications.inception_v3.preprocess_input(img_array)

Loading Without the Top (For Transfer Learning)

# include_top=False removes the final 1000-class Dense layer
# This is the standard setup for transfer learning to custom classes

base = tf.keras.applications.EfficientNetB0(
    input_shape=(224, 224, 3),
    include_top=False,      # ← Remove the ImageNet classifier
    weights='imagenet'      # ← Keep all the learned features
)

print("Number of layers:", len(base.layers))
print("Output shape:", base.output_shape)   # (None, 7, 7, 1280)

Feature Extraction Without Training

# Use a frozen pre-trained model to extract features from your images
base = tf.keras.applications.MobileNetV2(
    input_shape=(224, 224, 3),
    include_top=False,
    weights='imagenet'
)
base.trainable = False

# Extract features from your images
def extract_features(image_batch):
    preprocessed = tf.keras.applications.mobilenet_v2.preprocess_input(image_batch)
    return base(preprocessed, training=False)

# (batch, 7, 7, 1280) per image
features = extract_features(your_image_batch)

Inspecting Model Architecture

base = tf.keras.applications.ResNet50(include_top=False, weights='imagenet')

# Print layer names and output shapes
for i, layer in enumerate(base.layers[:10]):
    print(f"Layer {i:3d}: {layer.name:40s} → {layer.output_shape}")

# Visualize architecture
tf.keras.utils.plot_model(base, to_file='resnet50.png',
                          show_shapes=True, expand_nested=True)

Model Size and Inference Speed Comparison

Benchmark (single image, CPU):
  MobileNetV2:    ~25ms    — fast enough for real-time mobile
  EfficientNetB0: ~35ms    — good accuracy/speed balance
  ResNet50:       ~80ms    — slower but widely supported
  VGG16:          ~150ms   — very slow, memory-heavy

Benchmark (batch of 32, GPU):
  MobileNetV2:    ~8ms     — ideal for server-side real-time
  EfficientNetB4: ~25ms    — best accuracy per second
  ResNet152:      ~60ms    — comprehensive features

Which Pre-trained Model to Choose

Goal                              Recommended Model
────────────────────────────────────────────────────────────────
Mobile app deployment             MobileNetV2 or MobileNetV3
Best accuracy, any size OK        EfficientNetB4 or EfficientNetB7
Research reproducibility          ResNet50 (universal baseline)
Quick prototype                   MobileNetV2 (fast + lightweight)
Multi-scale feature extraction    InceptionV3
Edge device (IoT, microcontroller) MobileNetV3Small or TFLite models
────────────────────────────────────────────────────────────────

Pre-trained models are the biggest productivity multiplier in computer vision. Instead of weeks of training, you achieve state-of-the-art feature extraction in a single download. The next topic shows exactly how to take these models and fine-tune their layers for your specific dataset and task.

Leave a Comment

Your email address will not be published. Required fields are marked *