在导入一个png文件的时候,发现报这个错,源码如下,经过查询找到了两种解决方案。

from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
import torchwriter = SummaryWriter("logs")
img = Image.open("C:/Users/Mxx/PycharmProjects/pythonProject4/images_set/GOGOGOGO.png")
print(img)
#img = img.convert('RGB')
print(img)trans_totensor = transforms.ToTensor() #首先创建一个对象,来使用ToTensor的方法
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)#img = img.convert('RGB')
#Normalize
print(img_tensor[0][0][0])
print("---------")
print(img_tensor.shape)
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()

1.因为png是四个通道,所以需要在Normalize的时候加一个维度。

trans_norm = transforms.Normalize([0.5,0.5,0.5,0.5],[0.5,0.5,0.5,0.5])

2.讲png转成rgb这样只需要做三个维度的标准化。

img = img.convert('RGB')

将png和RGB以及标准化后的png和RGB进行输出,发现原本的png和RGB差不多。

标准化后的相比于标准化前是变的模糊了,但是标准化后的png和RGB是差不多的。

更改后的test代码如下

from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
import torchwriter = SummaryWriter("logs")
img = Image.open("C:/Users/Mxx/PycharmProjects/pythonProject4/images_set/GOGOGOGO.png")
print(img)
img_RGB = img.convert('RGB')trans_totensor = transforms.ToTensor() #首先创建一个对象,来使用ToTensor的方法
img_tensor = trans_totensor(img)
#rgb 的 tensor
imgRGB_tensor = trans_totensor(img_RGB)writer.add_image("ToTensor",img_tensor)writer.add_image("RGB-ToTensor",imgRGB_tensor)
#img = img.convert('RGB')
#Normalizetrans_norm_png = transforms.Normalize([0.5,0.5,0.5,0.5],[0.5,0.5,0.5,0.5])
img_norm = trans_norm_png(img_tensor)trans_norm_RGB = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
imgRGB_norm = trans_norm_RGB(imgRGB_tensor)writer.add_image("PNG Normalize",img_norm)
writer.add_image("RGB Normalize",imgRGB_norm)
writer.close()

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

  1. 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 解决: ...

  2. 报错解决——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 ...

  3. CUDA编译器nvcc可能遇到:error: asm operand type size(8) does not match type/size implied by constraint ‘r‘

    0. 问题描述 该问题产生于我在ptcharm中试图用cupy加速numpy计算,进行一些简单但是繁琐的数据处理,这个问题和cupy关系不大,cupy只要照着nvcc -v给出的版本号对应着下载就ok ...

  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. pytorch dataload 出错:: 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 ...

  8. Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.

    使用Pytorch_Geometric(PyG)时构建DataLoader,从DataLoader获取样本Batch时报错:RuntimeError: Sizes of tensors must ma ...

  9. 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 ...

最新文章

  1. pythoning ——3、数据类型(字符串)
  2. Android -- 再来一发Intent
  3. 鼠标右键转圈圈_win10系统右键一直转圈
  4. gradle入门_Gradle入门:简介
  5. linux nifi指定jdk,nifi在arm架构启动不了,怎么办?
  6. python io操作有什么_Python笔记:文件IO操作
  7. Nginx伪静态配置和常用Rewrite伪静态规则集锦
  8. php无限评论回复_php如何制作无限级评论功能?
  9. 如何用命令获知当前是一年中的第多少周和今天是周几
  10. dw中创建java程序_新建MainGame.java并创建窗口
  11. linux如何关闭oracle数据库,linux关闭oracle数据库命令
  12. lintcode算法-3 983 · 棒球游戏
  13. 关于使用RedisTemplate在主从架构下使用Lettuce的情况下如何实现读写分离
  14. html复杂表格,横向多级表头,纵向多级表头,合并行或列
  15. 组态王中时间存access怎么存_组态王数据保存
  16. 【0基础入门Pytorch】Pytorch的简介与安装(Windows)
  17. word模板填充数据
  18. 为什么气象站和 AI 都测不准天气?
  19. 一句话木马原理与实战
  20. go.path/filepath — 兼容操作系统的文件路径操作

热门文章

  1. 微信公众号文章信息(阅读量、在看、点赞数)获取
  2. GWAS数据分析流程—SNP、Indel提取
  3. Mybatis学习小结
  4. python3 简单爬虫实战|使用selenium来模拟浏览器抓取选股宝网站信息里面的股票
  5. dva介绍--Dva概念(二)
  6. DCMTK之MPPS服务实现
  7. java动态代理三座大山InvocationHandler、newProxyInstance()和invoke()
  8. TCP/IP网络编程 - 基础学习
  9. Oracle数据库:oracle外连接left/right/full outer join on,oracle扩展的左右外连接展示符号(+)
  10. MATLAB 级数计算