当前位置:网站首页>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
边栏推荐
猜你喜欢
Encrypted compressed backup bat script
J-link v9 使用技巧之虚拟串口功能
Counting garlic customers: Sudoku (DFS)
Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022
2022 melting welding and thermal cutting operation certificate examination question simulation examination platform operation
Chris LATTNER, father of llvm: the golden age of compilers
Error in face detection and signature of Tencent cloud interface
W801/W800/W806唯一ID/CPUID/FLASHID
Performance introduction of the first new version of cdr2022
Vscode + PHP debug + namespace guidelines
随机推荐
Abused "architect"!
Jerry's AI server [chapter]
Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)
力扣(LeetCode)112. 路径总和(2022.04.22)
The most understandable life cycle of dependency injection
使用单元测试框架编写单元测试的好处?
Counting garlic guest: the solution of the equation
2n queen problem
计蒜客家谱(dfs求直系后代数)
The most easy to understand service container and scope of dependency injection
Introduction to gbase8s SQL Engine framework
DFS奇偶性剪枝
After disk D is distributed to Disk C, what should I do if the database recovery hangs? Please answer
Itextsharp infrastructure
Vscode + PHP debug + namespace guidelines
电子采购如何成为供应链中的增值功能?
When should I write unit tests? What is TDD?
Chris LATTNER, father of llvm: the golden age of compilers
数字藏品平台入驻 数字藏品平台开发 数字藏品SaaS平台
What code needs unit testing?