How can I Modify pytorch dataset __getitem__ function to return a bag of 10 images?

I have a directory with multiple images separated into folders. Each folder has up to 3000 images. I would like to modify the pytorch dataset getitem function so that it returns bags of images, where each bag contains 10 images.

Here is what I have so far:

transform = transforms.Compose([transforms.Resize(255),
dataset = datasets.ImageFolder('./../BCNB/patches/WSI_1', transform=transform)
data_loader =, batch_size = 1)

My output of DataLoader should be a tensor with a shape of [1, 10, 3, 256, 256].
Any input would be very helpful!

Thank you very much in advance!

I don’t know how these 10 samples should be loaded, but in case you want to pass multiple indices from the sampler to the __getitem__ you could use a BatchSampler as described here.