当前位置:网站首页>RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
2022-04-23 01:43:00 【Zkaisen】
使用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×500
from 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')
版权声明
本文为[Zkaisen]所创,转载请带上原文链接,感谢
https://blog.csdn.net/fencecat/article/details/124312450
边栏推荐
- 彻底卸载Antidote 10 ?Antidote卸载不干净怎么办?
- 8GBs communication between client and server
- Blocking type of gbase 8s concurrency control
- 关于C4D动画如何导入Lumion
- 2n皇后问题
- 客户端项目管理经常面临的挑战
- Jerry's factors that usually affect CPU performance test results are: [article]
- 使用单元测试框架编写单元测试的好处?
- 计蒜客:等边三角形(DFS)
- Garlic customer: equilateral triangle (DFS)
猜你喜欢

紫光国微财报一枝独秀 2021年净利润三位数增长靠什么

Counting garlic customers: Sudoku (DFS)

Level 4 city area table xlsx, SQL file, domestic, provincial, county, street, township level 4 address in China (name, linkage ID, level, end level or not (1-yes))

领导/老师让填写电子excel表格文档可手机上如何编辑word/excel文件填写excel/word电子文档?

《维C中国》乡村助农暖人心第三站嘉宝果农场

The most easy to understand service container and scope of dependency injection

Leetcode 112 Total path (2022.04.22)

About how to import C4d animation into lumion

The most understandable life cycle of dependency injection

2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
随机推荐
Gbase 8s query processing and optimization
清研环境深交所上市:年营收1.8亿 市值41亿
Abused "architect"!
Server 2019 the available memory of the server is half of the actual memory
Oracle database query lock table SQL script and delete lock information script (necessary for database development ETL and DBA)
mb_substr()、mb_strpos()函数(故事篇)
Jerry's CPU performance test [chapter]
Chapter 6 uses Matplotlib to draw thermodynamic diagram
蒜头君开公司(DFS全排列)
Modify array (and search set)
Leetcode-阶乘函数后 K 个零
Is the stable currency a super opportunity to accelerate the death of the public chain or replace BTC?
New functions of ai2022, introduction to new functions of illustrator 2022
Jerry's factors that usually affect CPU performance test results are: [article]
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
Soatest preliminary understanding
Do447 manage user and team access
DO447管理用户和团队的访问
JSP basic knowledge summary
Self taught programming, don't read theory books foolishly, programmer: it's all left over by others