OK, I see.
How are you calculating the train and val indices?
Would it be possible to calculate them before creating the Dataset?
If so, you could create a train and val dataset using the indices, i.e. only images from these indexed paths will be loaded.
If that’s not possible, could you share the code of your Dataset?