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 https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py to find the mapping-

def find_classes(dir):
    classes = os.listdir(dir)
    classes.sort()
    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)
print(dataset.class_to_idx)
8 Likes

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

1 Like