使用transfroms方法过程报错:

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

代码块:

from PIL import Image
#from torchvision import transforms
#首先导入一张图片
img = Image.open("images/pytorch.png")
print(img)#运行一下看图片有没有正常读取
#它是PIL的图片类型,图片大小是955×500from torchvision import transforms
#首先看compose类,就是把不同的transforms结合在一起的#小课堂
#python中__call__的用法# transforms的compose方法既可以通过内置函数调用也可以通过__call__来调用
#常用一个类对象接一个括号()的形式调用#ToTensor
#将一个PIL或者numpy的图像转化成tensor
from torch.utils.tensorboard import SummaryWriter#从tensorboard引入了SummaryWriter
writer = SummaryWriter("logs")
img = Image.open("images/pytorch.png")
print(img)trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
# writer.close()##Normalization归一化
# 能够归一化一个tnesor的图像,随着他的平均值或者标准差
# 给一个均值和标准差看维度
# 能够将每一个信道的值归一化# 归一化的计算公式
# output[channel] = (input[channel] - mean[channel]) / std[channel]`
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("normalize",img_norm)
writer.close()

报错后定位到第40行代码:

img_norm = trans_norm(img_tensor)

原因分析:

输入图像有一个 alpha 通道的像素,因此它是四通道而不是是三通道的图像

验证猜想

解决方法:

将四通道RGBA图像转换成三通道的RGB图像

img = Image.open("images/pytorch.png").convert('RGB')

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio相关推荐

  1. 报错解决——RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton

    问题描述 RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dim ...

  2. The size of tensor a (4) must match the size of tensor b (3) at non-singletonThe size of

    报错: RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton 解决: ...

  3. The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 维度不匹配

    在导入一个png文件的时候,发现报这个错,源码如下,经过查询找到了两种解决方案. from PIL import Image from torchvision import transforms fr ...

  4. RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0—问题分析

    声明: 这里仅仅提供一个思路,暂时没有绝对正确通用的解决方案 作者:{ 墨理 } 感谢通过私信.评论交流,协力探讨解决问题的同学 UP .Penser 文章目录 探讨的 pytorch 报错信息如下 ...

  5. PyTorch 笔记(11)— Tensor内部存储结构(头信息区 Tensor,存储区 Storage)

    1. Tensor 内部存储结构 tensor 数据结构如下图所示,tensor 分为头信息区(Tensor)和存储区 (Storage),信息区主要保存着 Tensor 的形状(size).步长(s ...

  6. python中tensor与variable_NLP实战篇之tf2中tensor、variable、gradient、ops

    本文是基于tensorflow2.2.0版本,介绍了tf中变量.张量的概念,tf中梯度的计算方式和tensor相关的操作. 实战系列篇章中主要会分享,解决实际问题时的过程.遇到的问题或者使用的工具等等 ...

  7. Kernel size can‘t be greater than actual input size的解决方案

    报错信息:RuntimeError: Calculated padded input size per channel: (49). Kernel size: (64). Kernel size ca ...

  8. Pytorch踩坑: RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0.

    报错信息: RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 186 a ...

  9. python size和count_groupby 的妙用(注意size和count)

    Pandas的groupby()功能很强大,用好了可以方便的解决很多问题,在数据处理以及日常工作中经常能施展拳脚. 今天,我们一起来领略下groupby()的魅力吧. 首先,引入相关package: ...

  10. entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit

    webpack打包提示文件体积过大导致 The following asset(s) exceed the recommended size limit (244 KiB). This can imp ...

最新文章

  1. 原本要与Hinton当同事,最后被迫Bengio门下读博? | 独立研究员的坎坷之路
  2. LVS+keepalived构建PXC高可用集群
  3. MySQL 关于性能的参数配置梳理
  4. redhat配置dns服务器bind
  5. 基于 Quartz 开发企业级任务调度应用
  6. 2 shell 锂基脂_壬二酸和癸二酸制备的复合锂基脂到底有那些差别!
  7. rust面向对象_面向初学者的Rust操作员综合教程
  8. C++(0)--mac 使用VScode 进行C++编译、运行、调试
  9. GridView 批量删除,自定义分页,定位页码
  10. web后端语言_web前端学习路线图_快速入门web前端学习路线图
  11. python中的struct
  12. Atitit 数据库 负载均衡 方法总结 目录 1. 对称模型负载均衡 vs 非对称模型 2 1.1. 业务分离法 2 1.2. App + db分布式分离法 2 2. 负载均衡算法 2 2.1.
  13. Linux之内存管理mm_struct
  14. GLASS数据批量下载
  15. 用css做一个3d相册
  16. matlab figure 虚线圆,flutter 画圆,虚线圆,渐变圆
  17. pyqt QLineEdit 详细用法
  18. UR机器人TCP通讯示例
  19. javase笔记基础篇
  20. stata foreach循环语句的使用

热门文章

  1. win10 ISO文件下载
  2. 郭盛华动真格了!新公司获百亿融资,网友:还招人不
  3. Python分析上证指数历史数据,发现估值还不够低……
  4. 基金与私募基金概念解析:共同基金、单位信托、投资信托计划、券商集合理财、基金专户理财
  5. php 字典树,关于PHP字典树的定义与实现方法
  6. Win10 提示账户名与安全标识间无任何映射完成
  7. 通过spi调试linux应用程序,654123??SPI linux 驱动调试感悟
  8. AWS Device Shadow使用
  9. Boxy SVG for Mac(矢量图编辑器)
  10. Racket读写JSON