当前位置:网站首页>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:45:00 【Zkaisen】
Use transfroms Method process error :
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0
Code block :
from PIL import Image
#from torchvision import transforms
# First import a picture
img = Image.open("images/pytorch.png")
print(img)# Run it to see if the picture is read normally
# It is PIL Type of picture , The picture size is 955×500
from torchvision import transforms
# First of all to see compose class , Is to put different transforms Combined with
# Small class
#python in __call__ Usage of
# transforms Of compose Methods can be called either through built-in functions or through __call__ To call
# A class object is often followed by a bracket () Form call of
#ToTensor
# Will a PIL perhaps numpy Convert image into tensor
from torch.utils.tensorboard import SummaryWriter# from tensorboard Introduced 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 normalization
# Can normalize a tnesor Image , With his mean or standard deviation
# Give a mean and standard deviation dimension
# The value of each channel can be normalized
# Normalization formula
# 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()
After the error is reported, locate to the 40 Line code :
img_norm = trans_norm(img_tensor)
Cause analysis :
The input image has a alpha Pixels of the channel , So it's a four channel image instead of a three channel image
Verify the conjecture 
resolvent :
Connect four channels RGBA The image is converted into a three channel RGB Images
img = Image.open("images/pytorch.png").convert('RGB')
版权声明
本文为[Zkaisen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230143230402.html
边栏推荐
- Itextsharp page setup
- DFS parity pruning
- Learning methods and career development guide (2022 Edition)
- LSF的使用方法总结
- Rôles attributs personnels Abréviations
- Chapter 6 uses Matplotlib to draw thermodynamic diagram
- 电子采购如何成为供应链中的增值功能?
- The most easy to understand service container and scope of dependency injection
- NPM -- configure Taobao image
- Google developer tool preserve log
猜你喜欢
Redis implements distributed locks
如何“优雅”的测量系统性能
leetcode771. Gemstones and stones
FL studio20. 8 the latest Chinese version installation and download graphic tutorial
王子救公主(DFS)
The most easy to understand service container and scope of dependency injection
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimensio
Futr3d: a unified 3D detection framework for sensor fusion
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
Summary of LSF usage
随机推荐
Blocking granularity of gbase 8s concurrency control
Modify array (and search set)
Vscode + PHP debug + namespace guidelines
mb_ substr()、mb_ Strpos() function (story)
紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
Detonate the bomb (DFS)
2n queen problem
NR polar code 七- SCL(succesive cancellation list decoding)
JSP基础知识总结
(product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip
The most understandable life cycle of dependency injection
Find number (DFS)
UVC camera encapsulation class
Completely uninstall antidote 10? What if the antidote uninstall is not clean?
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
JSP basic knowledge summary
Server 2019 the available memory of the server is half of the actual memory
2022.4.10-----leetcode.804
什么是布尔类型?
第六章 使用 matplotlib 绘制热力图