How to use datasets.ImageFolder() for custom datasets?

uppose I have to design a 3 class classifier with following things in mind:

I have 3 folders as datasets such as:

  • …/data/cat/ …/data/dog/ …/data/man/

Each folder has images for that particular class only.

We divide the images into train,test,val using the following:

train_data = datasets.ImageFolder(train_dir, transform=data_transforms[‘train’])

test_data = datasets.ImageFolder(test_dir, transform=data_transforms[‘test’])

My question is how will ImageFolder() divide the images into train,test from EACH class folder as given above??


img,target = netx(iter(…))

gives the target or label. How is this generated? like how will ImageFolder() assign target=1 when taking image from …/data/cat/ and target=1 when taking from …/data/dog/ etc

It won’t divide the folders automatically. ImageFolder takes the root folder as an argument and will use all images from all subfolders as data samples.

To split the dataset, you could use, passing the indices, or also passing the indices.

ImageFolder will sort all subfolders and assign a class label to each of them as seen in these lines of code.