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

mac2026-08-12  5

介绍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

对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

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

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, widthinterpolation: default: Bilinear size = (100, 100) transform = transforms.Compose([ transforms.Resize(size), ]) new_img = transform(img) new_img

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

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

Lambda

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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)

RandomPerspective

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

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

最新回复(0)