当前位置:网站首页>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]
边栏推荐
- Redis入门完整教程:问题定位与优化
- 左程云 递归+动态规划
- Redis入门完整教程:AOF持久化
- A complete tutorial for getting started with redis: problem location and optimization
- Django database (SQLite) basic introductory tutorial
- 商城商品的知识图谱构建
- QT common Concepts-1
- 数论 --- 快速幂、快速幂求逆元
- 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?
- 知识图谱构建全流程
猜你喜欢
随机推荐
实施MES管理系统时,哪些管理点是需要注意的
MySQL --- 常用函数 - 字符串函数
QT common Concepts-1
Digital scrolling increases effect
Install redis from zero
Leetcode:minimum_ depth_ of_ binary_ Tree solutions
fasterxml ToStringSerializerBase报错
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
Use of promise in ES6
导数、偏导数、方向导数
如何分析粉丝兴趣?
How to design interface test cases? Teach you a few tips to draft easily
MySQL is an optimization artifact to improve the efficiency of massive data query
Error in fasterxml tostringserializerbase
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
Derivative, partial derivative, directional derivative
Code line breaking problem of untiy text box
The 8 element positioning methods of selenium that you have to know are simple and practical
Redis getting started complete tutorial: replication topology
基于ensp防火墙双击热备二层网络规划与设计








