You can set the class index manually, if you are creating the dataset.
If you are using a dataset from torchvision, such as MNIST, the classes would be already predefined.
For ImageFolder, the specified folders will be sorted and the class indices will be created according to the sorted folders starting at 0.