0
0
TensorFlowml~5 mins

Pre-trained models (VGG, ResNet, MobileNet) in TensorFlow

Choose your learning style9 modes available
Introduction
Pre-trained models help us use powerful AI models that are already trained on large image sets. This saves time and effort when solving new image tasks.
You want to recognize objects in photos without training a model from scratch.
You have limited data but want good image classification results.
You want to build an app that detects animals, cars, or everyday objects quickly.
You want to learn how deep learning models work by using ready-made examples.
You want to improve your model by starting from a strong base instead of random weights.
Syntax
TensorFlow
from tensorflow.keras.applications import VGG16, ResNet50, MobileNet

# Load a pre-trained model
model = VGG16(weights='imagenet', include_top=True)

# Use the model to predict on new images
predictions = model.predict(image_batch)
weights='imagenet' loads the model trained on the ImageNet dataset with 1000 classes.
include_top=True means the model includes the final classification layers.
Examples
Load VGG16 model pre-trained on ImageNet with default top layers.
TensorFlow
from tensorflow.keras.applications import VGG16
model = VGG16(weights='imagenet')
Load ResNet50 without the final classification layers to use as a feature extractor.
TensorFlow
from tensorflow.keras.applications import ResNet50
model = ResNet50(weights='imagenet', include_top=False)
Load MobileNet, a lightweight model good for mobile or low-power devices.
TensorFlow
from tensorflow.keras.applications import MobileNet
model = MobileNet(weights='imagenet')
Sample Model
This code loads the VGG16 model pre-trained on ImageNet, downloads an elephant image, preprocesses it, and predicts the top 3 classes with their probabilities.
TensorFlow
import numpy as np
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import get_file

# Load VGG16 pre-trained model
model = VGG16(weights='imagenet')

# Load and prepare an example image (elephant.jpg from Keras utils)
img_path = get_file('elephant.jpg', origin='https://storage.googleapis.com/download.tensorflow.org/example_images/elephant.jpg')
img = image.load_img(img_path, target_size=(224, 224))

# Convert image to array
x = image.img_to_array(img)

# Add batch dimension
x = np.expand_dims(x, axis=0)

# Preprocess input for VGG16
x = preprocess_input(x)

# Predict
preds = model.predict(x)

# Decode predictions to class names
print('Predicted:', decode_predictions(preds, top=3)[0])
OutputSuccess
Important Notes
Pre-trained models expect input images of specific size and preprocessing steps (like normalization).
Using include_top=False allows you to add your own classification layers for custom tasks.
MobileNet is smaller and faster but may be less accurate than VGG or ResNet.
Summary
Pre-trained models save time by using AI models already trained on big image datasets.
VGG, ResNet, and MobileNet are popular models with different sizes and speeds.
You can use these models directly for predictions or as a base for your own tasks.