Did you store the paths for each image in the corresponding .txt file or what do you mean by “class label” in the comments?
If you’ve stored the paths, I would recommend to write a custom Dataset (as described here 30).
You could read all file paths and create the target tensor in a separate tensor in the __init__ method.
Once you have a list of all paths and the target, you could lazily load and process the data in __getitem__.
Also, if you want to use a subet of the data, you could manually e.g. use random_split on the Dataset, use a Subset with your manually created indices or use a SubsetRandomSampler again with your indices.
Let me know, if that would work for you of if you need more information.