pytorch集锦(9)-数据变换(2)
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
目录
转换变换
ToPILImage([mode])
将张量或ndarray转换为PIL图像。
ToTensor()
将PIL图像或numpy.ndarray转换为张量。
PILToTensor()
将PIL图像转换为相同类型的张量
通用变换
Lambda(lambd)
将用户定义的lambda应用于转换。
自动增强变换
见论文
AutoAugment: Learning Augmentation Strategies from Data
Ekin D. Cubuk
∗, Barret Zoph∗ , Dandelion Man ́e, Vijay Vasudevan, Quoc V. Le Google
Brain
https://arxiv.org/pdf/1805.09501.pdf
AutoAugment是一种常见的数据增强技术可以提高图像分类模型的准确性。尽管数据扩充策略直接与它们的训练数据集相关联但实证研究表明ImageNet策略在应用于其他数据集时提供了显著的改进。在TorchVision中我们实施了在以下数据集上学习的3个策略ImageNet、CIFAR10和SVHN。新变换可以单独使用也可以与现有变换混合使用和匹配
AutoAugmentPolicy(value)
在不同数据集上学习的AutoAugment策略。
AutoAugment([policy, interpolation, fill])
基于“AutoAugment:从数据中学习增强策略”的AutoAugment数据增强方法。
RandAugment([num_ops, magnitude, …])
基于“RandAugment减少搜索空间的实用自动数据扩充”的RandAugment数据扩充方法。
TrivialAugmentWide([num_magnitude_bins, …])
使用TrivialAugment Wide进行数据集无关的数据扩充如“TrivialAugment无需调整但最先进的数据扩充”中所述。
AugMix([severity, mixture_width, …])
基于“AugMix:提高鲁棒性和不确定性的简单数据处理方法”的AugMix数据增强方法。
功能转换
函数转换为您提供了对转换管道的细粒度控制。与上述变换不同函数变换不包含参数的随机数生成器。这意味着您必须指定/生成所有参数但函数转换将为您提供跨调用的可复制结果。
示例可以将具有相同参数的函数变换应用于多个图像如下所示
import torchvision.transforms.functional as TF
import random
def my_segmentation_transforms(image, segmentation):
if random.random() > 0.5:
angle = random.randint(-30, 30)
image = TF.rotate(image, angle)
segmentation = TF.rotate(segmentation, angle)
# more transforms ...
return image, segmentation
示例可以使用函数转换来构建具有自定义行为的转换类
import torchvision.transforms.functional as TF
import random
class MyRotationTransform:
"""Rotate by one of the given angles."""
def __init__(self, angles):
self.angles = angles
def __call__(self, x):
angle = random.choice(self.angles)
return TF.rotate(x, angle)
rotation_transform = MyRotationTransform(angles=[-30, -15, 0, 15, 30])
adjust_brightness(img, brightness_factor)
调整图像的亮度。
adjust_contrast(img, contrast_factor)
调整图像的对比度。
adjust_gamma(img, gamma[, gain])
对图像执行伽马校正。
adjust_hue(img, hue_factor)
调整图像的色调。
adjust_saturation(img, saturation_factor)
调整图像的颜色饱和度。
adjust_sharpness(img, sharpness_factor)
调整图像的清晰度。
affine(img, angle, translate, scale, shear)
对图像应用仿射变换保持图像中心不变。
autocontrast(img)
通过重新映射每个通道的像素来最大化图像的对比度使最低的变成黑色最轻的变成白色。
center_crop(img, output_size)
在中心裁剪给定图像。
convert_image_dtype(image[, dtype])
将张量图像转换为给定的数据类型并相应地缩放值。此函数不支持PIL图像。
crop(img, top, left, height, width)
在指定位置和输出大小裁剪给定图像。
equalize(img)
通过对输入应用非线性映射来均衡图像的直方图以便在输出中创建灰度值的均匀分布。
erase(img, i, j, h, w, v[, inplace])
用给定值擦除输入张量图像。
five_crop(img, size)
将给定图像裁剪为四个角和中心裁剪。
gaussian_blur(img, kernel_size[, sigma])
通过给定的核对图像执行高斯模糊。
get_dimensions(img)
将图像的尺寸返回为[channelsheightwidth]。
get_image_num_channels(img)
返回图像的通道数。
get_image_size(img)
将图像的大小返回为[widthheight]。
hflip(img)
水平翻转给定图像。
invert(img)
反转RGB/灰度图像的颜色。
normalize(tensor, mean, std[, inplace])
用平均值和标准差归一化浮动张量图像。
pad(img, padding[, fill, padding_mode])
用给定的“Pad”值填充给定图像的所有边。
perspective(img, startpoints, endpoints[, …])
对给定图像执行透视变换。
pil_to_tensor(pic)
将PIL图像转换为相同类型的张量。
posterize(img, bits)
通过减少每个颜色通道的位数对图像进行后验。
resize(img, size[, interpolation, max_size, …])
将输入图像调整为给定大小。
resized_crop(img, top, left, height, width, size)
裁剪给定图像并将其调整为所需大小。
rgb_to_grayscale(img[, num_output_channels])
将RGB图像转换为图像的灰度版本。
rotate(img, angle[, interpolation, expand, …])
按角度旋转图像。
solarize(img, threshold)
通过反转高于阈值的所有像素值使RGB/灰度图像日光化。
ten_crop(img, size[, vertical_flip])
从给定图像生成十个裁剪图像。
to_grayscale(img[, num_output_channels])
将任何模式RGB、HSV、LAB等的PIL图像转换为图像的灰度版本。
to_pil_image(pic[, mode])
将张量或ndarray转换为PIL图像。
to_tensor(pic)
将PIL图像或numpy.ndarray转换为张量。
vflip(img)
垂直翻转给定图像。