- Stream COCO from the
detection-datasets/cocorepository - Build a PyTorch
DataLoaderthat works with the streamingIterableDataset - Fine-tune a Faster R-CNN model for 100 training steps
- Run inference and visualize predictions with bounding box overlays
Prerequisites
Install the required packages (if not already present):Load COCO with streaming
Withstreaming=True, no data is downloaded upfront. The dataset returns an
IterableDataset that fetches examples on demand.
Preprocess for detection
Faster R-CNN expects:- Images as
float32tensors in[0, 1]range - Targets as a list of dicts with
boxes(xyxy format) andlabels
[x, y, width, height] format, so we convert to
[x1, y1, x2, y2].
Build a streaming DataLoader
SinceIterableDataset 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.
Train with Faster R-CNN
We usefasterrcnn_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.
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.
Found an error on this page? Report an issue.

