In case you are creating the targets in the __init__ method or are passing them to the Datset, i.e. they are pre-calculated and not lazily computed, you could apply the same approach as in your posted link by filtering out the .targets as well as the corresponding .img_list or by using a Subset.