当前位置:网站首页>How-PIL-to-Tensor
How-PIL-to-Tensor
2022-07-06 19:22:00 【红烧code】
图片是如何从 N u m P y NumPy NumPy形式向 T e n s o r Tensor Tensor形式转换的呢?
from PIL import Image
import numpy as np
from torchvision.transforms import ToTensor
img=Image.open('./sword.png')
img_arr=np.array(img)
img_tensor=ToTensor().__call__(img)
N u m P y NumPy NumPy的图片格式 H , W , C H,W,C H,W,C,即 h e i g h t , w i d t h , c h a n n e l s height,width,channels height,width,channels(高、宽、通道数)
img_arr.shape
(73, 183, 4)
T e n s o r Tensor Tensor图片格式为 C , H , W C,H,W C,H,W,即 c h a n n e l s , h e i g h t , w i d t h channels,height,width channels,height,width(通道数、高、宽)
img_tensor.shape
torch.Size([4, 73, 183])
实际上将 N u m P y NumPy NumPy数组每个位置上对应的像素值除以 255 255 255就得到了对应的 T e n s o r Tensor Tensor数据类型
# 取出图像左上角第一个点的RGBA值
print(img_arr[0, 0, :])
print(img_tensor[:, 0, 0])
[ 35 33 47 255]
tensor([0.1373, 0.1294, 0.1843, 1.0000])
# 验证, 完全正确
print(255*img_tensor[:, 0, 0])
print(img_arr[0, 0, :])
tensor([ 35., 33., 47., 255.])
[ 35 33 47 255]
边栏推荐
- 服装企业部署MES管理系统的五个原因
- Unity使用MaskableGraphic画一条带箭头的线
- Code line breaking problem of untiy text box
- Introduction to ins/gps integrated navigation type
- 6-6漏洞利用-SSH安全防御
- The panel floating with the mouse in unity can adapt to the size of text content
- Common fitting models and application methods of PCL
- What are the applications and benefits of MES management system
- MySQL
- dotConnect for DB2数据提供者
猜你喜欢
左程云 递归+动态规划
6-6 vulnerability exploitation SSH security defense
[socket] ① overview of socket technology
【2022国赛模拟】多边形——计算几何、二分答案、倍增
Redis入門完整教程:問題定比特與優化
Read fast RCNN in one article
Cloud Mail .NET Edition
Web3's need for law
What management points should be paid attention to when implementing MES management system
进程管理基础
随机推荐
fasterxml ToStringSerializerBase报错
Contribution of Writing Series
Derivative, partial derivative, directional derivative
Fundamentals of process management
AWS learning notes (I)
用全连接+softmax对图片的feature进行分类
Cloud Mail . NET Edition
CSDN summer camp course project analysis
[leetcode]Search for a Range
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
MySQL
Mmdetection3d loads millimeter wave radar data
实施MES管理系统时,哪些管理点是需要注意的
Dotconnect for DB2 Data Provider
你不可不知道的Selenium 8种元素定位方法,简单且实用
Code debugging core step memory
电气工程及其自动化
【2022国赛模拟】多边形——计算几何、二分答案、倍增
S120驱动器基本调试步骤总结