ImageNet Dataset
The ImageNet project is a large visual database for visual object recognition software research. The idea for this project was conceived over 15 years ago by AI researcher Fei-Fei Li. The ImageNet team presented their dataset for the first time in 2009.
Keras comes bundled with many pre-trained classification models. As of Keras version 2.11, there are 19 different pre-trained models available, where some versions contain many variants as well. The list of models can be found here. Here we will use the following pre-trained models to make predictions on several sample test images.
- VGG16
- ResNet50
- InceptionV3
AI Image Recognition is the process of using artificial intelligence to identify and categorize objects within an image, a task that, while intuitive for humans, is complex for machines due to the significant processing power required.
Here's a simple Jupyter Notebook exercise for students to perform image classification using a pre-trained model on the ImageNet dataset. This exercise will guide them through loading a pre-trained model, making predictions, and visualizing the results.
Exercise: Image Classification with ImageNet
Step 1: Setup
First, ensure you have the necessary libraries installed. You can install them using pip if you haven't already:
%pip install torch torchvision matplotlib
Step 2: Import Libraries
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image
import matplotlib.pyplot as plt
import json
Step 3: Load Pre-trained Model
Load a pre-trained model (e.g., ResNet-18) and set it to evaluation mode:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
model = models.resnet18(pretrained=True)
model.eval()
Step 4: Load and Preprocess Image
Load an image and apply the necessary transformations:
def preprocess_image(image_path):
input_image = Image.open(image_path)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0)
return input_batch
import urllib.request
# Download a sample image
image_url = 'https://erasmus.tsp.edu.rs/wp-content/uploads/2024/12/rcd1024.jpg' # Replace with a valid image URL
image_path = 'rcd1024.jpg'
urllib.request.urlretrieve(image_url, image_path)
input_batch = preprocess_image(image_path)
Step 5: Make Predictions
Pass the preprocessed image through the model to get predictions:
with torch.no_grad():
output = model(input_batch)
Step 6: Decode Predictions
Download the ImageNet class labels and decode the predictions:
import urllib.request
# Download the labels file
labels_url = 'https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json'
labels_path = 'imagenet-simple-labels.json'
urllib.request.urlretrieve(labels_url, labels_path)
# Load the labels
with open(labels_path) as f:
labels = json.load(f)
# Get the predicted label
_, predicted_idx = torch.max(output, 1)
predicted_label = labels[predicted_idx.item()]
print(f'Predicted label: {predicted_label}')
Predicted label: power drill
Step 7: Visualize the Image and Prediction
Display the image along with the predicted label:
def show_image(image_path, label):
image = Image.open(image_path)
plt.imshow(image)
plt.title(f'Predicted: {label}')
plt.axis('off')
plt.show()
show_image(image_path, predicted_label)
Instructions for Students
1. Follow the steps in the notebook to load and preprocess an image.
2. Use the pre-trained ResNet-18 model to make predictions.
3. Decode the predictions and display the image with the predicted label.
4. Experiment with different images and observe the model's performance.
Completion requirements:
- Make a submission