当前位置:网站首页>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]
边栏推荐
- Code line breaking problem of untiy text box
- Niuke programming problem -- double pointer of 101 must be brushed
- Redis入门完整教程:客户端常见异常
- Leetcode 77: combination
- 数论 --- 快速幂、快速幂求逆元
- PSINS中19维组合导航模块sinsgps详解(滤波部分)
- Error in fasterxml tostringserializerbase
- 一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
- Go swagger use
- MySQL is an optimization artifact to improve the efficiency of massive data query
猜你喜欢

wireshark安装

Cloud Mail .NET Edition

uniapp的表单验证

Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey

How to write test cases for test coupons?

实施MES管理系统时,哪些管理点是需要注意的

The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?

Have fun | latest progress of "spacecraft program" activities

Redis入门完整教程:AOF持久化

Redis入门完整教程:复制原理
随机推荐
Qt蓝牙:QBluetoothDeviceInfo
Five reasons for clothing enterprises to deploy MES management system
写作系列之contribution
Redis入门完整教程:客户端案例分析
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
Hash table and full comments
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
牛客编程题--必刷101之双指针篇
Go swagger use
What management points should be paid attention to when implementing MES management system
安全交付工程师
Install redis from zero
你不可不知道的Selenium 8种元素定位方法,简单且实用
MySQL提升大量数据查询效率的优化神器
Pioneer of Web3: virtual human
Fundamentals of process management
哈希表及完整注释
Redis入门完整教程:客户端管理
[Mori city] random talk on GIS data (II)