当前位置:网站首页>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]
边栏推荐
猜你喜欢

左程云 递归+动态规划

Redis Getting started tutoriel complet: positionnement et optimisation des problèmes

A complete tutorial for getting started with redis: AOF persistence

Digital scrolling increases effect

Redis introduction complete tutorial: replication principle

软件测试——Jmeter接口测试之常用断言

哈希表及完整注释

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

Leetcode 77: combination

一文读懂Faster RCNN
随机推荐
6-6 vulnerability exploitation SSH security defense
LeetCode 77:组合
[2022 national tournament simulation] polygon - computational geometry, binary answer, multiplication
dotConnect for DB2数据提供者
Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
Qt蓝牙:QBluetoothDeviceInfo
wzoi 1~200
How to write test cases for test coupons?
Introduction to ins/gps integrated navigation type
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
MySQL
Redis introduction complete tutorial: client case analysis
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
The 8 element positioning methods of selenium that you have to know are simple and practical
Web3's need for law
基于ensp防火墙双击热备二层网络规划与设计
Redis getting started complete tutorial: common exceptions on the client
Redis入门完整教程:客户端管理
widerperson数据集转化为YOLO格式
Safety delivery engineer