Faster R-CNN
object detection
machine learning
deep learning
computer vision

Trying to use Faster-rcnn models

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Faster R-CNN is a two-stage object detector: it first proposes regions that may contain objects, then classifies and refines those regions. Many people struggle with it not because the model is conceptually mysterious, but because object detection APIs return boxes, scores, and labels in a format that is different from ordinary image classification.

What a Faster R-CNN Model Actually Produces

If you load a pretrained Faster R-CNN model, the output is not a single class label for the whole image. Instead, you get a collection of detections, usually including:

  • 'boxes, which are bounding box coordinates'
  • 'labels, which are integer class ids'
  • 'scores, which are confidence values'

That means you need post-processing after inference. You usually apply a score threshold, map label ids to class names, and draw or save the boxes yourself. The model gives detection results, not a ready-made visualization.

Running Inference with Torchvision

For practical use, a pretrained torchvision detector is one of the simplest entry points. The example below loads a model, preprocesses one image, and prints detections above a confidence threshold.

python
1from PIL import Image
2import torch
3from torchvision.models.detection import (
4    FasterRCNN_ResNet50_FPN_V2_Weights,
5    fasterrcnn_resnet50_fpn_v2,
6)
7
8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
10weights = FasterRCNN_ResNet50_FPN_V2_Weights.DEFAULT
11model = fasterrcnn_resnet50_fpn_v2(weights=weights).to(device)
12model.eval()
13
14image = Image.open("street.jpg").convert("RGB")
15tensor = weights.transforms()(image).to(device)
16
17with torch.no_grad():
18    prediction = model([tensor])[0]
19
20categories = weights.meta["categories"]
21
22for box, label, score in zip(
23    prediction["boxes"],
24    prediction["labels"],
25    prediction["scores"],
26):
27    if score.item() < 0.5:
28        continue
29
30    name = categories[label.item()]
31    coords = [round(x, 1) for x in box.tolist()]
32    print(name, round(score.item(), 3), coords)

This code is runnable as long as you have torch, torchvision, and Pillow installed, plus an image file called street.jpg. The key detail is that the model expects a list of images, even if you only pass one.

Understanding the Two-Stage Pipeline

Faster R-CNN combines two connected parts:

  1. A backbone extracts feature maps from the image.
  2. A region proposal network suggests candidate object locations.
  3. A detection head classifies those proposals and adjusts box coordinates.

This two-stage design is why Faster R-CNN is often more accurate than lightweight one-stage detectors, especially on harder datasets. The tradeoff is speed. If you need real-time processing on weaker hardware, a faster one-stage model may be more practical.

Fine-Tuning Instead of Training from Scratch

Most projects should not train Faster R-CNN from scratch. A pretrained model already knows generic visual features and object shapes, so fine-tuning usually converges faster and needs less labeled data.

A typical workflow is:

  1. Start with pretrained weights.
  2. Replace the classification head with the number of classes in your dataset.
  3. Train on your annotated images.
  4. Validate box quality using metrics such as mean average precision.

Training also requires properly formatted targets. For each image, the dataset must return tensors for boxes and labels. If the boxes are malformed or not aligned with the image size, training will fail or silently learn nonsense.

Common Pitfalls

One frequent mistake is using a classifier mindset with a detector. Faster R-CNN does not answer "what is this image?" It answers "what objects are present, where are they, and how confident is the model?"

Another mistake is forgetting the model's expected input format. Detection models usually want tensors in channel-first form and are often called with a list, not a single tensor by itself.

Class labels also cause confusion. Pretrained weights are often trained on COCO or another benchmark dataset, so label ids correspond to that dataset's categories. If you fine-tune on your own data, you must update the head and the label mapping.

Performance can also disappoint if image sizes are huge or if you run on CPU. Faster R-CNN is heavier than many beginner examples suggest, so inference time can be substantial without GPU acceleration.

Summary

  • Faster R-CNN returns multiple detections, not one image-level label.
  • The main outputs are boxes, labels, and confidence scores.
  • Pretrained torchvision models are a practical way to start using Faster R-CNN.
  • Fine-tuning pretrained weights is usually better than training from scratch.
  • Most usage problems come from incorrect input formatting, label handling, or missing post-processing.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.