Sorting or Grouping Images in the DataLoader

The Data Loader in PyTorch generally does not give the images in sorted fashion.

For a certain task, I have some images with the same image ID and I wish to group these images for a function that I wish to create.

How can I modify the data loader such that the images are sorted (or grouped) according to the image ID?

It would also be helpful to know more on how I can access individual files through the data loader as I find difficulty in accessing images individually.

Thanks in advance!

You could write a custom sampler by deriving from the base class from here and apply your custom sampling logic.
The sampler will yield the indices, which will be passed to the Dataset.__getitem__, so you could sort these indices to make sure that the right images are loaded.

1 Like