• Pytorch 图像增强 实现翻转裁剪色调等 附代码(全)


    前言

    下文中有使用到plt,不懂的可看我这篇文章:python之Matplotlib详细分析(附代码)

    数据比较少的时候使用数据增强是一个不错的选择

    导入对应的包:

    from PIL import Image
    from torchvision import transforms as tfs
    
    • 1
    • 2

    读取对应的照片:

    # 此处的Path 该项目的相对路径 或者 绝对路径
    # 读取 显示 照片
    image = Image.open('scenery.jpg')
    print(image.size)
    image
    
    • 1
    • 2
    • 3
    • 4
    • 5

    截图如下:
    在这里插入图片描述

    数据增强的处理方式有如下

    1. 裁剪

    裁剪之前 先叙述一遍缩放(毕竟裁剪是基于缩放的基础)

    from PIL import Image
    from torchvision import transforms as tfs
    
    # 原图尺寸为474 * 379
    img = Image.open('scenery.jpg')
    
    # 默认是300 * 300 
    # 缩放原图到一定比例
    img1 = tfs.Resize((300,300))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1, 2)
    
    axs[0].set_title('474 * 379')
    axs[0].imshow(img)
    
    axs[1].set_title('300 * 300')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    截图如下:
    在这里插入图片描述

    1.1 中心裁剪

    • 核心函数:transforms.CenterCrop(size)

    size 参数为int,将其原图 中心裁剪为设定尺寸
    如果参数为(h,w),则裁剪的尺寸为(h,w)

    from PIL import Image
    from torchvision import transforms as tfs
    
    # 打开对应的图片
    img = Image.open('scenery.jpg')
    
    # 默认是300 * 300 
    img1 = tfs.CenterCrop(300)(img)
    # 设置对应的尺寸为300 * 200 
    img2 = tfs.CenterCrop((300,200))(img)
    
    # 通过plt输出图片
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1, 2)
    
    axs[0].set_title('300 * 300')
    axs[0].imshow(img1)
    
    axs[1].set_title('300 * 200')
    axs[1].imshow(img2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    截图如下:
    在这里插入图片描述

    1.2 随机裁剪

    • 核心函数:transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')
    参数具体说明
    size1.为size,结果为size * size
    2.为(h,w),结果为h * w
    padding填充大小
    1.为a,上下左右填充a
    2.为(a,b)时,左右填充a个像素,上下填充b个像素
    3.为(a,b,c,d)时,左、上、右、下分别填充a、b、c、d
    pad_if_need默认为False
    为True,超出尺寸进行填充
    fill通道填充的颜色
    padding_mode填充模式
    1.constant,像素值由fill填充
    2.edge,图像边缘像素值填充
    3.reflect,镜像填充,最后一个像素不镜像,也就是反射。1, 2, 3, 4 变为 3, 2, 1, 2, 3, 4, 3, 2
    4.symmetric,镜像填充,最后一个像素镜像,也就是对称。1, 2, 3, 4 变为 2, 1, 1, 2, 3, 4, 4, 3
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 默认都300 * 300 
    img1 = tfs.RandomCrop(300)(img)
    
    # padding为填充元素
    img2 = tfs.RandomCrop(300,padding=(16, 64))(img)
    
    # fill填充的色彩
    img3 = tfs.RandomCrop(300,padding=(16, 64),fill=(255, 255, 0))(img)
    
    # 超出尺寸,该参数需要设置为True
    img4 = tfs.RandomCrop(2000,pad_if_needed=True)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('img1')
    axs[0].imshow(img1)
    
    axs[1].set_title('img2')
    axs[1].imshow(img2)
    
    axs[2].set_title('img3')
    axs[2].imshow(img3)
    
    axs[3].set_title('img4')
    axs[3].imshow(img4)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    截图如下:
    在这里插入图片描述


    按照填充模式的代码:

    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 填充模式为constant,默认本身也是constant
    img1 = tfs.RandomCrop(500,padding=128,padding_mode='constant')(img)
    
    # 填充模式为edge
    img2 = tfs.RandomCrop(500,padding=128,padding_mode='edge')(img)
    
    # 填充模式为reflect
    img3 = tfs.RandomCrop(500,padding=128,padding_mode='reflect')(img)
    
    # 填充模式为symmetric
    img4 = tfs.RandomCrop(500,padding=128,padding_mode='symmetric')(img)
    
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('img1')
    axs[0].imshow(img1)
    
    axs[1].set_title('img2')
    axs[1].imshow(img2)
    
    axs[2].set_title('img3')
    axs[2].imshow(img3)
    
    axs[3].set_title('img4')
    axs[3].imshow(img4)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    截图如下:

    在这里插入图片描述

    1.3 随机尺寸裁剪

    • 核心函数:transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(3/4, 4/3), interpolation=)
    参数具体说明
    size1.为size,结果为size * size
    2.为(h,w),结果为h * w
    scale随机裁剪面积比例,默认区间(0.08,1)之间随机取一个数
    ratio随机长宽比,默认区间(3/4,4/3)之间随机取一个数
    interpolation插值方法(PIL. Image. NEARESTPIL. Image. BILINEARPIL. Image. BICUBIC
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 裁剪的尺寸都是300 * 300
    # 随机裁剪面积比例为0.1-1之间的一个数
    img1 = tfs.RandomResizedCrop(300,scale=(0.1,1))(img)
    
    # 随机裁剪长宽比为1-4/3之间的一个数
    img2 = tfs.RandomResizedCrop(300,ratio=(1,4/3))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,3)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    axs[2].set_title('img2')
    axs[2].imshow(img2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    截图如下:

    在这里插入图片描述

    2. 翻转

    2.1 水平翻转

    • 核心函数:transforms.RandomHorizontalFlip(p=0.5)

    p为概率值,如果p为1,百分百翻转。p为0.5 ,百分之50的概率可能会翻转也可能不会

    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # p为1一定翻转
    # p为0.5 ,百分之50的概率可能会翻转也可能不会
    img1 = tfs.RandomHorizontalFlip(p=1)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1, 2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('flip horizontal')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    截图如下:

    在这里插入图片描述

    2.2 垂直翻转

    • 核心函数:transforms.RandomVerticalFlip(p=0.5)

    p为概率值,如果p为1,百分百翻转。p为0.5 ,百分之50的概率可能会翻转也可能不会

    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # p为1一定翻转
    # p为0.5 ,百分之50的概率可能会翻转也可能不会
    # 注意区分与水平翻转的函数
    img1 = tfs.RandomVerticalFlip(p=1)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1, 2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('flip vertical')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    截图如下:

    在这里插入图片描述

    2.3 随机旋转

    • 核心函数:transforms.RandomRotation(degrees, expand=False, center=None, fill=0, resample=None)
    参数具体说明
    degrees旋转角度
    1.为a,在(-a,a)区间随机选择旋转角度
    2.为(a,b),在在(a,b)区间随机选择旋转角度
    expand图片超出尺寸是否显示完整图
    1.默认False,超出不会显示完整图
    2.True,超出会显示完整图
    center旋转轴位置
    默认中心旋转
    resample重采样方法
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 逆时针旋转45
    img1 = tfs.RandomRotation(degrees=(45, 45))(img)
    
    # 逆时针旋转45,旋转超出区域启动扩展
    img2 = tfs.RandomRotation(degrees=(45, 45), expand=True)(img)
    
    # 逆时针旋转,图外区域填充颜色
    img3 = tfs.RandomRotation(degrees=(45, 45), fill=(255, 255, 0))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    axs[2].set_title('img2')
    axs[2].imshow(img2)
    
    axs[3].set_title('img3')
    axs[3].imshow(img3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    截图如下:

    在这里插入图片描述

    3. 色调

    3.1 灰度变换

    • 核心函数:transforms.Grayscale(num_output_channels=1)

    一般不设置,默认单通道灰度,如果需要三通道灰度,则对应将其值改为3即可

    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 灰度变换
    img1 = tfs.Grayscale()(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('gray')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    截图如下:

    在这里插入图片描述

    3.2 色彩抖动

    • 核心函数:transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)
    参数具体说明
    brightness亮度
    1.元组(min, max),给定区间随机变换,不可有负值
    2.浮点数,亮度范围为[max(0, 1 - brightness), 1 + brightness]
    contrast对比度。规则同上
    saturation饱和度。规则同上
    hue色调
    1.元组(min, max),给定区间随机变换,不可有负值
    2.浮点数,色调范围为[-hue, hue]区间随机变换
    整体区间范围为[0,0.5] or [-0.5,0.5]
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 亮度设置3
    img1 = tfs.ColorJitter(brightness=(3, 3))(img)
    
    # 对比度设置3
    img2 = tfs.ColorJitter(contrast=(3, 3))(img)
    
    # 饱和度设置3
    img3 = tfs.ColorJitter(saturation=(3, 3))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('brightness')
    axs[1].imshow(img1)
    
    axs[2].set_title('contrast')
    axs[2].imshow(img2)
    
    axs[3].set_title('saturation')
    axs[3].imshow(img3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    截图如下:
    在这里插入图片描述

    3.3 随机翻转颜色

    • 核心函数:transforms.RandomInvert(p=0.5)
      p为概率值,如果p为1,百分百翻转。p为0.5 ,百分之50的概率可能会翻转也可能不会
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 随机翻转图片颜色
    img1 = tfs.RandomInvert(p=0.5)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    截图如下:

    在这里插入图片描述

    3.4 随机调整锐度

    • 核心函数:transforms.RandomAdjustSharpness(sharpness_factor, p=0.5)

    sharpness_factor 该参数为调整锐度,0为模糊,1为原图。随着数字越大,锐度越高,没有设置锐度上限
    p为概率值,如果p为1,百分百翻转。p为0.5 ,百分之50的概率可能会翻转也可能不会

    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 随机调转图片锐度
    img1 = tfs.RandomAdjustSharpness(200)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    截图如下:
    在这里插入图片描述

    3.5 高斯模糊

    • 核心函数:transforms.GaussianBlur(kernel_size, sigma=(0.1, 2.0))
    参数具体说明
    kernel_size模糊半径(奇数)
    sigma正态分布标准差
    1.为(min, max),在(min, max)区间随机取一个数
    2.浮点数,则为该数
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # size为11,标准差为10
    img1 = tfs.GaussianBlur(11,10)(img)
    
    # size为51,标准差为10
    img2 = tfs.GaussianBlur(51,10)(img)
    
    # size为101,标准差为100
    img3 = tfs.GaussianBlur(101,100)(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    axs[2].set_title('img2')
    axs[2].imshow(img2)
    
    axs[3].set_title('img3')
    axs[3].imshow(img3)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    截图如下:

    在这里插入图片描述

    4. 边缘填充

    • 核心函数:transforms.Pad(padding, fill=0, padding_mode=‘constant’)
    参数具体说明
    padding拓展大小
    1.为a,上下左右填充a
    2.为(a,b)时,左右填充a个像素,上下填充b个像素
    3.为(a,b,c,d)时,左、上、右、下分别填充a、b、c、d
    fill填充值
    默认0,黑色。也可填充三通道
    padding_mode填充模式
    1.constant,像素值由fill填充
    2.edge,图像边缘像素值填充
    3.reflect,镜像填充,最后一个像素不镜像,也就是反射。1, 2, 3, 4 变为 3, 2, 1, 2, 3, 4, 3, 2
    4.symmetric,镜像填充,最后一个像素镜像,也就是对称。1, 2, 3, 4 变为 2, 1, 1, 2, 3, 4, 4, 3
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 第一个参数:扩充的宽度
    # 第二个参数:填充颜色
    img1 = tfs.Pad(100,fill=(0, 0, 255))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,2)
    
    axs[0].set_title('original')
    axs[0].imshow(img)
    
    axs[1].set_title('img1')
    axs[1].imshow(img1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    截图如下:

    在这里插入图片描述

    5. 仿射变换

    该含义为:旋转、平移、缩放、扭曲等组合

    • 核心函数:transforms.RandomAffine(degrees, translate=None, scale=None, shear=None, resample=0, fillcolor=0)
    参数具体说明
    degrees随机旋转角度范围,0为不旋转
    translate水平和垂直平移因子
    为(a, b),水平平移位置为 (-img_width * a , img_width * a)该区间随机选择一个数。垂直平移为 (-img_height * b , img_height * b)该区间随机选择一个数
    scale缩放因子
    为(a, b),在(a, b)区间随机选择一个数
    shear随机扭曲角度范围
    为(a, b),在(a, b)区间随机选择一个数
    resample重采样
    fillcolor填充色彩,可三通道填充
    from PIL import Image
    from torchvision import transforms as tfs
    
    img = Image.open('scenery.jpg')
    
    # 随机旋转
    img1 = tfs.RandomAffine(45)(img)
    
    # 随机平移
    img2 = tfs.RandomAffine(0, (0.7, 0))(img)
    
    # 随机缩放
    img3 = tfs.RandomAffine(0, None, (3, 5))(img)
    
    # 随机扭曲
    img4 = tfs.RandomAffine(0, None, None, (45, 90))(img)
    
    import matplotlib.pyplot as plt
    axs = plt.figure().subplots(1,4)
    
    axs[0].set_title('img1')
    axs[0].imshow(img1)
    
    axs[1].set_title('img2')
    axs[1].imshow(img2)
    
    axs[2].set_title('img3')
    axs[2].imshow(img3)
    
    axs[3].set_title('img4')
    axs[3].imshow(img4)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    截图如下:

    在这里插入图片描述

  • 相关阅读:
    hadoop集群搭建
    Mysql-解决创建存储函数This function has none of DETERMINISTIC
    剑桥高材生归国创业,15年AI长跑终于迎来IPO
    WEB基础及http协议(Apache)
    源码转换器 Tangible:Source Code Converters 23.9
    在 docker 容器中使用 docker
    在云栖深处见未来
    何为驱动(详解)
    数值类型转换02
    法语初级学习笔记-03-疑问句
  • 原文地址:https://blog.csdn.net/weixin_47872288/article/details/127956653