← Blog
A curved strip of film with three abstract frames and colored tabs.
Published on
2026-11-26

Stream WebDataset shards from object storage and verify image labels

Imagine a training run where every image opens, both class counts look right and two images have each other’s labels. That happened in our WebDataset test after we deliberately swapped two labels. Counting samples would have cleared the dataset for training.

WebDataset groups the files for one training example inside a tar archive. Put a set of those archives, or shards, in object storage and a worker can read many examples from a single object request. Here’s how to build a shard, stream it and check that the image you received belongs to the label you intend to train on.

Give each sample a stable key

Suppose you are training a classifier to distinguish vertical stripes (class 0) from horizontal stripes (class 1). Assign each image a key and give its components that same basename. Here is the beginning of our first shard.

train-000.tar
  sample-000.png
  sample-000.cls
  sample-001.png
  sample-001.cls

Inside the tar, write all components of one sample consecutively. The streaming reader groups adjacent files by their common key. If you write every PNG first and every label afterward, the reader can encounter an incomplete sample as soon as the key changes. Keep each pair in the same shard and use keys unique across the dataset so your checks can identify duplicates across shards.

For an existing image collection, let records yield the key, a Python Path to the PNG and the class ID from your annotations. Pass the original encoded image bytes to the writer. Repeat for each shard.

import webdataset as wds

with wds.TarWriter("train-000.tar") as shard:
    for key, image_path, class_id in records:
        shard.write({
            "__key__": key,
            "png": image_path.read_bytes(),
            "cls": str(class_id).encode("ascii"),
        })

Keep a manifest from the source annotations that records the expected image hash and label under each key. Deriving it by reading the finished shard would also copy any labeling mistake made during packaging. The WebDataset project documentation describes the archive convention and Python interface.

Read private shards in a repeatable order

Upload each tar as its own object. For the first pass, supply urls as an explicit list of read URLs in the desired order. We used one short-lived signed GET URL per shard. Set the lifetime to cover when a worker will actually open it, including queueing time and retries. The S3 presigned URL guide explains how to grant temporary access to a private object.

Load the source manifest as a dictionary named manifest. This check runs in a single process, with no shard shuffling or local file cache. It stops on an unexpected key, repeated key, incorrect PNG or incorrect label, then checks whether any expected keys were missing.

import hashlib
import webdataset as wds

samples = wds.WebDataset(urls, shardshuffle=False, cache_size=0)
seen = set()
for key, png, raw_label in samples.to_tuple("__key__", "png", "cls"):
    expected = manifest[key]
    if key in seen:
        raise ValueError(f"Duplicate key: {key}")
    if hashlib.sha256(png).hexdigest() != expected["sha256"]:
        raise ValueError(f"Image mismatch: {key}")
    if int(raw_label) != expected["label"]:
        raise ValueError(f"Label mismatch: {key}")
    seen.add(key)

if seen != set(manifest):
    raise ValueError("Incomplete dataset")

What two successful GETs can hide

We used WebDataset 1.0.2 with a CPU build of PyTorch 2.9.1 and a local S3 emulator. We wrote twelve distinct 32-by-32 PNGs and their class IDs into two shards, six pairs per shard. The reader made one successful GET per shard and decoded all twelve PNGs. Every key, image hash and label matched the source manifest.

For the second pass we swapped the labels for sample-008 and sample-009 in a copy of the second shard. The reader again saw twelve unique keys, six samples in each class and twelve decodable images with the original hashes. Checking the labels by key flagged precisely those two samples. If a dataset fails this check, rebuild the affected shard from the source annotations and verify it before training.

Choose shard sizes using the training job

A shard is a unit of transfer and a unit you can assign to a worker. Larger shards spread request overhead across more samples. Smaller ones give workers more units to share and reduce the amount tied up in one failed transfer. Pick a candidate size using the encoded bytes per sample, number of workers and cost of retrying. Keep the key and label checks in a single-reader validation pass before introducing shuffling, batching or multiple workers. The PyTorch DataLoader guide covers those settings.

To evaluate Object Storage with Hivenet, upload representative shards and repeat the checks from a training worker using its intended access permissions. Then measure time to the first batch, samples per second, retries and object requests over a typical epoch. The local test verified pairing and request behavior for two small shards. Throughput and transfer costs need measurements on your own storage and compute path.

Your next workload belongs on Hivenet.

Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.