ImageFolder will create targets using class indices. They won’t be in a one-hot encoded format, so the shape of the target won’t give you the number of classes.
However, this code should work:
n_classes = len(dataset.classes)