How to get the class names to class label mapping

I am using ResNet-18 for classification purpose. I have used dataloader to load the data. But how get the label to class name mapping? Does it load in alphabetical order?

1 Like

For future reference:

I have used this from to find the mapping-

def find_classes(dir):
    classes = os.listdir(dir)
    class_to_idx = {classes[i]: i for i in range(len(classes))}
    return classes, class_to_idx

@avijit_dasgupta hi, I have the same problem. Can you provide a class_idx to class name mapping? That would be super convenient.

Something like this:

dataset = datasets.ImageFolder(data_folder, transform=data_transforms)

For idx to classes mappping, just get list of keys from dict returned from dataset.class_to_idx

1 Like