Is it safe to assume labels will be the same for two different directories using datasets.ImageFolder

I have images dataset in two different directories Train_images and Test_images. Both directories are organized the same way where images are inside another directory with their associated label.

I am loading the images into train_dataset = datasets.ImageFolder(root='path/to/dataset/Train_images') and test_dataset = datasets.ImageFolder(root='path/to/dataset/Test_images'). I wanted to know if ImageFolder sort the directories before loading them for consistency or does some smart way for making sure that labels will be the same from the two different folders. If this is not the case, would you help me understand how the function ImageFolder work.

ImageFolder will call into find_classes to create the class labels and will sort the folders. On the same system and OS you should see the same classes. However, I think I’ve seen differences in sorting between e.g. Linux and Windows, but don’t know if this was an artifact of older Python versions.

Thanks for your reply!