Skip to content

Keras - Real Time Prediction using ResNet Model

This chapter demonstrates how to use a pre-trained Convolutional Neural Network (CNN) model, specifically ResNet50, for image classification. ResNet (Residual Network) is a powerful architecture known for its ability to train very deep networks effectively. We’ll use the ResNet50 model pre-trained on the large ImageNet dataset, available through tf.keras.applications.

Workflow:

  1. Load the pre-trained ResNet50 model.
  2. Load an input image.
  3. Preprocess the image to match the format ResNet50 expects.
  4. Use the model to predict the class probabilities for the image.
  5. Decode the predictions into human-readable labels.
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from tensorflow.keras.utils import load_img, img_to_array # Updated image utils
import numpy as np
# import matplotlib.pyplot as plt # Optional for displaying the image

Step 2: Load the Pre-trained ResNet50 Model

Section titled “Step 2: Load the Pre-trained ResNet50 Model”

Instantiate the ResNet50 model, specifying weights='imagenet' to download and load the pre-trained weights. include_top=True means we want the final classification layer.

# Load the ResNet50 model pre-trained on ImageNet
# Weights will be downloaded automatically on first use
model = ResNet50(weights='imagenet', include_top=True)

You can inspect the model structure:

# model.summary() # Optional: View the layers

Step 3: Load and Preprocess the Input Image

Section titled “Step 3: Load and Preprocess the Input Image”

Pre-trained models expect input images in a specific format (size, pixel value range, channel order). ResNet50 typically expects 224x224 pixel images.

# Specify the path to your image file
# Make sure you have an image file (e.g., 'banana.jpg') in your working directory
# or provide the full path.
img_path = 'banana.jpg' # CHANGE THIS TO YOUR IMAGE FILE
# Load the image, resizing it to the target size expected by ResNet50
target_size = (224, 224)
img = load_img(img_path, target_size=target_size)
# --- Optional: Display the loaded image ---
# plt.imshow(img)
# plt.axis('off')
# plt.show()
# -----------------------------------------
# Convert the PIL image object to a NumPy array
img_array = img_to_array(img)
# Expand dimensions to create a batch of 1 image
# Shape changes from (height, width, channels) to (1, height, width, channels)
img_batch = np.expand_dims(img_array, axis=0)
# Preprocess the image data for ResNet50
# This typically involves mean subtraction and scaling specific to the model
img_preprocessed = preprocess_input(img_batch)
print(f"Image loaded and preprocessed. Shape: {img_preprocessed.shape}")

The preprocess_input function handles the specific normalization required by the ResNet50 model (e.g., converting RGB to BGR, zero-centering with respect to ImageNet dataset means).

Feed the preprocessed image batch into the model’s predict method.

predictions = model.predict(img_preprocessed)

The predictions variable now holds a NumPy array containing the probability scores for each of the 1000 ImageNet classes. The shape will be (1, 1000) for a single image input.

The raw probabilities aren’t very interpretable. Use the decode_predictions utility to convert these probabilities into a list of (class_id, class_name, probability) tuples, typically showing the top N predictions.

# Decode the predictions into readable class names
# 'top=5' shows the top 5 most likely classes
decoded = decode_predictions(predictions, top=5)[0] # Index [0] because we have a batch of 1
print("\nTop 5 Predictions:")
for i, (imagenet_id, label, score) in enumerate(decoded):
print(f"{i + 1}: {label} ({score:.2f})")

The output will depend on your input image. For a typical banana image, you might see:

Image loaded and preprocessed. Shape: (1, 224, 224, 3)
Top 5 Predictions:
1: banana (0.99)
2: acorn_squash (0.00)
3: cucumber (0.00)
4: pineapple (0.00)
5: butternut_squash (0.00)

This shows the model is highly confident that the image contains a banana. This process illustrates the power of pre-trained models for quick and effective image classification without needing to train a model from scratch.