I don’t know how your custom Dataset works exactly as it seems a custom sampler would be needed, since __getitem__ expects two index values? If that’s the case, how many images are you loading? ImageFolder would load a single sample in the __getitem__ and I don’t know how you let it load two images so what’s the baseline you are comparing against?
To get some speedup you could use tensor = torch.from_numpy(arr) to share the underlying data instead of creating a copy via tensor = torch.tensor(arr).



 Asya:

and one more problem I have is that I can’t use big batch size as it doesn’t fit in GPU memory despite that I do del my_vars and torch.cuda.empty_cache()


I don’t know what my_vars is, but you might need to reduce the batch size to be able to train your model. Deleting unused tensors would reduce the memory requirement, but calling empty_cache() would slow down your code without avoiding the OOM issue.