0


神经网络数据增强transforms的相关操作(持续更新)

transforms的相关操作(Pytorch)

日志:
2022.6.18:加入transforms.TenCrop()

一、图像的相关变化

1、格式转换

(1)transforms.ToTensor()

可将PIL格式、数组格式转换为tensor格式

img_path ="E:\expression_recognition\\2.jpg"
img = Image.open(img_path)# #数组类型PIL类型都可
a1 = transforms.ToTensor()
img_tensor =(img)

(2)transforms.ToPILImage()

tensor格式 或者 数组类型的数据转换为 PIL 格式

img_path="E:\expression_recognition\\1.jpg"
img = Image.open(img_path)
img_array = numpy.array(img)
a1 = transforms.ToTensor()
img_tensor = a1(img)# 1、数组转换为PIL格式
a2 = transforms.ToPILImage()
img_array = numpy.uint8(img_array)# 满足类型需求
img_PIL = a2(img_array)# 2、tensor转换为PIL格式
img_tensor = img_tensor.float()# 满足类型需求
img_PIL = a2(img_tensor)

(3)transforms.Normalize()

注意该操作只能对tensor格式进行操作,因此放transforms.ToTensor()之后
对图像进行标准化处理
括号中的值transforms.Resize()mean,std以三通道为例:[0.5,0.5,0.5],[0.5,0.5,0.5]

2、图像大小和颜色变换

(1)transforms.Resize()

可对tensor类型,PIL类型进行缩放,不可对数组类型进行缩放
括号中的值transforms.Resize()W图片缩放为(WW)(H,W)图片缩放为(HW)

a = transforms.Resize([256,512])# [H,W]
img_resize = a2(img)
img_resize.show()

在这里插入图片描述

(2)transforms.ColorJitter()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
括号中的值transforms.ColorJitter(brightness, contrast, saturation, hue)brightness亮度contrast对比度saturation饱和度hue色调

a3 = transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
img_colorjitter = a3(img)
img_colorjitter.show()

左端原图,右端变换完的图

(3)transforms.Grayscale()和transforms.RandomGrayscale()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
功能transforms.Grayscale()将图像变为灰度图像transforms.RandomGrayscale()将图像随机变为灰度图像

a4 = transforms.Grayscale(num_output_channels=1)
img_grayscale = a4(img)
img_grayscale.show()
a5 = transforms.RandomGrayscale(p=0.1)
img_randomgrayscale = a5(img)
img_randomgrayscale.show()

3、图像的裁剪与旋转

(1)随机裁剪transforms.RandomCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
随机裁剪有很多参数,但是目前用到的只有这一种参数
参数功能size按照所给的size大小进行裁剪,size可以是(h,w)或直接是w

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

a6 = transforms.RandomResizedCrop((124,124))
a7 = transforms.RandomResizedCrop(256)
img_randomresizedcrop1 = a6(img)
img_randomresizedcrop2 = a7(img)
plt.subplot(1,3,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,3,2),plt.imshow(img_randomresizedcrop1),plt.title("转换后的图1")
plt.subplot(1,3,3),plt.imshow(img_randomresizedcrop2),plt.title("转换后的图2")
plt.show()

(2)中心裁剪transforms.CenterCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
与随机裁剪基本一样,只不过是从中心进行裁剪
参数功能size按照所给的size大小进行中心裁剪,size可以是(h,w)或直接是w

(3)随机长宽比裁剪transforms.RandomResizedCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
主要用到以下两个参数:
参数功能size输出的分辨率大小scale随机crop的大小区间,如scale=(0.8, 1.0)(前面是h的倍数,后面是w的倍数),表示随机crop出来的图片会在的0.8倍至1倍之间

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

a6 = transforms.RandomResizedCrop((64,128), scale=(0.08,1.0))
img_randomresizedcrop = a6(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomresizedcrop),plt.title("转换后的图")
plt.show()

(4)10次裁剪transforms.TenCrop()

裁剪图片的中间部分和四个角并进行翻转得到四张图片(默认水平翻转)
参数功能size裁剪图片的大小vertical_flip是否垂直翻转
默认采用水平翻转,第一张为原图

a7 = transforms.TenCrop(224)
img1 = a7(img)
plt.figure(figsize=(20,18))
plt.subplot(3,4,1),plt.imshow(img),plt.title(1)for i inrange(0,10):
    plt.subplot(3,4,i+2),plt.imshow(img1[i]),plt.title(str(i+1))
plt.show()


采用垂直翻转,第一张为原图

a8 = transforms.TenCrop(224,vertical_flip=True)
img2 = a8(img)
plt.figure(figsize=(20,18))
plt.subplot(3,4,1),plt.imshow(img),plt.title(0)for i inrange(0,10):
    plt.subplot(3,4,i+2),plt.imshow(img2[i]),plt.title(str(i+1))
plt.show()

(5)概transforms.RandomHorizontalFlip()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

a7 = transforms.RandomHorizontalFlip(p=0.5)
img_randomhorizontalflip = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomhorizontalflip),plt.title("翻转后的图")
plt.show()

参数功能p随机水平翻转的概率

(6)概率垂直翻转transforms.RandomVerticalFlip()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
参数功能p随机垂直翻转的概率

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

a7 = transforms.RandomVerticalFlip(p=0.5)
img_randomverticalflip = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomverticalflip),plt.title("翻转后的图")
plt.show()

(7)随机旋转:transforms.RandomRotation()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
参数功能70旋转范围为:(-70,70)(30,60)旋转范围为:(30,60)

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

a7 = transforms.RandomRotation((30,50))
img_randomrotation = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomrotation),plt.title("随机旋转后的图")
plt.show()

4、transforms.Compose([])

transforms.Compose是将上述数据变换包装起来依次执行。另外有不对的地方望指出。


本文转载自: https://blog.csdn.net/hjkdh/article/details/123439969
版权归原作者 燃烧吧哥们 所有, 如有侵权,请联系我们删除。

“神经网络数据增强transforms的相关操作(持续更新)”的评论:

还没有评论