I don’t think I have the most efficient answer, but it is the one that worked for me and solved my problem when I was dividing FMD (Flickr Material Database) for doing cross-validation:
You can create two different instances of the dataset, doing like
dataset_train = datasets.ImageFolder(data_dir, transform=train_transforms)
dataset_val = datasets.ImageFolder(data_dir, transform=val_transforms)

and then you subset each one with the indices that you want:
trainset = torch.utils.data.Subset(dataset_train, train_indices)
valset = torch.utils.data.Subset(dataset_val, val_indices)

For that to work you just have to create the “train_indices” and “val_indices” lists containing all the indices you want on each. And from that on you just use “trainset” and “valset” to create your dataloaders or to do whatever you want.
Notes:

I have used different transforms for train and validation as I think is common practice.
I would appreciate any comments and suggestions about how to do it in a better way.