Random split per class in ImageFolder

I am using torchvision ImageFolder to load my training data where each directory contain samples for a particular class instance.

I want to split the data into training and validation set. How can I do it so that the split is done per class i.e. if the split is 90-10 for 90% training data and 10% validation, it should randomly choose 90% of the data for training from each of the folders.

Is there an easy way to do this from within pytorch with the ImageFolder dataset?

You could use sklearn.model_selection.train_test_split with the stratify option to create the desired sample indices.
Once you have the indices, create the training and validation datasets by wrapping the whole dataset into Subsets and passing the corresponding indices to them.