huggingface/pytorch-image-models · warning
Transform returned None for index {idx}. Skipping sample.
Error message
Transform returned None for index {idx}. Skipping sample. What it means
NaFlexDataset.__iter__ applies the per-sample transform and checks for None returns. A None means the transform pipeline explicitly returned None (common with 'bad image' guards like PIL's Image.open failure fallbacks or timm's image-not-loaded checks) and the sample is skipped with a warning.
Source
Thrown at timm/data/naflex_dataset.py:544
continue
# Get the pre-initialized transform and patchifier using patch_idx
transform_key = (seq_len, patch_idx)
transform = self.transforms.get(transform_key)
batch_patchifier = self.patchifiers[patch_idx]
batch_imgs = []
batch_targets = []
for idx in indices:
try:
# Get original image and label from map-style dataset
img, label = self.base_dataset[idx]
# Apply transform if available
# Handle cases where transform might return None or fail
processed_img = transform(img) if transform else img
if processed_img is None:
warnings.warn(f"Transform returned None for index {idx}. Skipping sample.")
continue
batch_imgs.append(processed_img)
batch_targets.append(label)
except IndexError:
warnings.warn(f"IndexError encountered for index {idx} (possibly due to padding/repeated indices). Skipping sample.")
continue
except Exception as e:
# Log other potential errors during data loading/processing
warnings.warn(f"Error processing sample index {idx}. Error: {e}. Skipping sample.")
continue # Skip problematic sample
if self.mixup_fn is not None:
batch_imgs, batch_targets = self.mixup_fn(batch_imgs, batch_targets)
batch_imgs = [batch_patchifier(img) for img in batch_imgs]
batch_samples = list(zip(batch_imgs, batch_targets))View on GitHub (pinned to 9a5261e31b)
Solutions
- Audit your transform chain: every branch must return a tensor; a missing return yields None
- Remove or repair the flagged dataset samples
- If a guard transform intentionally returns None for bad images, accept the skip and clean the dataset
Example fix
# before
class MyTransform:
def __call__(self, img):
if img is None:
return None # or: forgets return on some path
# after
class MyTransform:
def __call__(self, img):
if img is None:
raise ValueError('bad image') # or return a placeholder tensor
return do_process(img) Defensive patterns
Strategy: validation
Validate before calling
out = transform(sample_img)\nassert out is not None, 'transform pipeline has a None-returning branch'
Prevention
- Unit-test transforms: every code path must return
- Lint custom transforms for missing return statements
When it happens
Trigger: A transform whose failure mode is returning None — e.g. a custom transform returning None on decode failure, or timm's ImageNetInfo/bad-image transforms — hitting a corrupt or missing sample during iteration.
Common situations: Datasets with unreadable entries where the transform authors chose None over raising; also custom user transforms that forget to return on some branch, making every sample return None (then warnings flood and batches are empty).
Related errors
- Error processing sample index {idx}. Error: {e}. Skipping sa
- {name.capitalize()} range reversed. Swapping.
- final_scale_range values should ideally be between 0.0 and 1
- Final scale randomization ({scale_factor:.2f}) resulted in s
- Input image must have positive dimensions, got H={height}, W
AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27).
Data as JSON: /api/errors/2cd2db1d87f35109.
Report an issue: GitHub.