Pytorch data.random_split() doesn't split randomly
I am trying to split my custom dataset randomly into test and train. The code runs and outputs the test and train folders successfully, but I need the test and train sets to be different each time I ran the code. Isn't this what randomly splitting supposed to mean/do?
p.s. just for clarification, the data is images, so I expect to see different images chosen for test and train sets each time I execute the code.
#Set the random seeds for reproducibility
SEED = 1234

random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

TRAIN_RATIO = 0.9

data_dir = 'Data Set 1'
images_dir = os.path.join(data_dir, 'images')
train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'test')

if os.path.exists(train_dir):
    shutil.rmtree(train_dir)
if os.path.exists(test_dir):
    shutil.rmtree(test_dir)

os.makedirs(train_dir)
os.makedirs(test_dir)

classes = os.listdir(images_dir)

for c in classes:

    class_dir = os.path.join(images_dir, c)

    images = os.listdir(class_dir)

    n_train = int(len(images) * TRAIN_RATIO)
    n_test = len(images) - n_train
    train_images, test_images = data.random_split(images,
                                               [n_train, n_test])

    os.makedirs(os.path.join(train_dir, c), exist_ok=True)
    os.makedirs(os.path.join(test_dir, c), exist_ok=True)

    for image in train_images:
        image_src = os.path.join(class_dir, image)
        image_dst = os.path.join(train_dir, c, image)
        shutil.copyfile(image_src, image_dst)

    for image in test_images:
        image_src = os.path.join(class_dir, image)
        image_dst = os.path.join(test_dir, c, image)
        shutil.copyfile(image_src, image_dst)







It's not random because you set the random seed. Think of the seed as a random number - if you define the seed, the number is not random anymore. If you don't define it, you will get a random seed every time.
Just comment out these lines :)
SEED = 1234

random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)

Alternatively, just do this: SEED = random.randint(1, 1000) to get a random number between 1 and 1000. This will let you print the value of SEED, if you need that for some reason.
You can see how the seed function works here:
https://www.w3schools.com/python/ref_random_seed.asp
