PyTorch之torchvision.transforms实战

本文详细介绍了PyTorch中torchvision.transforms模块的使用,包括Compose、Resize、Scale、CenterCrop、RandomCrop、RandomHorizontalFlip、RandomVerticalFlip和RandomResizedCrop等变换方法。通过实例展示了如何对图像进行等比缩放、随机裁剪、翻转等操作,为深度学习图像预处理提供参考。
摘要由CSDN通过智能技术生成

PyTorch之torchvision.transforms实战

#Pyton Image Library  PIL 一个python图片库
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt
%matplotlib inline

img = Image.open(r"C:\Users\pandas\Pictures\QQ图片20211021234845.jpg")
print(img.size)
plt.imshow(img)
(1668, 1450)





<matplotlib.image.AxesImage at 0x1c8548504c0>

在这里插入图片描述

transforms.Compose

transformer = transforms.Compose([                                
    transforms.Resize(256),
    transforms.transforms.RandomResizedCrop((224), scale = (0.5,1.0)),
    transforms.RandomHorizontalFlip(),
])
test_a = transformer(img)
plt.imshow(test_a)
<matplotlib.image.AxesImage at 0x1c856240be0>

在这里插入图片描述

transforms.Resize

对载入的图片数据按照我们的需要进行缩放,传递给这个类的size可以是一个整型数据,也可以是一个类似于 (h ,w) 的序列。如果输入是个(h,w)的序列,h代表高度,w代表宽度,h和w都是int,则直接将输入图像resize到这个(h,w)尺寸,相当于force。如果使用的是一个整型数据,则将图像的短边resize到这个int数,长边则根据对应比例调整,图像的长宽比不变。

# 等比缩放
test1 = transforms.Resize(224)(img)
print(test1.size)
plt.imshow(test1)
(257, 224)





<matplotlib.image.AxesImage at 0x1c8562e73a0>

在这里插入图片描述

transforms.Scale

对载入的图片数据我们的需要进行缩放,用法和torchvision.transforms.Resize类似。。传入的size只能是一个整型数据,size是指缩放后图片最小边的边长。举个例子,如果原图的height>width,那么改变大小后的图片大小是(size*height/width, size)。

# 等比缩放
test2 = transforms.Scale(224)(img)
print(test2.size)
plt.imshow(test2)
(257, 224)


C:\Users\pandas\anaconda3\envs\pytorch_test\lib\site-packages\torchvision\transforms\transforms.py:317: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +





<matplotlib.image.AxesImage at 0x1c856386700>

在这里插入图片描述

transforms.CenterCrop

test3 = transforms.CenterCrop((500,500))(img)
print(test3.size)
plt.imshow(test3)
(500, 500)





<matplotlib.image.AxesImage at 0x1c8563f0ca0>

在这里插入图片描述

test4 = transforms.CenterCrop(224)(img)
print(test4.size)
plt.imshow(test4)
(224, 224)





<matplotlib.image.AxesImage at 0x1c8564544f0>

在这里插入图片描述

transforms.RandomCrop

test5 = transforms.RandomCrop(224)(img)
print(test5.size)
plt.imshow(test5)
(224, 224)





<matplotlib.image.AxesImage at 0x1c8564c56a0>

在这里插入图片描述

test6 = transforms.RandomCrop((300,300))(img)
print(test6.size)
plt.imshow(test6)
(300, 300)





<matplotlib.image.AxesImage at 0x1c856539790>

在这里插入图片描述

transforms.RandomHorizontalFlip

test7 = transforms.RandomHorizontalFlip()(img)
print(test7.size)
plt.imshow(test7)
(1668, 1450)





<matplotlib.image.AxesImage at 0x1c85659ba90>

在这里插入图片描述

transforms.RandomVerticalFlip

test8 = transforms.RandomVerticalFlip()(img)
print(test8.size)
plt.imshow(test8)
(1668, 1450)





<matplotlib.image.AxesImage at 0x1c856f59c10>

在这里插入图片描述

transforms.RandomResizedCrop

test9 = transforms.RandomResizedCrop(224)(img)
print(test9.size)
plt.imshow(test9)
(224, 224)





<matplotlib.image.AxesImage at 0x1c8576bc6a0>

在这里插入图片描述

test9 = transforms.RandomResizedCrop(224,scale=(0.5,0.8))(img)
print(test9.size)
plt.imshow(test9)
(224, 224)





<matplotlib.image.AxesImage at 0x1c85772e8b0>

在这里插入图片描述

显示图片对比

# RandomResizedCrop 将给定图像随机裁剪为不同的大小和宽高比,然后缩放所裁剪得到的图像为制定的大小
print("原图大小:",img.size)
# Crop代表剪裁到某个尺寸
data1 = transforms.RandomResizedCrop(224)(img)
# data1、data2、data3尺寸一样,长宽都是224*224  size也可以是一个Integer,在这种情况下,切出来的图片的形状是正方形
print("随机裁剪后的大小:",data1.size)
data2 = transforms.RandomResizedCrop(224)(img)
data3 = transforms.RandomResizedCrop(224)(img)

# 放四个格,布局为2*2
plt.subplot(2,2,1),plt.imshow(img),plt.title("Original")
plt.subplot(2,2,2),plt.imshow(data1),plt.title("Transform 1")
plt.subplot(2,2,3),plt.imshow(data2),plt.title("Transform 2")
plt.subplot(2,2,4),plt.imshow(data3),plt.title("Transform 3")
plt.show()
原图大小: (1668, 1450)
随机裁剪后的大小: (224, 224)

在这里插入图片描述

# 以输入图的中心点为中心点做指定size的crop操作
img1 = transforms.CenterCrop(224)(img)
img2 = transforms.CenterCrop(224)(img)
img3 = transforms.CenterCrop(224)(img)
# img1、img2、img3三个图是一样的
plt.subplot(2,2,1),plt.imshow(img),plt.title("Original")
plt.subplot(2,2,2), plt.imshow(img1), plt.title("Transform 1")
plt.subplot(2,2,3), plt.imshow(img2), plt.title("Transform 2")
plt.subplot(2,2,4), plt.imshow(img3), plt.title("Transform 3")
plt.show()

在这里插入图片描述

# 以给定的概率随机水平旋转给定的PIL的图像,默认为0.5
img1 = transforms.RandomHorizontalFlip()(img)
img2 = transforms.RandomHorizontalFlip()(img)
img3 = transforms.RandomHorizontalFlip()(img)

plt.subplot(2,2,1),plt.imshow(img),plt.title("Original")
plt.subplot(2,2,2), plt.imshow(img1), plt.title("Transform 1")
plt.subplot(2,2,3), plt.imshow(img2), plt.title("Transform 2")
plt.subplot(2,2,4), plt.imshow(img3), plt.title("Transform 3")
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ACxz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值