当前位置:网站首页>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]
边栏推荐
- Examples of how to use dates in Oracle
- wireshark安装
- How to design interface test cases? Teach you a few tips to draft easily
- ODBC database connection of MFC windows programming [147] (with source code)
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
- Linear list --- circular linked list
- 惯导标定国内外研究现状小结(删减版)
- MySQL提升大量数据查询效率的优化神器
- 哈希表及完整注释
- 【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
猜你喜欢

记一次JAP查询导致OOM的问题分析

Five reasons for clothing enterprises to deploy MES management system
![[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication](/img/09/b9d50f7a10e6898ac4088ee005d00b.png)
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication

uniapp的表单验证

Go swagger use

数字滚动增加效果

电气工程及其自动化
MySQL is an optimization artifact to improve the efficiency of massive data query

你不可不知道的Selenium 8种元素定位方法,简单且实用

MES管理系统的应用和好处有哪些
随机推荐
Classify the features of pictures with full connection +softmax
Redis入门完整教程:复制原理
Rethinking of investment
你不可不知道的Selenium 8种元素定位方法,简单且实用
测试优惠券要怎么写测试用例?
Derivative, partial derivative, directional derivative
KYSL 海康摄像头 8247 h9 isapi测试
PCL 常用拟合模型及使用方法
实施MES管理系统时,哪些管理点是需要注意的
Huitong programming introductory course - 2A breakthrough
widerperson数据集转化为YOLO格式
HAVE FUN | “飞船计划”活动最新进展
Go swagger use
What are the applications and benefits of MES management system
MATLB|具有储能的经济调度及机会约束和鲁棒优化
数字滚动增加效果
[socket] ① overview of socket technology
Unity custom webgl packaging template
写作系列之contribution
Redis入門完整教程:問題定比特與優化