Shuffling 2 downloaders in the same way

Good afternoon!
I’m building a DL model in PyTorch, based on 2 types of input - images and the corresponding numerical coordinates of 50 landmarks on the image. Both types of input have labels 0 or 1.
The file names are aligned: image name is filename1.png and the corresponding landmarks filename1.txt
I need to shuffle both images and landmarks in unison to keep the labels aligned. What is the best way to do it?
I tried importing the data in NumPy arrays first, then turning the arrays into torch tensors and then turning the tensors into dataloaders, however, this approach corrupts the tensors’ shape and makes a mess.

The cleanest approach would be to create a custom Dataset (e.g. as described here) and to load the data sample with its corresponding target in the __getitem__. This would avoid creating two separate DataLoaders and making sure the sampler shuffles both in the same way.

1 Like