PIL+pyplot+transforms.ToTensor+unsqueeze+div
创始人
2024-03-12 21:05:24

目录

  • PIL & pyplot
    • transforms.ToTensor
  • torch.unsqueeze(input,dim,out=None)
  • torch.squeeze(input, dim=None, out=None)
  • torch.div

PIL & pyplot

from PIL import Image
import matplotlib.pyplot as plt
from torchvision import transforms# PIL打开图片
img_PIL = Image.open("data/style/cat.jpg")
print(img_PIL)
print(type(img_PIL))
# img_PIL.show()  # 调用os system自带的browser来显示图片
# plt.imshow(img_PIL)
# plt.show()# plt打开图片
img_plt = plt.imread("data/style/cat.jpg")
print(img_plt.shape)
print(type(img_plt))
# plt.imshow(img_plt)
# plt.show()

在这里插入图片描述
可以看到plt打开的图片格式为 HxWxC 且数据类型为numpy.ndarray,并不是张量形式,所以 plt 打开的图片也要用transforms.ToTensor 来转化为张量。

因为PIL打开的图片并不是张量的形式,所以需要transforms.ToTensor()来转换为张量

transforms.ToTensor

convert a PIL Image or numpy.ndarray to tensor

Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]

pytorch官网

# PIL打开图片
img_PIL = Image.open("data/style/cat.jpg")
img_PIL = transforms.ToTensor()(img_PIL)
print(img_PIL.shape)
# output:torch.Size([3, 940, 940])# plt打开图片
img_plt = plt.imread("data/style/cat.jpg")
img_plt = transforms.ToTensor()(img_plt)
print(img_plt.shape)  # torch.Size([3, 940, 940])
print(type(img_plt))  # 

输出为 (C×H×W)(C \times H\times W)(C×H×W)

或者用transforms.Compose打包transforms.ToTensor()

transform = transforms.Compose([transforms.ToTensor()])# PIL打开图片
img_PIL = Image.open("data/style/cat.jpg")
img_PIL = transform(img_PIL)
print(img_PIL.shape)
# output:torch.Size([3, 940, 940])

torch.unsqueeze(input,dim,out=None)

官网torch.unsqueeze

在指定位置添加维度

# plt打开图片
img_plt = plt.imread("data/style/cat.jpg")
img_plt = transform(img_plt)
print(img_plt.shape)
print(type(img_plt))# 在指定的维度上插入1维
img_plt = img_plt.unsqueeze(2)
print(img_plt.shape)
print(type(img_plt))

在这里插入图片描述
通常的使用方法是img = transform(img).unsqueeze(0)

transform = transforms.Compose([transforms.ToTensor()])
# plt打开图片
img_plt = plt.imread("data/style/cat.jpg")
print(img_PIL.shape)
print(type(img_PIL))
# img_plt = transforms.ToTensor()(img_plt)
img_plt = transform(img_plt)img_plt = img_plt.unsqueeze(0)print(img_plt.shape)
print(type(img_plt))

在这里插入图片描述
unsqueeze(0)就是在最外层增加了一个维度。

unqueeze()这个函数主要是对数据维度进行扩充, 起到升维的作用,方便后续图像处理可以更好地批处理。这是应该是因为网络的接收输入是一个mini-batch,image unsqueeze后第一个维度是留给batch_size的

torch.squeeze(input, dim=None, out=None)

去除那些维度大小为1的维度

不传入dim时,则删除所有大小为1的维度

import torch
b = torch.tensor([[[[1],[2]]],[[[3],[4]]]])
print(b)
print(b.size(),b.squeeze().size())
print(b.squeeze())

在这里插入图片描述

传入dim时,仅在给定尺寸中进行挤压操作

x = torch.zeros(2, 1, 2, 1, 2)# dim0的维度为2,所以挤压后不变,
# squeeze(0)表示如果dim0的维度为1则删除dim0
print(x.size(),x.squeeze(0).size())print(x.size(),x.squeeze(1).size(),x.squeeze(1).size())# 不影响x的原来的形状
print(x.shape)

在这里插入图片描述
参考博客
参考博客

torch.div

张量和标量做逐元素除法
或者两个可广播的张量之间做逐元素除法
在这里插入图片描述

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...