Train set can be divided into train and validation set by using random_split method of torch.utils.data.dataset.
Importing random split method
from torch.utils.data.dataset import random_split
from torchvision import datasets
Providing train test set dir paths for creating dataset class objects and applying transformations
mage_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),  data_transforms[x]) for x in [‘train’, ‘test’]}
Creating variable for holding original train dataset len
train_dataset_len = image_datasets[‘train’].len()
Creating 50 50 train/validation splits out of original train set
image_datasets[‘train’], image_datasets[‘val’] = random_split(image_datasets[‘train’],[train_dataset_len//2, train_dataset_len-dataset_len//2] )
Creating DataLoader objs for splits so that splits can be used for training, validation and testing
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=100,
shuffle=True, num_workers=3) for x in [‘train’, ‘val’,  ‘test’]}