Feature Request for torchvision ImageFolder using/inheriting DatasetFolder
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 17.9k
- Forks
- 7.3k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 13
Description
I came across a feature other users also demanded as may be seen in pytorch forums.
For a detailed problem Description and how to solve it see here in a discussion from users, @ptrblck and me:
https://discuss.pytorch.org/t/how-to-sample-images-belonging-to-particular-classes/43776/9
In short: Using ImageFolder, which inherits from DatasetFolder, is limiting the user to retrieve a whole dataset from a folder, instead of just using some classes/dirs of the folder structure. Even though one can implement a custom find_classes() method or rather call it a function if one passes it an overwritten DatasetFolder custom implementation, this is often hidden to the user, since one only uses ImageFolder which uses DatasetFolder under the hood.
For users getting this wrong also see the pytorch discussion from the link above in the forum, where @ptrblck and I figured out that it would be nice to be able to just pass such a function that only selects a subset of a folder structure directly by passing an optional function to the ImageFolder.
The line I am talking about in current torchvision DatasetFolder implementation, where subsets from a folder may be retrieved, by overwriting this function:
https://github.com/pytorch/vision/blob/fba4f42e3bc24b7b2c6cad09b6db653ac73dc6b7/torchvision/datasets/folder.py#L144
My Suggestion for this improvement that users can use only a subset of a folder structure in ImageFolder looks as follows as also stated in the pytorch forum:
def find_classes(directory: str, desired_class_names: List) -> Tuple[List[str], Dict[str, int]]:
"""Finds the class folders in a dataset."""
classes = sorted(entry.name for entry in os.scandir(directory) if entry.is_dir())
classes = classes [desired_class_names] # TODO: do something like this line! Not tested it yet!
if not classes:
raise FileNotFoundError(f"Couldn't find any class folder in {directory}.")
class_to_idx = {cls_name: i for i, cls_name in enumerate(classes)}
return classes, class_to_idx
Current implementation suggest overwriting the function as follows within DatasetFolder, but most Users tend to be using ImageFolder as I inferred from posts.
Also as stated @ptrblck suggested to make it possible to pass a function to ImageFolder directly instead of overwriting DatasetFolder. Regarding this i have no code to suggest but it might be trivial by just passing parameters.
cc @pmeier
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with torchvision/datasets/folder.py and the ImageFolder/DatasetFolder find_classes entry points linked in the issue. Clarify the optional class-selection API and its expected behavior, then verify that ImageFolder can restrict discovered classes while preserving normal DatasetFolder behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- computer-vision, data
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100