You could create a custom Dataset as explained in this tutorial 54 and apply the transformation on each sample in the __getitem__ of this class. To create the class indices based on the folder structure you could reuse the logic from DatasetFolder 7.