timm documentation

Data

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.0.30).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Data

This page contains the API reference documentation for the timm.data sub-package, and an overview of the input pipeline it provides: resolving a model’s preprocessing config, building transforms, creating datasets and loaders, Mixup / CutMix, and the NaFlex variable resolution loader.

Usage

A pipeline is built in four steps: resolve_model_data_config reads the model’s preprocessing config, create_transform builds the train or eval transform, create_dataset creates a dataset by name and root, and create_loader builds the transform and a data loader for a dataset.

import os
import tempfile

import torch
from PIL import Image

import timm
from timm.data import create_dataset, create_loader, create_transform, resolve_model_data_config

model = timm.create_model('resnet50', pretrained=False).eval()

# Preprocessing config from the model's pretrained_cfg, the default pretrained tag's config is attached to the model
# with or without pretrained weights.
data_config = resolve_model_data_config(model)
print(data_config)
# {'input_size': (3, 224, 224), 'interpolation': 'bicubic', 'mean': (0.485, 0.456, 0.406),
#  'std': (0.229, 0.224, 0.225), 'crop_pct': 0.95, 'crop_mode': 'center'}

# Eval transform for a PIL image, resize to 224 / crop_pct, center crop 224, to tensor and normalize.
transform = create_transform(**data_config, is_training=False)
img = Image.new('RGB', (320, 240), color=(200, 120, 40))
with torch.no_grad():
    output = model(transform(img).unsqueeze(0))  # (1, 1000)

# A folder dataset, <root>/<split>/<class>/<image>, two classes w/ four generated images each.
root = tempfile.mkdtemp()
for i, cls in enumerate(('cat', 'dog')):
    os.makedirs(os.path.join(root, 'validation', cls))
    for j in range(4):
        path = os.path.join(root, 'validation', cls, f'{j}.jpg')
        Image.new('RGB', (300, 200), color=(60 * i, 40 * j, 128)).save(path)

dataset = create_dataset('', root=root, split='validation')  # finds the 'validation' sub-folder
print(len(dataset), dataset.reader.class_to_idx)  # 8 {'cat': 0, 'dog': 1}

# The loader creates the transform from the same config and assigns it to the dataset.
loader = create_loader(dataset, **data_config, batch_size=4, use_prefetcher=False, num_workers=0)
with torch.no_grad():
    for images, targets in loader:
        output = model(images)  # images (4, 3, 224, 224), targets (4,)

The quickstart walks through the config to transform steps for inference.

The training script (train.py) exposes these options for each part of the pipeline.

Input config overrides (resolve_input_data_config):

  • --input-size sets the input shape (C, H, W).
  • --img-size is the square shortcut for --input-size, keeping the model’s channel count.
  • --in-chans sets the input channels.
  • --interpolation sets the resize interpolation.
  • --mean sets the normalization mean.
  • --std sets the normalization standard deviation.
  • --crop-pct sets the evaluation crop fraction.

Dataset creation (create_dataset) and target setup:

  • --dataset selects the dataset and reader.
  • --data-dir sets the dataset root.
  • --train-split selects the training split.
  • --val-split selects the validation split.
  • --class-map supplies the class mapping.
  • --input-img-mode sets the image color conversion mode.
  • --input-key selects the image field.
  • --target-key selects the label field or fields.
  • --target-format selects class indices or dense multi-hot targets.

Loader and augmentation options (create_loader) include:

  • --aa selects the auto-augmentation policy.
  • --reprob sets the random erasing probability.
  • --remode selects the random erasing fill mode.
  • --scale sets the random resized crop scale range.
  • --ratio sets the random resized crop aspect ratio range.
  • --hflip sets the horizontal flip probability.
  • --color-jitter sets color jitter strength.
  • --train-interpolation sets the training resize interpolation.
  • --workers sets the number of loader workers.
  • --no-prefetcher disables the prefetcher.

Mixup and CutMix (Mixup):

  • --mixup sets the Mixup alpha.
  • --cutmix sets the CutMix alpha.
  • --cutmix-minmax sets the CutMix box size range.
  • --mixup-prob sets the probability of mixing.
  • --mixup-switch-prob sets the probability of choosing CutMix when both methods are enabled.
  • --mixup-mode selects batch, pair or element-wise mixing.
  • --smoothing sets label smoothing.

See Scripts for examples.

Data config

resolve_data_config returns a dict with the keys below, which match the arguments of create_transform and create_loader. Values set in args (a dict, e.g. vars(args) of parsed command line args) take precedence over the pretrained_cfg argument, which defaults to model.pretrained_cfg. resolve_model_data_config is the same with the model as first argument.

Both read the input size and channels from the pretrained cfg, which describes the source checkpoint, not a model created or changed to use a different img_size or in_chans. resolve_input_data_config, used by the training, validation and inference scripts, resolves against the model’s current input instead: a fixed input size model always gets its current size, explicit args that conflict with the model’s channels or fixed size raise an error, and a mean / std that doesn’t match the channel count is replaced by its average (with a warning).

Keyargs keysDescriptionDefault
input_sizeinput_size, img_size, in_chans(C, H, W) input size. With use_test_size=True the config’s test_input_size is used if it has one(3, 224, 224)
interpolationinterpolationResize interpolation, bilinear, bicubic, …bicubic
mean, stdmean, stdNormalization, a single value is repeated for each channelImageNet mean / std
crop_pctcrop_pctEval crop, the image is resized to size / crop_pct before cropping to size (test_crop_pct with use_test_size=True)0.875
crop_modecrop_modeEval crop mode, center, squash or bordercenter

The eval crop modes, for an output size and scale_size = floor(size / crop_pct):

crop_modeBehaviour
centerResize the shortest edge to scale_size (aspect ratio preserved), center crop to size
squashResize both edges to scale_size (aspect ratio not preserved), center crop to size
borderResize the longest edge to scale_size, center crop and pad with the mean color, the whole image is kept at crop_pct=1.0

Transforms

create_transform returns a torchvision Compose. The eval transform resizes and crops as above, then converts to a tensor and normalizes. The training transform is a random resized crop (scale=(0.08, 1.0), ratio=(3/4, 4/3) by default), horizontal flip (hflip=0.5), color jitter (color_jitter=0.4), the optional auto_augment policy, tensor conversion and normalization, and random erasing.

  • train_crop_mode='rkrc' or 'rkrr' replaces the random resized crop with a random resize that keeps the aspect ratio, followed by a center (rkrc) or random (rkrr) crop or pad.
  • interpolation='random' (the train.py default for training) picks bilinear or bicubic per image.
  • no_aug=True reduces training to a resize and center crop.
auto_augmentAugmentation
rand-m9-mstd0.5-inc1RandAugment via rand_augment_transform
augmix-m5-w4-d2AugMix via augment_and_mix_transform
original, originalr, v0, v0r, 3aAutoAugment policy via auto_augment_transform

RandAugment option tokens:

  • m sets magnitude (default 10).
  • n sets the number of ops per image (default 2).
  • p sets the probability per op (default 0.5).
  • mstd sets magnitude noise std.
  • inc1 selects ops that increase in severity w/ magnitude.
  • t<name> selects the op set (weights, 3a, 3aw).

AugMix option tokens:

  • m sets severity (default 3).
  • w sets width (default 3).
  • d sets depth (default -1, random 1-3).
  • b1 enables blended augmentation.
  • mstd sets magnitude noise std.

AutoAugment accepts mstd, e.g. original-mstd0.5.

Transform behavior:

  • Color jitter is disabled when auto_augment is set, unless the string contains 3a.
  • RandomErasing (re_prob, re_count) runs on the normalized tensor. re_mode fills with zeros (const, the default), a random normal color per region (rand) or random normal pixels (pixel, the train.py default).
  • use_prefetcher=True ends the transform at a uint8 tensor, normalization and random erasing are then done by the loader on the device.
  • create_transform and create_loader default to bilinear interpolation and ImageNet mean / std, not the model’s values. Pass the resolved data config to match the model’s pretraining.

Datasets

create_dataset selects the dataset implementation and reader from a prefix of name:

nameDatasetrootRequires
'' or no prefixImageDataset, images in class sub-folders (searched recursively, the leaf folder name is the class), or a single .tar file of the sameDataset folder or .tar file
torch/<name>torchvision cifar10, cifar100, mnist, kmnist, fashion_mnist, qmnist, inaturalist (inat), places365, imagenet or image_folder (folder)torchvision dataset root
hfds/<name>ImageDataset on a Hugging Face Hub dataset (or local path) via datasets.load_datasetCache dir (optional)datasets
hfids/<name>IterableImageDataset, the Hugging Face dataset streamed, or downloaded and prepared first with download=TrueCache dir (optional)datasets
tfds/<name>IterableImageDataset on TensorFlow Datasets, download=True downloads and buildsTFDS data dirtensorflow, tensorflow-datasets
wds/<name>IterableImageDataset on WebDataset tar shardsShard folder w/ an _info.json (or info.json) describing the splitswebdataset

Dataset options:

  • split selects the dataset split (default validation). For WebDataset it can also be a shard pattern with a sample count, 'imagenet-train-{0000..1023}.tar|1281167'.
  • search_split=True (default) makes folder datasets look for the sub-folder of root named split or, failing that, a synonym of a train (train, training) or eval (val, valid, validation, eval, evaluation) split.
  • add_img_extensions adds to the default image extensions (.png, .jpg and .jpeg).
  • class_map remaps class names to indices with a dict, a .txt (one class per line) or a .pkl file.

Iterable datasets (hfids, tfds, wds) also take:

  • is_training enables shuffling.
  • batch_size rounds each worker’s share of the samples up to whole batches when training.
  • num_samples sets the sample count, required when the reader can’t determine it (hfids without split info).
  • seed sets the random seed.
  • repeats multiplies the samples per epoch, the readers pass over the data that many times.

Call dataset.set_epoch() each epoch to reshuffle.

Other keyword arguments pass through to the reader. For the Hugging Face, TFDS and WebDataset readers:

  • input_key selects the image field (image by default, for wds a jpg, png or webp image).
  • target_key selects the label field (label by default, for wds the .cls file or a .json sidecar field named by target_key). A comma separated value reads one binary field per class.
  • target_format='multihot' reads dense multi-hot vectors instead of lists of class indices for multi-label data.
  • target_transform=MultiLabelTarget(num_classes) encodes targets as float multi-hot tensors. Pass dense=True to MultiLabelTarget for multi-hot fields.

The folder and tar readers yield single class indices. See multi-label classification for details.

Loaders

create_loader creates the transform with create_transform from its own arguments, assigns it to dataset.transform (replacing any existing transform) and returns a DataLoader. Training loaders (is_training=True) shuffle and drop the last incomplete batch.

  • Prefetcher. With use_prefetcher=True (default) the dataset yields uint8 tensors, the batches are moved to device (default cuda, on a side stream for CUDA), converted to img_dtype, normalized and randomly erased there. Set device to the training device, or use_prefetcher=False for a plain DataLoader of normalized tensors.
  • Distributed. distributed=True uses a DistributedSampler for map-style training datasets (or a repeated augmentation sampler w/ num_aug_repeats) and an ordered sampler for eval that pads each process to the same number of samples, which can slightly alter validation results. Iterable datasets shard themselves across processes and workers.
  • Collate. collate_fn replaces the default (fast_collate w/ the prefetcher, default_collate without), e.g. with a FastCollateMixup.
  • Augmentation splits. For AugMix w/ the JSD loss (--aug-splits), wrap the dataset in AugMixDataset(dataset, num_splits=n) and pass num_aug_splits=n. Each batch holds a clean split and n - 1 augmented splits, concatenated along the batch dimension by the prefetcher.
  • Scheduled resolution. input_size_choices (--train-img-sizes) picks the input size per batch, optionally with a batch size per choice (batch_size_choices) and a progressive schedule from the first to the last choice.

Mixup and CutMix

Mixup mixes a batch of normalized images on the device, after the loader. FastCollateMixup takes the same arguments and mixes uint8 images while collating in the loader workers, it requires the prefetcher. Both return dense (batch_size, num_classes) targets with label_smoothing (default 0.1) applied, to be used with a soft target loss such as timm.loss.SoftTargetCrossEntropy, and require an even batch size.

from timm.data import Mixup, FastCollateMixup

# Continuing the example above.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
mixup_args = dict(mixup_alpha=0.8, cutmix_alpha=1.0, label_smoothing=0.1, num_classes=1000)

# Prefetcher enabled (default), mixed in the loader's collate.
loader = create_loader(
    dataset,
    **data_config,
    batch_size=4,
    is_training=True,
    collate_fn=FastCollateMixup(**mixup_args),
    device=device,
)
for images, targets in loader:
    ...  # images normalized on device, targets (4, 1000)

# Prefetcher disabled, mixed in the training loop.
loader = create_loader(dataset, **data_config, batch_size=4, is_training=True, use_prefetcher=False)
mixup_fn = Mixup(**mixup_args)
for images, targets in loader:
    images, targets = mixup_fn(images.to(device), targets.to(device))

Mixup and CutMix controls:

  • mixup_alpha > 0 enables Mixup.
  • cutmix_alpha > 0 enables CutMix.
  • cutmix_minmax enables CutMix with a box size range.
  • switch_prob sets the probability of choosing CutMix when both methods are enabled.
  • prob sets the probability of mixing at all.
  • mode draws the parameters once per batch, per pair of elements or per elem (element).
  • multi_label=True mixes dense multi-label targets, smoothing each label towards 0.5.
  • mixup_enabled = False turns mixing off, e.g. for the final epochs (--mixup-off-epoch). For FastCollateMixup it is read in the worker processes, persistent workers keep the value they started with.

NaFlex variable resolution

create_naflex_loader loads images as variable length sequences of patches, w/o resizing to a fixed square size, for models that take NaFlex patch inputs (e.g. the naflexvit_* models). Each batch is a dict of patches, patch_coord, patch_valid and seq_len, padded to the batch’s sequence length.

  • Training. The dataset is wrapped in a NaFlexMapDatasetWrapper (map-style datasets only), which picks one of train_seq_lens per batch and random resize crops each image to fit that many patches. batch_size is the batch size at the longest sequence length, shorter lengths get proportionally larger batches (rounded down to a multiple of 8) so the tokens per batch stay roughly constant. patch_size_choices (w/ patch_size_choice_probs) also varies the patch size per batch, for models that support it.
  • Eval. Images are resized to fit max_seq_len patches at the given patch_size (required for eval) and padded to max_seq_len.
  • Mixup. Pass a NaFlexMixup as mixup_fn, it mixes within each batch and does not support mode or cutmix_minmax.

The train.py options are:

  • --naflex-loader enables the NaFlex loader.
  • --naflex-train-seq-lens sets the training sequence lengths.
  • --naflex-max-seq-len sets the maximum evaluation sequence length.
  • --naflex-patch-sizes sets the patch sizes for variable patch size training.
  • --naflex-patch-size-probs sets the probabilities for those patch sizes.
  • --naflex-loss-scale selects loss scaling by batch size (none, sqrt or linear).

API

Data config

timm.data.resolve_data_config

< >

( args = Nonepretrained_cfg = Nonemodel = Noneuse_test_size = Falseverbose = False )

timm.data.resolve_model_data_config

< >

( modelargs = Nonepretrained_cfg = Noneuse_test_size = Falseverbose = False )

Parameters

  • model (nn.Module) — the model instance
  • args (dict) — command line arguments / configuration in dict form (overrides pretrained_cfg)
  • pretrained_cfg (dict) — pretrained model config (overrides pretrained_cfg attached to model)
  • use_test_size (bool) — use the test time input resolution (if one exists) instead of default train resolution
  • verbose (bool) — enable extra logging of resolved values

Resolve Model Data Config This is equivalent to resolve_data_config() but with arguments re-ordered to put model first.

timm.data.resolve_input_data_config

< >

( modelargs: typing.Optional[typing.Dict[str, typing.Any]] = Nonepretrained_cfg: typing.Optional[typing.Dict[str, typing.Any]] = Noneuse_test_size: bool = Falseverbose: bool = False )

Resolve preprocessing against the current model’s input constraints.

Fixed dimensions and channels must agree with explicit requests. Flexible models retain train/test preprocessing recommendations unless overridden. Normalization uses the prefetch loaders’ existing channel adaptation policy.

Transforms

timm.data.create_transform

< >

( input_size: typing.Union[int, typing.Tuple[int, int], typing.Tuple[int, int, int]] = 224is_training: bool = Falseno_aug: bool = Falsetrain_crop_mode: typing.Optional[str] = Nonescale: typing.Optional[typing.Tuple[float, float]] = Noneratio: typing.Optional[typing.Tuple[float, float]] = Nonehflip: float = 0.5vflip: float = 0.0color_jitter: typing.Union[float, typing.Tuple[float, ...]] = 0.4color_jitter_prob: typing.Optional[float] = Nonegrayscale_prob: float = 0.0gaussian_blur_prob: float = 0.0auto_augment: typing.Optional[str] = Noneinterpolation: str = 'bilinear'mean: typing.Tuple[float, ...] = (0.485, 0.456, 0.406)std: typing.Tuple[float, ...] = (0.229, 0.224, 0.225)re_prob: float = 0.0re_mode: str = 'const're_count: int = 1re_num_splits: int = 0crop_pct: typing.Optional[float] = Nonecrop_mode: typing.Optional[str] = Nonecrop_border_pixels: typing.Optional[int] = Nonetf_preprocessing: bool = Falseuse_prefetcher: bool = Falsenormalize: bool = Trueseparate: bool = Falsenaflex: bool = Falsepatch_size: typing.Union[int, typing.Tuple[int, int]] = 16max_seq_len: int = 576patchify: bool = Falsepatchify_channels_last: bool = Truepatchify_flatten: bool = True )

Parameters

  • input_size — Target input size (channels, height, width) tuple or size scalar.
  • is_training — Return training (random) transforms.
  • no_aug — Disable augmentation for training (useful for debug).
  • train_crop_mode — Training random crop mode (‘rrc’, ‘rkrc’, ‘rkrr’).
  • scale — Random resize scale range (crop area, < 1.0 => zoom in).
  • ratio — Random aspect ratio range (crop ratio for RRC, ratio adjustment factor for RKR).
  • hflip — Horizontal flip probability.
  • vflip — Vertical flip probability.
  • color_jitter — Random color jitter component factors (brightness, contrast, saturation, hue). Scalar is applied as (scalar,) * 3 (no hue).
  • color_jitter_prob — Apply color jitter with this probability if not None (for SimlCLR-like aug).
  • grayscale_prob — Probability of converting image to grayscale (for SimCLR-like aug).
  • gaussian_blur_prob — Probability of applying gaussian blur (for SimCLR-like aug).
  • auto_augment — Auto augment configuration string (see auto_augment.py).
  • interpolation — Image interpolation mode.
  • mean — Image normalization mean.
  • std — Image normalization standard deviation.
  • re_prob — Random erasing probability.
  • re_mode — Random erasing fill mode.
  • re_count — Number of random erasing regions.
  • re_num_splits — Control split of random erasing across batch size.
  • crop_pct — Inference crop percentage (output size / resize size).
  • crop_mode — Inference crop mode. One of [‘squash’, ‘border’, ‘center’]. Defaults to ‘center’ when None.
  • crop_border_pixels — Inference crop border of specified # pixels around edge of original image.
  • tf_preprocessing — Use TF 1.0 inference preprocessing for testing model ports
  • use_prefetcher — Pre-fetcher enabled. Do not convert image to tensor or normalize.
  • normalize — Normalization tensor output w/ provided mean/std (if prefetcher not used).
  • separate — Output transforms in 3-stage tuple.
  • patchify — Patchify the output instead of relying on prefetcher.
  • patchify_channels_last — Use channels-last layout within each patch.
  • patchify_flatten — Flatten eval patches into vectors. Disable when the model needs their spatial dimensions for patch-size interpolation.

timm.data.rand_augment_transform

< >

( config_str: strhparams: typing.Optional[typing.Dict] = Nonetransforms: typing.Union[str, typing.Dict, typing.List, NoneType] = None )

Parameters

  • config_str (str) — String defining configuration of random augmentation. Consists of multiple sections separated by dashes (’-’). The first section defines the specific variant of rand augment (currently only ‘rand’). The remaining sections, not order specific determine
    • ‘m’ - integer magnitude of rand augment
    • ‘n’ - integer num layers (number of transform ops selected per image)
    • ‘p’ - float probability of applying each layer (default 0.5)
    • ‘mstd’ - float std deviation of magnitude noise applied, or uniform sampling if infinity (or > 100)
    • ‘mmax’ - set upper bound for magnitude to something other than default of _LEVEL_DENOM (10)
    • ‘inc’ - integer (bool), use augmentations that increase in severity with magnitude (default: 0)
    • ‘t’ - str name of transform set to use
  • hparams (dict) — Other hparams (kwargs) for the RandAugmentation scheme

Create a RandAugment transform

Examples:

  • 'rand-m9-n3-mstd0.5' is RandAugment with magnitude 9, 3 layers and a magnitude std of 0.5.
  • 'rand-mstd1-tweights' is RandAugment with a magnitude std of 1.0 and weighted transform selection, at the default magnitude of 10 and 2 layers.

timm.data.auto_augment_transform

< >

( config_str: strhparams: typing.Optional[typing.Dict] = None )

Parameters

  • config_str — String defining configuration of auto augmentation. Consists of multiple sections separated by dashes (’-’). The first section defines the AutoAugment policy (one of ‘v0’, ‘v0r’, ‘original’, ‘originalr’). While the remaining sections define other arguments
    • ‘mstd’ - float std deviation of magnitude noise applied
  • hparams — Other hparams (kwargs) for the AutoAugmentation scheme

Create a AutoAugment transform

Examples:

  • 'original-mstd0.5' is AutoAugment with the original policy and a magnitude std of 0.5.

timm.data.auto_augment.augment_and_mix_transform

< >

( config_str: strhparams: typing.Optional[typing.Dict] = None )

Parameters

  • config_str (str) — String defining configuration of random augmentation. Consists of multiple sections separated by dashes (’-’). The first section defines the specific variant of rand augment (currently only ‘rand’). The remaining sections, not order specific determine ‘m’ - integer magnitude (severity) of augmentation mix (default: 3) ‘w’ - integer width of augmentation chain (default: 3) ‘d’ - integer depth of augmentation chain (-1 is random [1, 3], default: -1) ‘b’ - integer (bool), blend each branch of chain into end result without a final blend, less CPU (default: 0) ‘mstd’ - float std deviation of magnitude noise applied (default: 0) Ex ‘augmix-m5-w4-d2’ results in AugMix with severity 5, chain width 4, chain depth 2
  • hparams — Other hparams (kwargs) for the Augmentation transforms

Create AugMix PyTorch transform

class timm.data.random_erasing.RandomErasing

< >

( probability = 0.5min_area = 0.02max_area = 0.3333333333333333min_aspect = 0.3max_aspect = Nonemode = 'const'min_count = 1max_count = Nonenum_splits = 0device = 'cuda' )

Parameters

  • probability — Probability that the Random Erasing operation will be performed.
  • min_area — Minimum percentage of erased area wrt input image area.
  • max_area — Maximum percentage of erased area wrt input image area.
  • min_aspect — Minimum aspect ratio of erased area.
  • mode — pixel color mode, one of ‘const’, ‘rand’, or ‘pixel’ ‘const’ - erase block is constant color of 0 for all channels ‘rand’ - erase block is same per-channel random (normal) color ‘pixel’ - erase block is per-pixel random (normal) color

Randomly selects a rectangle region in an image and erases its pixels. ‘Random Erasing Data Augmentation’ by Zhong et al. See https://arxiv.org/pdf/1708.04896.pdf

This variant of RandomErasing is intended to be applied to either a batch or single image tensor after it has been normalized by dataset mean and std.

max_count: maximum number of erasing blocks per image, area per box is scaled by count. per-image count is randomly chosen between 1 and this value.

Datasets

timm.data.create_dataset

< >

( name: strroot: typing.Optional[str] = Nonesplit: str = 'validation'search_split: bool = Trueclass_map: dict = Noneload_bytes: bool = Falseis_training: bool = Falsedownload: bool = Falsebatch_size: int = 1num_samples: typing.Optional[int] = Noneseed: int = 42repeats: int = 0input_img_mode: str = 'RGB'trust_remote_code: bool = False**kwargs )

Parameters

  • name — Dataset name, empty is okay for folder based datasets
  • root — Root folder of dataset (All)
  • split — Dataset split (All)
  • search_split — Search for split specific child fold from root so one can specify imagenet/ instead of /imagenet/val, etc on cmd line / config. (Folder, Torch)
  • class_map — Specify class -> index mapping via text file or dict (Folder)
  • load_bytes — Load data, return images as undecoded bytes (Folder)
  • download — Download dataset if not present and supported (HFIDS, TFDS, Torch)
  • is_training — Create dataset in train mode, this is different from the split. For Iterable / TDFS it enables shuffle, ignored for other datasets. (TFDS, WDS, HFIDS)
  • batch_size — Batch size hint for iterable datasets (TFDS, WDS, HFIDS)
  • seed — Seed for iterable datasets (TFDS, WDS, HFIDS)
  • repeats — Dataset repeats per iteration i.e. epoch (TFDS, WDS, HFIDS)
  • input_img_mode — Input image color conversion mode e.g. ‘RGB’, ‘L’ (folder, TFDS, WDS, HFDS, HFIDS)
  • trust_remote_code — Trust remote code in Hugging Face Datasets if True (HFDS, HFIDS)
  • **kwargs — Other args to pass through to underlying Dataset and/or Reader classes

Dataset factory method

In parentheses after each arg are the type of dataset supported for each arg, one of:

  • Folder - default, timm folder (or tar) based ImageDataset
  • Torch - torchvision based datasets
  • HFDS - Hugging Face Datasets
  • HFIDS - Hugging Face Datasets Iterable (streaming mode, with IterableDataset)
  • TFDS - Tensorflow-datasets wrapper in IterabeDataset interface via IterableImageDataset
  • WDS - Webdataset
  • All - any of the above

class timm.data.ImageDataset

< >

( rootreader = Nonesplit = 'train'class_map = Noneload_bytes = Falseinput_img_mode = 'RGB'transform = Nonetarget_transform = Noneadditional_features = None**kwargs )

class timm.data.IterableImageDataset

< >

( rootreader = Nonesplit = 'train'class_map = Noneis_training = Falsebatch_size = 1num_samples = Noneseed = 42repeats = 0download = Falseinput_img_mode = 'RGB'input_key = Nonetarget_key = Nonetransform = Nonetarget_transform = Nonemax_steps = None**kwargs )

class timm.data.AugMixDataset

< >

( datasetnum_splits = 2 )

Dataset wrapper to perform AugMix or other clean/augmentation mixes

class timm.data.MultiLabelTarget

< >

( num_classes: intdense: bool = False )

Encode a multi-label target as a float32 multi-hot tensor of length num_classes.

By default the target is a sequence of positive class indices: an empty sequence means no positive labels, repeats are allowed, and negative, out-of-range, or non-integer values are rejected. With dense=True the target is already a vector of length num_classes holding bool, int, or float values in [0, 1]; it is validated and cast to float32, keeping soft values.

Loaders

timm.data.create_loader

< >

( dataset: typing.Union[timm.data.dataset.ImageDataset, timm.data.dataset.IterableImageDataset]input_size: typing.Union[int, typing.Tuple[int, int], typing.Tuple[int, int, int]]batch_size: intis_training: bool = Falseno_aug: bool = Falsere_prob: float = 0.0re_mode: str = 'const're_count: int = 1re_split: bool = Falsetrain_crop_mode: typing.Optional[str] = Nonescale: typing.Optional[typing.Tuple[float, float]] = Noneratio: typing.Optional[typing.Tuple[float, float]] = Nonehflip: float = 0.5vflip: float = 0.0color_jitter: float = 0.4color_jitter_prob: typing.Optional[float] = Nonegrayscale_prob: float = 0.0gaussian_blur_prob: float = 0.0auto_augment: typing.Optional[str] = Nonenum_aug_repeats: int = 0num_aug_splits: int = 0interpolation: str = 'bilinear'mean: typing.Tuple[float, ...] = (0.485, 0.456, 0.406)std: typing.Tuple[float, ...] = (0.229, 0.224, 0.225)num_workers: int = 1distributed: bool = Falsecrop_pct: typing.Optional[float] = Nonecrop_mode: typing.Optional[str] = Nonecrop_border_pixels: typing.Optional[int] = Nonecollate_fn: typing.Optional[typing.Callable] = Nonepin_memory: bool = Falsefp16: bool = Falseimg_dtype: dtype = torch.float32device: device = torch.device()use_prefetcher: bool = Trueuse_multi_epochs_loader: bool = Falsepersistent_workers: bool = Trueworker_seeding: str = 'all'tf_preprocessing: bool = Falseinput_size_choices: typing.Optional[typing.Sequence[typing.Union[int, typing.Tuple[int, int], typing.Tuple[int, int, int]]]] = Nonebatch_size_choices: typing.Optional[typing.Sequence[int]] = Nonebatch_choice_weights: typing.Optional[typing.Sequence[float]] = Nonebatch_choice_seed: int = 0batch_choice_schedule: str = 'constant'batch_schedule_epochs: typing.Optional[int] = Nonebatch_schedule_spread: float = 0.65batch_schedule_random_mix: float = 0.1num_batches: typing.Optional[int] = None )

Parameters

  • dataset — The image dataset to load.
  • input_size — Target input size (channels, height, width) tuple or size scalar.
  • batch_size — Number of samples in a batch.
  • is_training — Return training (random) transforms.
  • no_aug — Disable augmentation for training (useful for debug).
  • re_prob — Random erasing probability.
  • re_mode — Random erasing fill mode.
  • re_count — Number of random erasing regions.
  • re_split — Control split of random erasing across batch size.
  • scale — Random resize scale range (crop area, < 1.0 => zoom in).
  • ratio — Random aspect ratio range (crop ratio for RRC, ratio adjustment factor for RKR).
  • hflip — Horizontal flip probability.
  • vflip — Vertical flip probability.
  • color_jitter — Random color jitter component factors (brightness, contrast, saturation, hue). Scalar is applied as (scalar,) * 3 (no hue).
  • color_jitter_prob — Apply color jitter with this probability if not None (for SimlCLR-like aug
  • grayscale_prob — Probability of converting image to grayscale (for SimCLR-like aug).
  • gaussian_blur_prob — Probability of applying gaussian blur (for SimCLR-like aug).
  • auto_augment — Auto augment configuration string (see auto_augment.py).
  • num_aug_repeats — Enable special sampler to repeat same augmentation across distributed GPUs.
  • num_aug_splits — Enable mode where augmentations can be split across the batch.
  • interpolation — Image interpolation mode.
  • mean — Image normalization mean.
  • std — Image normalization standard deviation.
  • num_workers — Num worker processes per DataLoader.
  • distributed — Enable dataloading for distributed training.
  • crop_pct — Inference crop percentage (output size / resize size).
  • crop_mode — Inference crop mode. One of [‘squash’, ‘border’, ‘center’]. Defaults to ‘center’ when None.
  • crop_border_pixels — Inference crop border of specified # pixels around edge of original image.
  • collate_fn — Override default collate_fn.
  • pin_memory — Pin memory for device transfer.
  • fp16 — Deprecated argument for half-precision input dtype. Use img_dtype.
  • img_dtype — Data type for input image.
  • device — Device to transfer inputs and targets to.
  • use_prefetcher — Use efficient pre-fetcher to load samples onto device.
  • use_multi_epochs_loader —
  • persistent_workers — Enable persistent worker processes when num_workers is greater than zero.
  • worker_seeding — Control worker random seeding at init.
  • tf_preprocessing — Use TF 1.0 inference preprocessing for testing model ports.
  • input_size_choices — Per-batch input size choices for scheduled resolution training.
  • batch_size_choices — Batch size corresponding to each input size choice. Uses batch_size for all choices when not specified.
  • batch_choice_weights — Sampling weights for input size choices. Uniform when not specified.
  • batch_choice_seed — Random seed used to create and shuffle the batch schedule.
  • batch_choice_schedule — Choice schedule mode, either constant or progressive.
  • batch_schedule_epochs — Number of epochs over which a progressive schedule moves through its choices.
  • batch_schedule_spread — Standard deviation of the progressive choice window, in choice-index units.
  • batch_schedule_random_mix — Fraction of uniform random exploration over nonzero-weight choices in a progressive schedule.
  • num_batches — Fixed number of loader batches per epoch. Inferred from the schedule-average batch size for progressive schedules when not specified.

Mixup and CutMix

class timm.data.Mixup

< >

( mixup_alpha = 1.0cutmix_alpha = 0.0cutmix_minmax = Noneprob = 1.0switch_prob = 0.5mode = 'batch'correct_lam = Truelabel_smoothing = 0.1num_classes = 1000multi_label = False )

Parameters

  • mixup_alpha (float) — mixup alpha value, mixup is active if > 0.
  • cutmix_alpha (float) — cutmix alpha value, cutmix is active if > 0.
  • cutmix_minmax (List[float]) — cutmix min/max image ratio, cutmix is active and uses this vs alpha if not None.
  • prob (float) — probability of applying mixup or cutmix per batch or element
  • switch_prob (float) — probability of switching to cutmix instead of mixup when both are active
  • mode (str) — how to apply mixup/cutmix params (per ‘batch’, ‘pair’ (pair of elements), ‘elem’ (element)
  • correct_lam (bool) — apply lambda correction when cutmix bbox clipped by image borders
  • label_smoothing (float) — apply label smoothing to the mixed target tensor
  • num_classes (int) — number of classes for target
  • multi_label (bool) — Mix dense multi-label targets, smoothing each label towards 0.5.

Mixup/Cutmix that applies different params to each element or whole batch

class timm.data.FastCollateMixup

< >

( mixup_alpha = 1.0cutmix_alpha = 0.0cutmix_minmax = Noneprob = 1.0switch_prob = 0.5mode = 'batch'correct_lam = Truelabel_smoothing = 0.1num_classes = 1000multi_label = False )

Fast Collate w/ Mixup/Cutmix that applies different params to each element or whole batch

A Mixup impl that’s performed while collating the batches.

NaFlex

timm.data.create_naflex_loader

< >

( datasetpatch_size: typing.Union[int, typing.Tuple[int, int], NoneType] = Nonepatch_size_choices: typing.Optional[typing.List[int]] = Nonepatch_size_choice_probs: typing.Optional[typing.List[float]] = Nonetrain_seq_lens: typing.Tuple[int, ...] = (128, 256, 576, 784, 1024)max_seq_len: int = 576batch_size: int = 32is_training: bool = Falsemixup_fn: typing.Optional[typing.Callable] = Noneno_aug: bool = Falsere_prob: float = 0.0re_mode: str = 'const're_count: int = 1re_split: bool = Falsetrain_crop_mode: typing.Optional[str] = Nonescale: typing.Optional[typing.Tuple[float, float]] = Noneratio: typing.Optional[typing.Tuple[float, float]] = Nonehflip: float = 0.5vflip: float = 0.0color_jitter: float = 0.4color_jitter_prob: typing.Optional[float] = Nonegrayscale_prob: float = 0.0gaussian_blur_prob: float = 0.0auto_augment: typing.Optional[str] = Nonenum_aug_repeats: int = 0num_aug_splits: int = 0interpolation: str = 'bilinear'mean: typing.Tuple[float, ...] = (0.485, 0.456, 0.406)std: typing.Tuple[float, ...] = (0.229, 0.224, 0.225)crop_pct: typing.Optional[float] = Nonecrop_mode: typing.Optional[str] = Nonecrop_border_pixels: typing.Optional[int] = Nonenum_workers: int = 4distributed: bool = Falserank: int = 0world_size: int = 1seed: int = 42epoch: int = 0use_prefetcher: bool = Truepin_memory: bool = Trueimg_dtype: dtype = torch.float32device: typing.Union[str, torch.device] = torch.device()persistent_workers: bool = Trueworker_seeding: str = 'all'patchify_channels_last: bool = True )

Parameters

  • dataset — Dataset to load from.
  • patch_size — Single patch size to use.
  • patch_size_choices — List of patch sizes for variable patch size training.
  • patch_size_choice_probs — Probabilities for each patch size choice.
  • train_seq_lens — Training sequence lengths for dynamic batching.
  • max_seq_len — Fixed sequence length for validation.
  • batch_size — Batch size for validation and max training sequence length.
  • is_training — Whether this is for training (enables dynamic batching).
  • mixup_fn — Optional mixup function.
  • no_aug — Disable augmentation.
  • re_prob — Random erasing probability.
  • re_mode — Random erasing mode.
  • re_count — Maximum number of erasing rectangles.
  • re_split — Random erasing split flag.
  • train_crop_mode — Training crop mode.
  • scale — Scale range for random resize crop.
  • ratio — Aspect ratio range for random resize crop.
  • hflip — Horizontal flip probability.
  • vflip — Vertical flip probability.
  • color_jitter — Color jitter factor.
  • color_jitter_prob — Color jitter probability.
  • grayscale_prob — Grayscale conversion probability.
  • gaussian_blur_prob — Gaussian blur probability.
  • auto_augment — AutoAugment policy.
  • num_aug_repeats — Number of augmentation repeats.
  • num_aug_splits — Number of augmentation splits.
  • interpolation — Interpolation method.
  • mean — Normalization mean values.
  • std — Normalization standard deviation values.
  • crop_pct — Crop percentage for validation.
  • crop_mode — Crop mode.
  • crop_border_pixels — Crop border pixels.
  • num_workers — Number of data loading workers.
  • distributed — Whether using distributed training.
  • rank — Process rank for distributed training.
  • world_size — Total number of processes.
  • seed — Random seed.
  • epoch — Starting epoch.
  • use_prefetcher — Whether to use prefetching.
  • pin_memory — Whether to pin memory.
  • img_dtype — Image data type.
  • device — Device to move tensors to.
  • persistent_workers — Whether to use persistent workers when num_workers is greater than zero.
  • worker_seeding — Worker seeding mode.
  • patchify_channels_last — Per-patch flat layout. True (default): channel index varies fastest (NaFlex default). False: C-P-P (channels first — Gemma4 / HF native layout). Forwarded to the upstream Patchify and to the prefetcher’s normalization layout.

Create a data loader with dynamic sequence length sampling for training.

class timm.data.NaFlexMapDatasetWrapper

< >

( base_dataset: Datasetpatch_size: typing.Union[int, typing.Tuple[int, int], NoneType] = Nonepatch_size_choices: typing.Optional[typing.List[int]] = Nonepatch_size_choice_probs: typing.Optional[typing.List[float]] = Noneseq_lens: typing.Tuple[int, ...] = (128, 256, 576, 784, 1024)max_tokens_per_batch: int = 16384transform_factory: typing.Optional[typing.Callable] = Nonemixup_fn: typing.Optional[typing.Callable] = Noneseed: int = 42shuffle: bool = Truedistributed: bool = Falserank: int = 0world_size: int = 1epoch: int = 0batch_divisor: int = 8patchify_channels_last: bool = True )

IterableDataset wrapper for a map-style base dataset.

Yields batches with variable sequence lengths. It calculates a canonical batch schedule (sequence length, batch size pairs) once based on the total dataset size (padded for distribution). Each epoch, it shuffles the order of this canonical schedule and the dataset indices. This ensures a consistent number of batches and samples per epoch across all ranks. Handles distributed training and multiple workers.

set_epoch

< >

( epoch: int )

Parameters

  • epoch — New epoch number.

Set the multiprocessing-safe epoch read by DataLoader workers.

class timm.data.NaFlexMixup

< >

( num_classes: intmixup_alpha: float = 0.8cutmix_alpha: float = 1.0switch_prob: float = 0.5prob: float = 1.0local_shuffle: int = 4label_smoothing: float = 0.0mixup_off_epoch: int = 0multi_label: bool = False )

Callable wrapper that combines mixing and target generation.

set_epoch

< >

( epoch: int )

Update the mixup state for the given epoch.

Update on GitHub