使用 Pytorch 显示每个类的图像数量

时间:2021-01-01 10:36:12

标签: pytorch tensor pytorch-dataloader

我将 Pytorch 与 FashionMNIST 数据集一起使用,我想显示来自 10 个类中的每个类的 8 个图像样本。但是,我没有弄清楚如何将训练测试拆分为 train_labels,因为我需要在标签(类)上循环并打印每个类的 8 个。 知道我如何实现这一目标吗?

classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot')

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                              #  transforms.Lambda(lambda x: x.repeat(3,1,1)),
                                transforms.Normalize((0.5, ), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True)
# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=True)


print('Training set size:', len(trainset))
print('Test set size:',len(testset))

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您想按标签对数据集进行分组,然后显示它们。

您可以先构建一个字典来按标签存储示例:

examples = {i: [] for i in range(len(classes))}

然后遍历训练集并使用标签的索引附加到列表中:

for x, i in trainset:
    examples[i].append(x)

然而,这将贯穿整个系列。如果您想提前停止并避免每个班级超过 8,您可以通过添加条件来实现:

n_examples = 8
for x, i in trainset:
    if all([len(ex) == n_examples for ex in examples.values()])
        break
    if len(examples[i]) < n_examples:
        examples[i].append(x)

剩下的就是用 torchvision.transforms.ToPILImage 显示:

transforms.ToPILImage()(examples[3][0])

如果您想显示多个,您可以使用两个连续的 torch.cat,一个在 dim=1(按行)然后在 dim=2(按列)创建网格。

grid = torch.cat([torch.cat(examples[i], dim=1) for i in range(len(classes))], dim=2)
transforms.ToPILImage()(grid)

可能的结果:

enter image description here