当前位置:网站首页>How-PIL-to-Tensor
How-PIL-to-Tensor
2022-07-07 03:04:00 【Braised code】
How is the picture from N u m P y NumPy NumPy Form orientation T e n s o r Tensor Tensor What about form conversion ?

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 Picture format for H , W , C H,W,C H,W,C, namely 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( high 、 wide 、 The channel number )
img_arr.shape
(73, 183, 4)
T e n s o r Tensor Tensor The format of the picture is C , H , W C,H,W C,H,W, namely 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( The channel number 、 high 、 wide )
img_tensor.shape
torch.Size([4, 73, 183])
Actually N u m P y NumPy NumPy Divide the corresponding pixel value at each position of the array by 255 255 255 You get the corresponding T e n s o r Tensor Tensor data type
# Take out the first point in the upper left corner of the image RGBA value
print(img_arr[0, 0, :])
print(img_tensor[:, 0, 0])
[ 35 33 47 255]
tensor([0.1373, 0.1294, 0.1843, 1.0000])
# verification , Absolutely right
print(255*img_tensor[:, 0, 0])
print(img_arr[0, 0, :])
tensor([ 35., 33., 47., 255.])
[ 35 33 47 255]
边栏推荐
- Five reasons for clothing enterprises to deploy MES management system
- Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
- 从零安装Redis
- Django数据库(SQlite)基本入门使用教程
- Es6中Promise的使用
- Redis getting started complete tutorial: common exceptions on the client
- 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?
- Redis getting started complete tutorial: replication topology
- Left path cloud recursion + dynamic planning
- 基于ensp防火墙双击热备二层网络规划与设计
猜你喜欢

【Socket】①Socket技术概述
![[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication](/img/09/b9d50f7a10e6898ac4088ee005d00b.png)
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication

【2022国赛模拟】多边形——计算几何、二分答案、倍增

Redis入门完整教程:复制原理

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

哈希表及完整注释

How to write test cases for test coupons?

Redis入门完整教程:复制拓扑

The panel floating with the mouse in unity can adapt to the size of text content

服装企业部署MES管理系统的五个原因
随机推荐
Redis入门完整教程:复制拓扑
代码调试core-踩内存
Kubernetes源码分析(二)----资源Resource
A complete tutorial for getting started with redis: AOF persistence
Code debugging core step memory
Unity使用MaskableGraphic画一条带箭头的线
CSDN summer camp course project analysis
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
Fundamentals of process management
你不可不知道的Selenium 8种元素定位方法,简单且实用
导数、偏导数、方向导数
Error in fasterxml tostringserializerbase
MMDetection3D加载毫米波雷达数据
Redis introduction complete tutorial: replication principle
左程云 递归+动态规划
Unity custom webgl packaging template
Hash table and full comments
uniapp适配问题
The panel floating with the mouse in unity can adapt to the size of text content
Electrical engineering and automation