The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0解决方法

在自学小土堆pytorch的过程中,在transform的章节遇到一个问题

用自己上传的图片进行标准化时,上传的图片由PIL转为Tensor类型时为4维张量,与3维张量不匹配。

报错内容如下

分析原因

定位到错误行16行,里面的参数img_tensor

查看img_tensor的维度,发现其维度是4,与trans_norm中的3维不匹配。

解决问题

img = Image.open("image/image.png")

改为

with open("image/image.png",'rb') as f:
    img = Image.open(f).convert("RGB")

记录一下自己解决问题的过程,如有缺漏,欢迎大佬指正!!

参考文章

Pytorch报错解决:The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio-CSDN博客

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值