Skip to main content
Open In Colab The COCO dataset is a cornerstone benchmark for object detection, but at ~20 GB it takes significant time and disk space to download. Hugging Face datasets streaming lets you train on COCO without downloading the full dataset, images are fetched on-the-fly as your training loop requests them. In this tutorial you will:
  1. Stream COCO from the detection-datasets/coco repository
  2. Build a PyTorch DataLoader that works with the streaming IterableDataset
  3. Fine-tune a Faster R-CNN model for 100 training steps
  4. Run inference and visualize predictions with bounding box overlays

Prerequisites

Install the required packages (if not already present):

Load COCO with streaming

With streaming=True, no data is downloaded upfront. The dataset returns an IterableDataset that fetches examples on demand.
Let’s peek at the schema by grabbing one example.

Preprocess for detection

Faster R-CNN expects:
  • Images as float32 tensors in [0, 1] range
  • Targets as a list of dicts with boxes (xyxy format) and labels
COCO bounding boxes are in [x, y, width, height] format, so we convert to [x1, y1, x2, y2].

Build a streaming DataLoader

Since IterableDataset from HF datasets inherits from torch.utils.data.IterableDataset, we can pass it directly to a DataLoader. We use a custom collate function because detection targets have variable-length box lists.

Visualize a batch

Let’s draw bounding boxes on a batch of images to verify the preprocessing.
Output from cell 6

Train with Faster R-CNN

We use fasterrcnn_resnet50_fpn_v2 pretrained on COCO and fine-tune for 100 steps as a demonstration. In a real scenario you would train for many more steps and evaluate on the validation split.

Run inference

Switch to eval mode and visualize predictions on a few streamed images. We keep predictions with confidence > 0.5.
Output from cell 9

Next steps

  • Scale up training: increase NUM_STEPS, add a learning rate scheduler, and evaluate on the full validation split with mAP metrics.
  • Try YOLOv11: explore our YOLOv11 from-scratch notebooks for a different detection architecture built entirely in PyTorch for a different detection architecture built entirely in PyTorch.
  • Explore HF streaming: the Hugging Face datasets streaming guide covers advanced features like multi-worker loading, shuffling strategies, and checkpoint resumption with StatefulDataLoader.