PIL+pyplot+transforms.ToTensor+unsqueeze+div
创始人
2024-03-12 17:51:30
0

目录

  • 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

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

相关内容

热门资讯

公安部打掉黑灰产犯罪团伙200... 12月25日,公安部在京召开专题新闻发布会,通报公安部和国家金融监督管理总局联合部署开展金融领域“黑...
公安机关打掉金融领域“黑灰产”... 新华社北京12月25日电(记者任沁沁、熊丰)公安部12月25日在京召开新闻发布会,通报今年6月至11...
老年婚介服务引纠纷 成都金牛区... 追求爱情从来不是年轻人的专利,老年人也有权利去追求真爱。不过,不可只抱有美好期许,也要擦亮眼睛。近日...
国家医保局:长护险制度将从试点... 本报讯(中青报·中青网记者 刘昶荣)在日前浙江宁波召开的2025年全国长期护理保险高质量发展大会上,...
公安部发布金融领域“黑灰产”违... 12月25日,公安部在京召开专题新闻发布会,通报公安部和国家金融监督管理总局联合部署开展金融领域“黑...
国家发展改革委:加快推动交通运... 12月25日,国家发展改革委基础设施发展司刊发题为《加快构建现代化基础设施体系》的署名文章。文章指出...
国家发改委:优化收费公路政策 ... 每经AI快讯,据国家发改委官微消息,12月25日,国家发展改革委基础设施发展司发文称,深化重点领域改...
清华大学开展招生宣讲,发布招生... 红星新闻网12月25日讯12月24日,清华大学招生办公室发布声明。 声明称,近日,清华大学招生办接到...
公安部:立案查处金融领域“黑灰... 北京商报讯(记者 岳品瑜 董晗萱)12月25日,公安部召开新闻发布会,通报公安部和国家金融监督管理总...
感知山东| 胶州市开展“法律护... 为不断深化“陪伴成长”全环境立德树人品牌建设,近日,胶州市司法局李哥庄司法所联合镇宣传办,邀请市“蓝...