Skip to content

[Feature Request] add ImageFolder/DatasetFolder: incremental index caching to avoid full filesystem re-scans #9659

Open

Description

@powerofaisinstudy-debug

ImageFolder and DatasetFolder do a full recursive scan of the directory
(os.walk / os.scandir) every time they're instantiated, even if nothing on
disk has changed. For large datasets, or anything on a network mount, this
can take minutes — every single run.

This hurts most with: restarting training scripts during debugging, jobs
that get preempted and restarted on clusters, distributed training where
every rank/worker rescans the same unchanged tree, and datasets that grow
over time with no way to update the index without a full rescan.

Image

An opt-in cache_index flag with an incremental mode that only rescans
folders that actually changed.

from torchvision.datasets import ImageFolder

dataset = ImageFolder("data/train/")

dataset = ImageFolder(
    "data/train/",
    cache_index=True,
    cache_mode="incremental",
    cache_path=None,
)

First run scans normally and writes the index to cache_path. Later runs
check folder mtimes against the cache, rescan only what changed, and merge
the result — no full rescan unless nearly everything changed.

Alternatives considered

Leaving this to user-built wrappers (already common, but inconsistent and
often buggy), caching by default (risky — could silently hide stale data),
and a heavier format like LMDB/SQLite (more than this needs).

Pure Python change in torchvision/datasets/folder.py, fully backward
compatible.

Activity

  1. MBUYt0n commented on Sep 16, 2026

    @MBUYt0n

    Hi, I'm currently looking into this issue and will try raising a PR on it soon!

  2. Kaustubh1235 commented on Sep 19, 2026

    @Kaustubh1235

    The idea of adding an incremental index caching feature sounds useful, especially for large datasets or network mounts. It could really help with reducing unnecessary rescans during debugging or distributed training.

  3. powerofaisinstudy-debug commented on Sep 20, 2026

    @powerofaisinstudy-debug
    Author

    Hi @MBUYt0n, checking in to see if you've started working on a draft PR for this! I'd be happy to collaborate, help write unit tests, or test the implementation once it's ready.

    Also @Kaustubh1235, totally agree—incremental index caching would be a huge performance win for large datasets and network mounts."

  4. MBUYt0n commented on Sep 23, 2026

    @MBUYt0n

    I'll raise the PR once the issue is assigned to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions