Splitting a directory with images into sub folders using Pytorch or Python
I have a directory with two sub directories in it. Each sub-dir has bunch of images. These sub-dir also specify the two classes of images. 

I want to have 3 directories (train, validation, test) and within each of these 3 sub dir, I want to 2 sub directory of each class respectively with images. 

I want to split the no.of images into train, val, test directories by random sampling. SO some 60% images go to train, 20, val and 20 in test. 

Initial Structure: 

Main_folder
 - Good
 - Bad 


What I want: 

Main_folder: 
 - Train 
  - Good
  - Bad
 - Val
  - Good 
  - Bad
 - Test 
  - Good 
  - Bad


I want to split each sub directories into two directories within it with random assignment of images (

There is a pytorch way to do that (and I would advise using single library for such easy task).
Creating dataset
There is a pre-made one inside torchvision, namely ImageFolder.
Simply use it like this:
import torchvision

dataset = torchvision.datasets.ImageFolder(&quot;./my_data&quot;)

This will create a dataset, where Good folder has 0 label and Bad has label of 1 for each image in those folder respectively.
Dividing into train, validation, test
No need for sklearn for such an easy job + torch has most of the numpy functionality as well and I would rather stick with one library instead of 3 (though train_test_split could be used twice consecutively, similar to what @ESZ proposed).
IMO simpler would be:
def get_subset(indices, start, end):
    return indices[start : start + end]


TRAIN_PCT, VALIDATION_PCT = 0.6, 0.2  # rest will go for test
train_count = int(len(dataset) * TRAIN_PCT)
validation_count = int(len(dataset) * VALIDATION_PCT)

indices = torch.randperm(len(dataset))

train_indices = get_subset(indices, 0, train_count)
validation_indices = get_subset(indices, train_count, validation_count)
test_indices = get_subset(indices, train_count + validation_count, len(dataset))

This will create indices for SubsetRandomSampler and torch.utils.data.DataLoader.
So, similarly to @ESZ once again:
dataloaders = {
    &quot;train&quot;: torch.utils.data.DataLoader(
        dataset, sampler=SubsetRandomSampler(train_indices)
    ),
    &quot;validation&quot;: torch.utils.data.DataLoader(
        dataset, sampler=SubsetRandomSampler(validation_indices)
    ),
    &quot;test&quot;: torch.utils.data.DataLoader(
        dataset, sampler=SubsetRandomSampler(test_indices)
    ),
}

You can specify batch_size and other argument to DataLoader, see documentation if you need more info.
