Skip to content

Feature Request: Support for Path objects in torchvision.datasets.ImageFolder #8525

@songyuc

Description

@songyuc

🚀 The feature

It would be highly beneficial for PyTorch to support Path objects from the pathlib module when specifying dataset directories in functions like torchvision.datasets.ImageFolder. This support would make the code more Pythonic and in line with modern Python programming practices.

Motivation, pitch

Currently, when using torchvision.datasets.ImageFolder, the dataset directory must be specified as a string. However, the Path object is more versatile and is recommended for file system paths in Python 3. By supporting Path objects, users can take full advantage of the functionality provided by the pathlib module.

Example

from pathlib import Path
from torchvision import datasets, transforms

data_dir = Path("dataset", "train")
transform = transforms.Compose([
    transforms.Resize((256, 256)),
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])

# Currently, this requires converting Path to str
dataset = datasets.ImageFolder(str(data_dir), transform=transform)

# Proposed enhancement would allow this
dataset = datasets.ImageFolder(data_dir, transform=transform)

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