【问题标题】:Change dimensions of image when creating custom dataloader in pytorch在 pytorch 中创建自定义数据加载器时更改图像的尺寸
【发布时间】:2019-01-17 01:08:48
【问题描述】:

我在 CIFAR10 数据集上训练我的 CNN。我提取了 50,000 张尺寸 (32 x 32 x 3) 的图像并在列表中读取它们。我将它们转换为 numpy 数组并将它们存储在一个列表中。我对标签进行了同样的训练和测试。

然后我在 pytorch 中构建了两层和一个 FC 的 CNN。在此之前,我创建了自己的自定义数据加载器。这样做时,我输入的图像的尺寸正在发生变化。尺寸 (32 x 32 x 3) 更改为 (3 x 32 x 32),我无法训练我的神经网络。

tensor_x = torch.stack([torch.Tensor(i) for i in train_images])
tensor_y = torch.stack([torch.Tensor(i) for i in train_labels])
dataset = data_utils.TensorDataset(tensor_x , tensor_y)
train_dataloader = data_utils.DataLoader(dataset=dataset)

tensor_x = torch.stack([torch.Tensor(i) for i in test_images])
tensor_y = torch.stack([torch.Tensor(i) for i in test_labels])
dataset = data_utils.TensorDataset(tensor_x , tensor_y)
test_dataloader = data_utils.DataLoader(dataset=dataset)

RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[1, 32, 32, 3] to have 3 channels, but got 32 channels instead

【问题讨论】:

  • 更新:torchvision.transforms.ToTensor 解决了我的问题

标签: python machine-learning deep-learning conv-neural-network pytorch


【解决方案1】:

在 PyTorch 图像中,通道是第一位的,因此您的图像应该是 3, 32, 32 而不是 32, 32, 3

如果image 是一个numpy 数组,那么你可以这样做

image = image.transpose((2, 0, 1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2019-12-30
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多