{"record":{"id":"2cd2db1d87f35109","repo":"huggingface/pytorch-image-models","slug":"transform-returned-none-for-index-idx-skipping","errorCode":null,"errorMessage":"Transform returned None for index {idx}. Skipping sample.","messagePattern":"Transform returned None for index (.+?)\\. Skipping sample\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"timm/data/naflex_dataset.py","lineNumber":544,"sourceCode":"                 continue\n\n            # Get the pre-initialized transform and patchifier using patch_idx\n            transform_key = (seq_len, patch_idx)\n            transform = self.transforms.get(transform_key)\n            batch_patchifier = self.patchifiers[patch_idx]\n\n            batch_imgs = []\n            batch_targets = []\n            for idx in indices:\n                try:\n                    # Get original image and label from map-style dataset\n                    img, label = self.base_dataset[idx]\n\n                    # Apply transform if available\n                    # Handle cases where transform might return None or fail\n                    processed_img = transform(img) if transform else img\n                    if processed_img is None:\n                        warnings.warn(f\"Transform returned None for index {idx}. Skipping sample.\")\n                        continue\n\n                    batch_imgs.append(processed_img)\n                    batch_targets.append(label)\n\n                except IndexError:\n                     warnings.warn(f\"IndexError encountered for index {idx} (possibly due to padding/repeated indices). Skipping sample.\")\n                     continue\n                except Exception as e:\n                    # Log other potential errors during data loading/processing\n                    warnings.warn(f\"Error processing sample index {idx}. Error: {e}. Skipping sample.\")\n                    continue # Skip problematic sample\n\n            if self.mixup_fn is not None:\n                batch_imgs, batch_targets = self.mixup_fn(batch_imgs, batch_targets)\n\n            batch_imgs = [batch_patchifier(img) for img in batch_imgs]\n            batch_samples = list(zip(batch_imgs, batch_targets))","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/data/naflex_dataset.py#L526-L562","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"# before\nclass MyTransform:\n    def __call__(self, img):\n        if img is None:\n            return None  # or: forgets return on some path\n# after\nclass MyTransform:\n    def __call__(self, img):\n        if img is None:\n            raise ValueError('bad image')  # or return a placeholder tensor\n        return do_process(img)","handlingStrategy":"validation","validationCode":"out = transform(sample_img)\\nassert out is not None, 'transform pipeline has a None-returning branch'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Unit-test transforms: every code path must return","Lint custom transforms for missing return statements"],"tags":["naflex","transforms","robust-loading","timm"],"backgroundTag":"transform-returned-none","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}