Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
songyuc opened this issue Jul 15, 2024 · 1 comment · Fixed by #8526
Closed

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

songyuc opened this issue Jul 15, 2024 · 1 comment · Fixed by #8526

Comments

@songyuc
Copy link

songyuc commented Jul 15, 2024

🚀 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)
@NicolasHug
Copy link
Member

Thanks for the feature request @songyuc . ImageFolder already supports Path objects, we probably just forgot to edit its type annotation. I've opened #8526 to fix that, it will be available in the next release soon, but you should already be able to use Path objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants