简体   繁体   English

Pytorch RuntimeError:张量 a (4) 的大小必须与非单维 0 处的张量 b (3) 的大小相匹配

[英]Pytorch RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

I use code from here to train a model to predict printed style number from 0 to 9 :我使用此处的代码来训练 model 以预测从09的打印样式编号:

idx_to_class = {0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7:"7", 8: "8", 9:"9"}
def predict(model, test_image_name):

    transform = image_transforms['test']

    test_image = Image.open(test_image_name)
    plt.imshow(test_image)

    test_image_tensor = transform(test_image)

    if torch.cuda.is_available():
        test_image_tensor = test_image_tensor.view(1, 3, 224, 224).cuda()
    else:
        test_image_tensor = test_image_tensor.view(1, 3, 224, 224)

    with torch.no_grad():
        model.eval()
        # Model outputs log probabilities
        out = model(test_image_tensor)
        ps = torch.exp(out)
        topk, topclass = ps.topk(1, dim=1)
        # print(topclass.cpu().numpy()[0][0])
        print("Image class:  ", idx_to_class[topclass.cpu().numpy()[0][0]])

predict(model, "path_of_test_image")

But I get an error when try to use predict :但是尝试使用predict时出现错误:

Traceback (most recent call last):

  File "<ipython-input-12-f8636d3ba083>", line 26, in <module>
    predict(model, "/home/x/文档/Deep_Learning/pytorch/MNIST/test/2/QQ截图20191022093955.png")

  File "<ipython-input-12-f8636d3ba083>", line 9, in predict
    test_image_tensor = transform(test_image)

  File "/home/x/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)

  File "/home/x/.local/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 166, in __call__
    return F.normalize(tensor, self.mean, self.std, self.inplace)

  File "/home/x/.local/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 217, in normalize
    tensor.sub_(mean[:, None, None]).div_(std[:, None, None])

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

How could I fix it?我该如何解决? Thanks.谢谢。

I suspect your test_image has an additional alpha channel per pixel, thus it has 4 channels instead of only three.我怀疑您的test_image每个像素有一个额外的 alpha 通道,因此它有 4 个通道而不是只有 3 个。
Try:尝试:

test_image = Image.open(test_image_name).convert('RGB')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyTorch:RuntimeError:张量 a (224) 的大小必须与非单维 3 的张量 b (244) 的大小相匹配 - PyTorch: RuntimeError: The size of tensor a (224) must match the size of tensor b (244) at non-singleton dimension 3 RuntimeError:张量 a (128) 的大小必须与非单维 3 的张量 b (256) 的大小相匹配 - RuntimeError: The size of tensor a (128) must match the size of tensor b (256) at non-singleton dimension 3 RuntimeError:张量 a (4000) 的大小必须与非单维 1 的张量 b (512) 的大小相匹配 - RuntimeError: The size of tensor a (4000) must match the size of tensor b (512) at non-singleton dimension 1 对于 SNN,如何解决`RuntimeError:张量 a (3) 的大小必须与非单维 1 的张量 b (128) 的大小相匹配? - How to resolve `RuntimeError: The size of tensor a (3) must match the size of tensor b (128) at non-singleton dimension 1` for SNN? RuntimeError:张量 a (38) 的大小必须与非单维 3 处的张量 b (34) 的大小相匹配 - RuntimeError: The size of tensor a (38) must match the size of tensor b (34) at non-singleton dimension 3 运行时错误:张量 a (1024) 的大小必须与非单维 3 处的张量 b (512) 的大小匹配 - RuntimeError: The size of tensor a (1024) must match the size of tensor b (512) at non-singleton dimension 3 训练 CNN 时出错:“RuntimeError:张量 a (10) 的大小必须与非单维 1 的张量 b (64) 的大小相匹配” - Error when training CNN: "RuntimeError: The size of tensor a (10) must match the size of tensor b (64) at non-singleton dimension 1" Pytorch 迁移学习错误:张量 a (16) 的大小必须与非单维 2 的张量 b (128) 的大小匹配 - Pytorch transfer learning error: The size of tensor a (16) must match the size of tensor b (128) at non-singleton dimension 2 对于 pytorch 中的 pad_sequence(),张量 a (20) 的大小必须与非单维 1 处的张量 b (25) 的大小相匹配 - The size of tensor a (20) must match the size of tensor b (25) at non-singleton dimension 1 for pad_sequence() in pytorch 张量 a (707) 的大小必须与非单维 1 处的张量 b (512) 的大小相匹配 - The size of tensor a (707) must match the size of tensor b (512) at non-singleton dimension 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM