PyTorch ImageFolder:文件夹获取哪个索引?

PyTorch ImageFolder: Which Index Does a Folder Get?

我想创建一个如图所示的混淆矩阵 , though I use a different dataset than FashinMNIST. Concretely, I want the axes to contain names such as "banana", "orange", "cucumber", etc. I have a folder where images of bananas, etc. are stored, which I load with PyTorch's ImageFolder class。现在,PyTorch 自动将数字 09 分配给我的 classes(我总共有 10 个 classes),但我不确定标签“香蕉”总是得到相同的数字 0.

如何在使用 ImageFolder class 时找出哪个索引属于哪个标签(子文件夹名称)?

Class ImageFolder 具有属性 class_to_idx。检查 docs.

x = torchvision.datasets.ImageFolder(root=path, transform=transform)
print(x.class_to_idx)