0
0
PyTorchml~5 mins

Faster R-CNN usage in PyTorch

Choose your learning style9 modes available
Introduction

Faster R-CNN helps find and label objects in pictures quickly and accurately.

You want to detect cars and people in street photos.
You need to find animals in wildlife images.
You want to count items on a store shelf from photos.
You want to spot defects in product images on a factory line.
Syntax
PyTorch
import torchvision
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()

# To predict on an image:
# 1. Load and transform the image
# 2. Pass it to the model
# 3. Get predictions with boxes, labels, and scores

Use pretrained=True to get a model already trained on common objects.

Call model.eval() to set the model for prediction (not training).

Examples
Load the Faster R-CNN model with pretrained weights and set it to evaluation mode.
PyTorch
import torchvision
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
Load an image and convert it to a tensor for the model.
PyTorch
from PIL import Image
from torchvision import transforms

image = Image.open('image.jpg')
transform = transforms.Compose([
    transforms.ToTensor()
])
image_tensor = transform(image)
Run the model on the image tensor without tracking gradients to get predictions.
PyTorch
import torch
with torch.no_grad():
    predictions = model([image_tensor])
Sample Model

This code loads a pretrained Faster R-CNN model, creates a blank image, transforms it, and runs prediction. It prints the keys in the prediction dictionary and some example outputs.

PyTorch
import torch
from PIL import Image
from torchvision import transforms
import torchvision

# Load pretrained Faster R-CNN model
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()

# Load and transform image
image = Image.new('RGB', (300, 300), color='white')  # blank white image
transform = transforms.Compose([
    transforms.ToTensor()
])
image_tensor = transform(image)

# Predict
with torch.no_grad():
    predictions = model([image_tensor])

# Print prediction keys and example outputs
print('Prediction keys:', predictions[0].keys())
print('Boxes shape:', predictions[0]['boxes'].shape)
print('Labels:', predictions[0]['labels'])
print('Scores:', predictions[0]['scores'])
OutputSuccess
Important Notes

Faster R-CNN expects input images as tensors with shape [3, height, width] and values between 0 and 1.

Predictions include bounding boxes, labels (class IDs), and confidence scores.

On images without detectable objects, predictions may be empty (zero boxes).

Summary

Faster R-CNN finds objects in images and gives their locations and labels.

Use pretrained models for quick start and evaluation mode for prediction.

Transform images to tensors before passing to the model.