How to decode labels generated by pytorchvision

I am new on Pytorch and I was wondering how to decode the labels when using pytorchvision and datasets.ImageFolder.

As far I understood that labels are created with the image folder name

I have 43 folders so 43 classes that are loaded the folder structure is the following data_img/train/Classes/image_from_classes.

Here is the code line that i have used to load the images:
train_data = datasets.ImageFolder('data_img/train/', transform=train_transforms)

When the model is calculated and that I try to pass an image and predict the result I was expecting the label name however I have a tensor as a return “Image predicted as tensor([15])”.

I was wondering how to decode this number to get the class name?

Thanks for your help,


You can use class_to_idx attribute of ImageFolder. So train_data.class_to_idx will give you a dictionary with key as class name and id as its value. You can find class names by checking ids within this dictionary.

Thanks I will try that right away.