PyTorch datasets: ImageFolder and subfolder filtering
I would like to use ImageFolder to create an Image Dataset.

My current image directory structure looks like this:

/root
-- train/
---- 001.jpg
---- 002.jpg
---- ....
-- test/
---- 001.jpg
---- 002.jpg
---- ....


I would like to have a dataset dedicated to training data, and a dataset dedicated to test data.

As I understand, doing so:

dataset = ImageFolder(root='root/train')


does not find any images.

Doing

dataset = ImageFolder(root='root')


find images but train and test images are just scrambled together.

ImageFolder has argument loader but I did not manage to find any use-case for it.

How can I discriminate images in the root folder according to the subfolder they belong to?

ImageFolder expects the data folder (the one that you pass as root) to contain subfolders representing the classes to which its images belong. Something like this:
data/
├── train/
|   ├── class_0/
|   |   ├── 001.jpg
|   |   ├── 002.jpg
|   |   └── 003.jpg
|   └── class_1/
|       ├── 004.jpg
|       └── 005.jpg
└── test/
    ├── class_0/
    |   ├── 006.jpg
    |   └── 007.jpg
    └── class_1/
        ├── 008.jpg
        └── 009.jpg

Having the above folder structure you can do the following:
train_dataset = ImageFolder(root='data/train')
test_dataset  = ImageFolder(root='data/test')

Since you don't have that structure, one obvious option is to create class-subfolders and put the images into them. Another option is to create a custom Dataset, see here.
