Pytorch 中的数据增强(自学用)

介绍Pytorch中torchvision.transforms几个数据增强函数的使用方法:

from torchvision import transforms
from PIL import Image
from torchvision.transforms import functional as TF
import torch
path = 'D:\\片儿\\1910pt课程\\pract\\001.png'
img = Image.open(path)
img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kK6bMTcz-1572679553591)(output_1_0.png)]

对torch数据操作的变换

  • ToPILImage
    convert a tensor or an ndarray to PIL image
# 将 PIL Image or numpy.ndarray 转换成 tensor 在转换成 PIL Image
transform = transforms.Compose([
    transforms.ToTensor(),
    # convert a PIL image or NUMPY.ndarray to tensor
    transforms.ToPILImage()
    # convert a tensor or an ndarray to PIL Image
])
new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wYs2W5es-1572679553592)(output_3_0.png)]

  • Normalize
    present a list which contains the mean and std of operated images, by which will normalize all the images, the type of data is Tensor
mean = [0.5, 0.5, 0.5,0.5]
std = [0.5, 0.5, 0.5,0.5]

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean, std),
    transforms.ToPILImage()# visualization
    
])
new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dDxK0ucv-1572679553592)(output_5_0.png)]

operations towards PIL data

  • ToTensor
    turn PIL Image or numpy.ndarray into tensor
transform = transforms.Compose([
    transforms.ToTensor(),   
])
new_img = transform(img)
  • Resize
    resize the input PIL Image to the given size.

parameters

  • size: (x) width=height; (h,w) height, width
  • interpolation: default: Bilinear
size = (100, 100)
transform = transforms.Compose([
    transforms.Resize(size),
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MZVRJADQ-1572679553592)(output_9_0.png)]

  • Scale
    abandoned, replaced by Resize

  • CenterCrop
    Crops the given PIL Image at the center.

parameter:

  • size: 一个值的话,高和宽共享,否则对应是 (h, w),若是该值超过原始图片尺寸,则外围用 0 填充

size = (400, 300)
transform = transforms.Compose([
    transforms.CenterCrop(size),
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VBt60QdD-1572679553593)(output_12_0.png)]

  • Pad

Pad the given PIL Image on all sides with the given “pad” value.

参数

  • padding:填充的宽度,可以是一个 值、或者元组,分别对应 4 个边

  • fill:填充的值,可以是一个值(所有通道都用该值填充),或者一个 3 元组(RGB 三通道) 当 padding_mode=constant 起作用

  • padding_mode:填充的模式:constant, edge(填充值为边缘), reflect (从边缘往内一个像素开始做镜像) or symmetric(从边缘做镜像).

padding = (100, 200, 300, 400)
transform = transforms.Compose([
    transforms.Pad(padding, padding_mode="symmetric"), 
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-piLoPAf0-1572679553593)(output_14_0.png)]

  • Lambda

根据用户自定义的方式进行变换


lambd = lambda x: TF.rotate(x, 100)
transform = transforms.Compose([
    transforms.Lambda(lambd)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rrcIKyOv-1572679553594)(output_16_0.png)]

  • RandomApply

给定一定概率从一组 transformations 应用


transform = [transforms.Pad(300, fill=(0, 255, 255)), transforms.CenterCrop(400), transforms.RandomRotation(45)]
transform = transforms.Compose([
    transforms.RandomApply(transform, p=0.8)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wa5WYKML-1572679553594)(output_18_0.png)]

  • RandomChoice

Apply single transformation randomly picked from a list


transform = [transforms.Pad(300, fill=(0, 255, 255)), transforms.CenterCrop(400), transforms.RandomRotation(45)]
transform = transforms.Compose([
    transforms.RandomChoice(transform)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nFGPwhV6-1572679553595)(output_20_0.png)]

  • RandomOrder

Apply a list of transformations in a random order


transform = [transforms.Pad(300, fill=(0, 255, 255)), transforms.CenterCrop(400), transforms.RandomRotation(45)]
transform = transforms.Compose([
    transforms.RandomOrder(transform)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t8qrHZTN-1572679553596)(output_22_0.png)]

  • RandomCrop

Crop the given PIL Image at a random location.

  • 参数:

size

padding=None

pad_if_needed=False

fill=0

padding_mode=‘constant’

transform = transforms.Compose([
    transforms.RandomCrop((100, 300))
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m9dE4d5G-1572679553596)(output_24_0.png)]

  • RandomHorizontalFlip & RandomVerticalFlip

Horizontally/Vertically flip the given PIL Image randomly with a given probability.


transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomVerticalFlip(p=0.5)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IFX8uczN-1572679553596)(output_26_0.png)]

  • RandomResizedCrop

Crop the given PIL Image to random size and aspect ratio.

参数:

size: expected output size of each edge

scale: range of size of the origin size cropped

ratio: range of aspect ratio of the origin aspect ratio cropped

interpolation: Default: PIL.Image.BILINEAR


transform = transforms.Compose([
    transforms.RandomResizedCrop((200, 300))
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J58pHOGY-1572679553597)(output_28_0.png)]

  • RandomSizedCrop

已经废弃,由 RandomResizedCrop. 取代了

  • FiveCrop

将给定的 PIL 图像裁剪成四个角和中间的裁剪


UNIT_SIZE = 200 # 每张图片的宽度是固定的
size = (100, UNIT_SIZE)
transform = transforms.Compose([
    transforms.FiveCrop(size)
])

new_img = transform(img)
delta = 20  # 偏移量,几个图片间隔看起来比较明显
new_img_2 = Image.new("RGB", (UNIT_SIZE*5+delta, 100))
top_right = 0
for im in new_img:
    new_img_2.paste(im, (top_right, 0)) # 将image复制到target的指定位置中
    top_right += UNIT_SIZE + int(delta/5) # 左上角的坐标,因为是横向的图片,所以只需要 x 轴的值变化就行

new_img_2

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kfPVZV34-1572679553597)(output_30_0.png)]

  • TenCrop

裁剪一张图片的 4 个角以及中间得到指定大小的图片,并且进行水平翻转 / 竖直翻转 共 10 张

  • 参数:

size

vertical_flip=False (默认是水平翻转)


UNIT_SIZE = 200 # 每张图片的宽度是固定的
size = (100, UNIT_SIZE)

transform = transforms.Compose([
    transforms.TenCrop(size, vertical_flip=True)
])

new_img = transform(img)

delta = 50  # 偏移量,几个图片间隔看起来比较明显
new_img_2 = Image.new("RGB", (UNIT_SIZE*10+delta, 100))
top_right = 0
for im in new_img:
    new_img_2.paste(im, (top_right, 0)) # 将image复制到target的指定位置中
    top_right += UNIT_SIZE + int(delta/10) # 左上角的坐标,因为是横向的图片,所以只需要 x 轴的值变化就行

new_img_2

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tnakBSwT-1572679553598)(output_32_0.png)]

  • LinearTransformation

白化变换,笔者不是很理解,但是好像消耗的内存应该比较大

  • ColorJitter

Randomly change the brightness, contrast and saturation of an image. 随机改变图像的亮度、对比度和饱和度

  • 参数:

brightness:亮度

contrast:对比度

saturation:饱和度

hue:色调 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.

transform = transforms.Compose([
    transforms.ColorJitter(brightness=(0, 36), contrast=(
        0, 10), saturation=(0, 25), hue=(-0.5, 0.5))
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PGoqChqb-1572679553599)(output_34_0.png)]

  • RandomRotation

一定角度旋转图像

  • 参数:

degrees:旋转的角度

resample=False:重采样过滤器 可选 {PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}

expand=False:如果为 True ,则展开输出,使其足够大以容纳整个旋转后的图像。如果为 Fales 或省略,使输出图像的大小与输入图像相同。

center=None 旋转中心


transform = transforms.Compose([
    transforms.RandomRotation(30, resample=Image.BICUBIC, expand=False, center=(100, 300))
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hwmu6DgB-1572679553599)(output_36_0.png)]

  • RandomAffine

保持图像中心不变的随机仿射变换,可以进行随心所欲的变化

  • 参数:

degrees:旋转角度

translate:水平偏移

scale:

shear: 裁剪

resample ({PIL.Image.NEAREST, PIL.Image.BILINEAR, PIL.Image.BICUBIC}, optional)

fillcolor: 图像外部填充颜色 int


transform = transforms.Compose([
    transforms.RandomAffine(degrees=30, translate=(0, 0.2), scale=(0.9, 1), shear=(6, 9), fillcolor=66)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A1HNSUbC-1572679553599)(output_38_0.png)]

  • Grayscale

转换图像灰度。

  • 参数:

num_output_channels:1 或者 3 输出图像所需的通道数 (若是 3 的话,则代表三个通道的值是一样的)

import numpy as np
transform = transforms.Compose([
    transforms.Grayscale(num_output_channels=3)
])

new_img = transform(img)
new_img_array = np.array(new_img)
r, g, b = new_img_array[:, :, 0], new_img_array[:, :, 1], new_img_array[:, :, 2]
print(r == b)
print("shape:", new_img_array.shape)
new_img
[[ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 ...
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]]
shape: (758, 1256, 3)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xns2NcDW-1572679553600)(output_40_1.png)]

  • RandomGrayscale

Randomly convert image to grayscale with a probability of p (default 0.1). 以一定的概率对图像进行灰度化,转换后的图片还是 3 通道的


transform = transforms.Compose([
    transforms.RandomGrayscale(p=0.6)
])

new_img = transform(img)
print(np.array(new_img).shape)
new_img
(758, 1256, 3)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yxJHytNH-1572679553600)(output_42_1.png)]

  • RandomPerspective

对给定的 PIL 图像以给定的概率随机进行透视变换。


transform = transforms.Compose([
    transforms.RandomPerspective(distortion_scale=1, p=1, interpolation=3)
])

new_img = transform(img)
new_img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NHoE15mE-1572679553601)(output_44_0.png)]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deyiwang89

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

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

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

打赏作者

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

抵扣说明:

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

余额充值