torchvision.transforms¶
转换是常见的图像转换。它们可以使用Compose
.
此外,还有torchvision.transforms.functional
模块。
函数转换提供对转换的精细控制。
如果您必须构建更复杂的转换管道,这将非常有用
(例如,在分段任务的情况下)。
所有转换都接受 PIL Image、Tensor Image 或批量 Tensor Image 作为输入。Tensor Image 是一个具有形状的张量,其中 是多个通道,是图像的高度和宽度。批次
Tensor Images 是形状的张量,其中 是批次中的图像数量。Deterministic 或
应用于该批次的 Tensor Images 的随机变换与该批次的所有图像的变换相同。(C, H, W)
C
H
W
(B, C, H, W)
B
警告
从 v0.8.0 开始,所有的随机变换都使用 torch 默认的随机生成器来采样随机参数。 这是一个向后兼容性的破坏性变化,用户应该按如下方式设置 random 状态:
# Previous versions
# import random
# random.seed(12)
# Now
import torch
torch.manual_seed(17)
请记住,torch 随机生成器和 Python 随机生成器的相同种子不会 产生相同的结果。
可编写脚本的转换¶
为了编写转换脚本,请使用 而不是torch.nn.Sequential
Compose
.
transforms = torch.nn.Sequential(
transforms.CenterCrop(10),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
)
scripted_transforms = torch.jit.script(transforms)
确保仅使用可编写脚本的转换,即使用 lambda 函数且不需要 lambda 函数或 .torch.Tensor
PIL.Image
对于要与 一起使用的任何自定义转换,它们应派生自 。torch.jit.script
torch.nn.Module
转换的组合¶
-
类(转换)[来源]
torchvision.transforms.
Compose
¶ 将多个转换组合在一起。此转换不支持 torchscript。 请参阅下面的注释。
参数: transforms (list of objects) - 要组合的转换列表。 Transform
例
>>> transforms.Compose([ >>> transforms.CenterCrop(10), >>> transforms.ToTensor(), >>> ])
注意
要编写转换脚本,请使用以下方法。
torch.nn.Sequential
>>> transforms = torch.nn.Sequential( >>> transforms.CenterCrop(10), >>> transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), >>> ) >>> scripted_transforms = torch.jit.script(transforms)
确保仅使用可编写脚本的转换,即使用 不需要 lambda 函数或 .
torch.Tensor
PIL.Image
在 PIL Image 和 torch.*Tensor 上进行转换¶
-
class (size)[来源]
torchvision.transforms.
CenterCrop
¶ 在中心裁剪给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: size (sequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
-
类别(亮度=0,对比度=0,饱和度=0,色调=0)[来源]
torchvision.transforms.
ColorJitter
¶ 随机更改图像的亮度、对比度和饱和度。
参数: - brightness (float or tuple of python:float (min, max)) - 亮度抖动的程度。 brightness_factor 从 [max(0, 1 - brightness), 1 + brightness] 中统一选择 或给定的 [min, max]。应为非负数。
- contrast (float or tuple of python:float (min, max)) – 抖动对比度的程度。 contrast_factor 从 [max(0, 1 - contrast), 1 + contrast] 中统一选择 或给定的 [min, max]。应为非负数。
- saturation (float or tuple of python:float (min, max)) – 抖动饱和度的程度。 saturation_factor 从 [max(0, 1 - saturation), 1 + saturation] 中统一选择 或给定的 [min, max]。应为非负数。
- hue (float or tuple of python:float (min, max)) – 色调抖动的程度。 hue_factor 是从 [-hue, hue] 或给定的 [min, max] 中统一选择的。 应具有 0<= 色相 <= 0.5 或 -0.5 <= 最小值 <= 最大值 <= 0.5。
-
class (size)[来源]
torchvision.transforms.
FiveCrop
¶ 将给定的图像裁剪为四个角,并进行中央裁剪。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
注意
此转换返回一个图像元组,并且 inputs 并定位您的 Dataset 返回。有关如何处理的示例,请参见下文 这。
参数: size (sequence or int) – 作物的所需输出大小。如果 size 是 (h, w) 等序列,则进行 size (size, size) 的方形裁剪。 如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。 int
例
>>> transform = Compose([ >>> FiveCrop(size), # this is a list of PIL Images >>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor >>> ]) >>> #In your test loop you can do the following: >>> input, target = batch # input is a 5d tensor, target is 2d >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops >>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
-
类 (num_output_channels=1)[来源]
torchvision.transforms.
Grayscale
¶ 将图像转换为灰度。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., 3, H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: num_output_channels (int) – 输出图像所需的(1 或 3)个通道数 返回: - 输入的灰度版本。
- 如果 :返回的图像是单通道
num_output_channels == 1
- 如果 : 返回的图像是 3 通道,其中 r == g == b
num_output_channels == 3
- 如果 :返回的图像是单通道
返回类型: 太平船务图片
-
类 (padding, fill=0, padding_mode='constant')[来源]
torchvision.transforms.
Pad
¶ 用给定的 “pad” 值在给定的图像的所有侧面填充。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - padding (int or tuple or list) – 每个边框上的填充。如果提供了单个 int
用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充
分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组
这分别是 left、top、right 和 bottom 边框的填充。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
[padding, ]
- fill (int or tuple) – 常量填充的像素填充值。默认值为 0。如果 length 3,分别用于填充 R、G、B 通道。 仅当 padding_mode 为 constant 时,才使用此值
- padding_mode (str) –
填充类型。应为:constant、edge、reflect 或 symmetric。 默认值为 constant。Tensor 输入尚不支持 Mode symmetric。
- 常数:具有常数值的焊盘,该值由 fill 指定
- edge:在图像边缘使用最后一个值的填充
- Reflect:使用图像反射进行填充,而不重复边缘上的最后一个值例如,在反射模式下,填充 [1, 2, 3, 4] 两侧有 2 个元素 将导致 [3, 2, 1, 2, 3, 4, 3, 2]
- 对称:带有图像反射的焊盘重复边缘上的最后一个值例如,在对称模式下,两侧有 2 个元素填充 [1, 2, 3, 4] 将产生 [2, 1, 1, 2, 3, 4, 4, 3]
- padding (int or tuple or list) – 每个边框上的填充。如果提供了单个 int
用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充
分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组
这分别是 left、top、right 和 bottom 边框的填充。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
-
class (degrees, translate=None, scale=None, shear=None, resample =0, fillcolor=0)[来源]
torchvision.transforms.
RandomAffine
¶ 图像的随机仿射变换保持中心不变。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - degrees (sequence or float or int) - 可供选择的度数范围。 如果 degrees 是一个数字而不是序列,如 (min, max),则度的范围 将为 (-degrees, +degrees)。设置为 0 可停用旋转。
- translate (tuple, optional) - 水平最大绝对分数的元组 和垂直翻译。例如 translate=(a, b),然后水平偏移 在 -img_width * a < dx < img_width * a 范围内随机采样,垂直偏移为 在 -img_height * b < dy < img_height * b 范围内随机采样。默认情况下不会翻译。
- scale (tuple, optional) - 缩放因子间隔,例如 (a, b),则 scale 为 从范围 A <= scale <= b 中随机采样。默认情况下将保持原始比例。
- shear (sequence or float or int, optional) – 可供选择的度数范围。 如果 shear 是一个数字,则为平行于范围内 x 轴的剪切 (-shear, +shear) 将应用。否则,如果 shear 是一个元组或 2 个值的列表,则 shear 平行于 范围 (shear[0], shear[1])。否则,如果 shear 是一个 4 个值的元组或列表,则 将应用 x 轴剪切输入 (shear[0], shear[1]) 和 y 轴剪切输入 (shear[2], shear[3])。 默认情况下不会应用剪切。
- resample (int, optional) – 可选的重采样筛选条件。有关更多信息,请参阅筛选条件。
如果省略,或者图像具有模式“1”或“P”,则将其设置为 。
如果 input 为 Tensor,则仅支持 和 。
PIL.Image.NEAREST
PIL.Image.NEAREST
PIL.Image.BILINEAR
- fillcolor (tuple or int) – 区域的可选填充颜色 (RGB 图像的 Tuple 和灰度的 int 在输出图像中的变换之外 (Pillow>=5.0.0)。Tensor 不支持此选项 输入。输出图像中变换外部区域的填充值始终为 0。
-
类(转换,p=0.5)[来源]
torchvision.transforms.
RandomApply
¶ 随机应用具有给定概率的转换列表。
注意
为了编写转换脚本,请使用 list / tuple 作为输入,而不是 transforms,如下所示:
torch.nn.ModuleList
>>> transforms = transforms.RandomApply(torch.nn.ModuleList([ >>> transforms.ColorJitter(), >>> ]), p=0.3) >>> scripted_transforms = torch.jit.script(transforms)
确保仅使用可编写脚本的转换,即使用 不需要 lambda 函数或 .
torch.Tensor
PIL.Image
参数:
-
class (size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')[来源]
torchvision.transforms.
RandomCrop
¶ 在随机位置裁剪给定的图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: - size (sequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
- padding (int 或 sequence,可选) – 每个边框上的可选填充
的图像。默认值为 None。如果提供了单个 int
用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充
分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组
这分别是 left、top、right 和 bottom 边框的填充。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
[padding, ]
- pad_if_needed (boolean) – 如果小于 所需的大小来避免引发异常。由于裁剪已完成 填充后,填充似乎以随机偏移量完成。
- fill (int or tuple) – 常量填充的像素填充值。默认值为 0。如果 length 3,分别用于填充 R、G、B 通道。 仅当 padding_mode 为 constant 时,才使用此值
- padding_mode (str) –
填充类型。应为:constant、edge、reflect 或 symmetric。默认值为 constant。 Tensor 输入尚不支持 Mode symmetric。
- 常数:具有常数值的焊盘,该值由 fill 指定
- edge:图像边缘上最后一个值的填充
- reflect:带有图像反射的焊盘(不重复边缘上的最后一个值)填充 [1, 2, 3, 4],在反射模式下两侧有 2 个元素 将导致 [3, 2, 1, 2, 3, 4, 3, 2]
- symmetric: 带有图像反射的焊盘(在边缘重复最后一个值)填充 [1, 2, 3, 4],对称模式下两侧 2 个元素 将产生 [2, 1, 1, 2, 3, 4, 4, 3]
-
等级 (P=0.1)[来源]
torchvision.transforms.
RandomGrayscale
¶ 将图像随机转换为灰度,概率为 p(默认为 0.1)。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., 3, H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: p (float) - 图像应转换为灰度的概率。 返回: 输入图像的灰度版本,概率为 p 且未更改 与概率 (1-p)。 - 如果输入图像为 1 通道:灰度版本为 1 通道 - 如果输入图像为 3 通道:灰度版本为 3 通道,其中 r == g == b 返回类型: PIL 图像或张量
-
等级 (P=0.5)[来源]
torchvision.transforms.
RandomHorizontalFlip
¶ 以给定的概率随机水平翻转给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: p (float) - 图像被翻转的概率。默认值为 0.5
-
类(distortion_scale=0.5,p=0.5,插值=2,填充=0)[来源]
torchvision.transforms.
RandomPerspective
¶ 以给定的概率对给定图像执行随机透视变换。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - distortion_scale (float) – 控制失真程度的参数,范围从 0 到 1。 默认值为 0.5。
- p (float) - 图像被转换的概率。默认值为 0.5。
- - interpolation (int) - 插值类型。如果 input 为 Tensor,则仅支持 和 。默认值,用于 PIL 图像和 Tensor。
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BILINEAR
- fill (n-tuple or int or float) – 旋转区域之外区域的像素填充值
图像。如果为 int 或 float,则该值分别用于所有波段。默认值为 0。
此选项仅适用于 。Tensor 不支持此选项
输入。输出图像中变换外部区域的填充值始终为 0。
pillow>=5.0.0
-
类(大小,比例=(0.08,1.0),比率=(0.75,1.333333333333333),插值=2)[来源]
torchvision.transforms.
RandomResizedCrop
¶ 将给定图像裁剪为随机大小和纵横比。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
原始大小的随机大小(默认值:0.08 到 1.0)的裁剪和随机 长宽比(默认值:3/4 到 4/3)为原始长宽比。这种作物 最终将大小调整为给定大小。 这通常用于训练 Inception 网络。
参数: - size (int 或 sequence) - 每条边的预期输出大小。如果 size 是
int 而不是像 (h, w) 这样的序列,则方形输出大小为
䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
(size, size)
- scale (tuple of python:float) - 裁剪的原点大小的大小范围
- ratio (tuple of python:float) - 裁剪的原点纵横比的纵横比范围。
- interpolation (int) – 由过滤器定义的所需插值枚举。
默认值为 。如果 input 为 Tensor,则仅支持 和 。
PIL.Image.BILINEAR
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
- size (int 或 sequence) - 每条边的预期输出大小。如果 size 是
int 而不是像 (h, w) 这样的序列,则方形输出大小为
䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
-
class (degrees, resample=False, expand=False, center=None, fill=None)[来源]
torchvision.transforms.
RandomRotation
¶ 按角度旋转图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - degrees (sequence or float or int) - 可供选择的度数范围。 如果 degrees 是一个数字而不是序列,如 (min, max),则度的范围 将为 (-degrees, +degrees)。
- resample (int, optional) – 可选的重采样筛选条件。有关更多信息,请参阅筛选条件。
如果省略,或者图像具有模式“1”或“P”,则将其设置为 PIL。Image.NEAREST 的
如果 input 为 Tensor,则仅支持 和 。
PIL.Image.NEAREST
PIL.Image.BILINEAR
- expand (bool, optional) – 可选的扩展标志。 如果为 true,则扩展输出,使其足够大以容纳整个旋转的图像。 如果为 false 或省略,则使输出图像与输入图像的大小相同。 请注意,expand 标志假定绕中心旋转且无平移。
- center (list or tuple, optional) – 可选的旋转中心 (x, y)。原点 是左上角。 默认值是图像的中心。
- fill (n-tuple or int or float) – 旋转区域之外区域的像素填充值 图像。如果为 int 或 float,则该值分别用于所有波段。 对于所有波段,默认值为 0。此选项仅适用于 Pillow>=5.2.0。 Tensor input 不支持此选项。输出中变换外部区域的 Fill 值 image 始终为 0。
-
class (*args, **kwargs)[来源]
torchvision.transforms.
RandomSizedCrop
¶ 注意:此转换已弃用,取而代之的是 RandomResizedCrop。
-
等级 (P=0.5)[来源]
torchvision.transforms.
RandomVerticalFlip
¶ 以给定的概率随机垂直翻转给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: p (float) - 图像被翻转的概率。默认值为 0.5
-
class (size, interpolation=2)[来源]
torchvision.transforms.
Resize
¶ 将输入图像的大小调整为给定的大小。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - size (sequence or int) – 所需的输出大小。如果 size 是类似于
(h, w) 时,output size 将与此匹配。如果 size 是 int,则
图像的较小边缘将与此数字匹配。
即,如果 height > width,则 image 将被重新缩放为
(尺寸 * 高度 / 宽度、大小)。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
[size, ]
- interpolation (int, optional) – 由过滤器定义的所需插值枚举。
默认值为 。如果 input 为 Tensor,则仅支持 和 。
PIL.Image.BILINEAR
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
- size (sequence or int) – 所需的输出大小。如果 size 是类似于
(h, w) 时,output size 将与此匹配。如果 size 是 int,则
图像的较小边缘将与此数字匹配。
即,如果 height > width,则 image 将被重新缩放为
(尺寸 * 高度 / 宽度、大小)。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
-
class (size, vertical_flip=False)[来源]
torchvision.transforms.
TenCrop
¶ 将给定的图像裁剪为四个角,并进行中央裁剪加上翻转的 这些(默认使用水平翻转)。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
注意
此转换返回一个图像元组,并且 inputs 并定位您的 Dataset 返回。有关如何处理的示例,请参见下文 这。
参数: 例
>>> transform = Compose([ >>> TenCrop(size), # this is a list of PIL Images >>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor >>> ]) >>> #In your test loop you can do the following: >>> input, target = batch # input is a 5d tensor, target is 2d >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops >>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
-
类 (kernel_size, sigma=(0.1, 2.0))[来源]
torchvision.transforms.
GaussianBlur
¶ 使用随机选择的高斯模糊来模糊图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., C, H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: 返回: 输入图像的高斯模糊版本。
返回类型: PIL 图像或张量
仅在 PIL 图像上变换¶
仅在 torch.*Tensor 上进行转换¶
-
类 (transformation_matrix, mean_vector)[来源]
torchvision.transforms.
LinearTransformation
¶ 使用方变换矩阵和计算mean_vector变换张量图像 离线。 给定 transformation_matrix 和 mean_vector,将使 torch.*Tensor 和 从中减去 mean_vector,然后计算点 product 替换为变换矩阵,然后将张量重塑为其 原形。
- 应用:
- 白化变换:假设 X 是一个以 0 为中心的列向量数据。 然后计算数据协方差矩阵 [D x D],其中 torch.mm(X.t(), X), 对此矩阵执行 SVD 并将其作为 transformation_matrix 传递。
参数: - transformation_matrix (Tensor) – 张量 [D x D], D = C x H x W
- mean_vector (Tensor) – 张量 [D], D = C x H x W
-
类 (mean, std, inplace=False)[来源]
torchvision.transforms.
Normalize
¶ 使用平均值和标准差对张量图像进行归一化。 给定 mean: 和 std: 对于通道,此转换将对 input 的每个通道进行归一化,即
(mean[1],...,mean[n])
(std[1],..,std[n])
n
torch.*Tensor
output[channel] = (input[channel] - mean[channel]) / std[channel]
注意
这种转换的行为不合适,即它不会改变 Importing 张量。
参数: - - mean (sequence) - 每个通道的均值序列。
- std (sequence) - 每个通道的标准差序列。
- inplace (bool,optional) – Bool 就地执行此作。
-
类 (p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0, inplace=False)[来源]
torchvision.transforms.
RandomErasing
¶ 在图像中随机选择一个矩形区域并擦除其像素。 Zhong 等人的“Random Erasing Data Augmentation”。查看 https://arxiv.org/abs/1708.04896
参数: - p – 执行随机擦除作的概率。
- scale – 擦除区域与输入图像的比例范围。
- ratio – 擦除区域的纵横比范围。
- value – 擦除值。默认值为 0。如果为单个 int,则用于 擦除所有像素。如果元组长度为 3,则用于擦除 R、G、B 通道。 如果 str 为 'random',则擦除具有随机值的每个像素。
- inplace – 布尔值,使此转换就地。默认值设置为 False。
返回: 擦除的图像。
例
>>> transform = transforms.Compose([ >>> transforms.RandomHorizontalFlip(), >>> transforms.ToTensor(), >>> transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), >>> transforms.RandomErasing(), >>> ])
-
类 (dtype: torch.dtype) → 无[来源]
torchvision.transforms.
ConvertImageDtype
¶ 将张量图像转换为给定的图像并相应地缩放值
dtype
参数: dtype (torch.dpython:type) – 输出的所需数据类型 注意
从较小的整数转换为较大的整数时,最大值不会精确映射。 如果来回转换,则此不匹配不起作用。
dtype
提高: RuntimeError
– 尝试投射到 或 作为 以及尝试转换为 。这些转化可能会导致 overflow 错误,因为浮点无法存储整个范围内的连续整数 整数 。torch.float32
torch.int32
torch.int64
torch.float64
torch.int64
dtype
dtype
转换转换¶
-
class (mode=None)[来源]
torchvision.transforms.
ToPILImage
¶ 将 tensor 或 ndarray 转换为 PIL 图像。此转换不支持 torchscript。
转换形状为 C x H x W 的 torch.*Tensor 或形状为 H x W x C 添加到 PIL 图像,同时保留值范围。
参数: 模式 (PIL.Image mode) – 输入数据的色彩空间和像素深度(可选)。 如果 is (默认),则对 input 数据进行了一些假设: - 如果输入有 4 个通道,则假定 为 。 - 如果输入有 3 个通道,则假定 为 。 - 如果输入有 2 个通道,则假定 为 。 - 如果输入有 1 个通道,则 由数据类型(即 、 、 )确定。 mode
None
mode
RGBA
mode
RGB
mode
LA
mode
int
float
short
-
类 [来源]
torchvision.transforms.
ToTensor
¶ 将 or 转换为 tensor。此转换不支持 torchscript。
PIL Image
numpy.ndarray
转换范围内的 PIL 图像或 numpy.ndarray (H x W x C) [0, 255] 到Torch。形状为 (C x H x W) 在 [0.0, 1.0] 范围内的 FloatTensor 如果 PIL 图像属于以下模式之一(L、LA、P、I、F、RGB、YCbCr、RGBA、CMYK、1) 或者如果 numpy.ndarray 具有 dtype = np.uint8
在其他情况下,将返回张量而不进行缩放。
注意
由于输入图像缩放为 [0.0, 1.0],因此在以下情况下不应使用此转换 变换目标图像蒙版。请参阅有关实现图像遮罩转换的参考资料。
泛型变换¶
函数转换¶
函数转换可让您对转换管道进行精细控制。 与上述转换相反,函数转换不包含随机数 generator 的参数。 这意味着您必须指定/生成所有参数,但您可以重用函数式转换。
例: 您可以将具有相同参数的函数转换应用于多个图像,如下所示:
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
例: 您可以使用 Functional transform 来构建具有自定义行为的 transform 类:
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])
-
torchvision.transforms.functional.
adjust_brightness
(img:Torch。Tensor,brightness_factor:float) → torch。张量[来源]¶ 调整图像的亮度。
参数: - img (PIL Image or Tensor) – 要调整的图像。
- brightness_factor (float) (浮动) – 调整亮度的程度。可以是 任何非负数。0 表示黑色图像,1 表示 原始图像 2 时,亮度增加 2 倍。
返回: 亮度调整图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
adjust_contrast
(img:Torch。Tensor,contrast_factor:float)→ torch。张量[来源]¶ 调整图像的对比度。
参数: - img (PIL Image or Tensor) – 要调整的图像。
- contrast_factor (float) (浮动) – 调整对比度的程度。可以是任何 非负数。0 表示纯灰色图像,1 表示 原始图像 (2) 将对比度提高 2 倍。
返回: 对比度调整的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
adjust_gamma
(img:Torch。张量、gamma:浮点数、增益:浮点数 = 1) → torch。张量[来源]¶ 对图像执行 Gamma 校正。
也称为幂律变换。调整 RGB 模式下的强度 基于以下公式:
\[I_{\text{out}} = 255 \times \text{gain} \times \left(\frac{I_{\text{in}}}{255}\right)^{\gamma}\]有关更多详细信息,请参阅 Gamma 校正。
参数: 返回: Gamma 校正调整后的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
adjust_hue
(img:Torch。Tensor,hue_factor:float)→ torch。张量[来源]¶ 调整图像的色相。
通过将图像转换为 HSV 和 周期性地改变色相通道 (H) 中的强度。 然后,图像将转换回原始图像模式。
hue_factor 是 H 通道中的偏移量,并且必须位于 区间 [-0.5, 0.5]。
有关更多详细信息,请参阅 Hue。
参数: - img (PIL Image or Tensor) – 要调整的图像。
- hue_factor (float) – 偏移色调通道的量。应为 [-0.5, 0.5].0.5 和 -0.5 表示 色调通道的完全反转 HSV 空间分别在正方向和负方向上。 0 表示无偏移。因此,-0.5 和 0.5 都会给出一个图像 替换为互补色,而 0 则提供原始图像。
返回: 色相调整图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
adjust_saturation
(img:Torch。Tensor,saturation_factor:float)→ torch。张量[来源]¶ 调整图像的颜色饱和度。
参数: - img (PIL Image or Tensor) – 要调整的图像。
- saturation_factor (float) ( (float) (浮点数) ) – 调整饱和度的程度。0 将 给出黑白图像,1 将给出原始图像,而 2 会将饱和度提高 2 倍。
返回: 饱和度调整图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
affine
(img:Torch。张量、角度:浮点数、平移量:List[int]、缩放:浮点数、剪切力:List[float]、重采样:int = 0、fillcolor:Union[int, NoneType] = None) → torch。张量[来源]¶ 对图像应用仿射变换,保持图像中心不变。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - img (PIL Image or Tensor) – 要转换的图像。
- angle (float or int) - 顺时针方向上-180到180之间的旋转角度,以度为单位。
- translate (list or tuple of python:integers) – 水平和垂直平移 (旋转后平移)
- scale (float) – 整体比例
- shear (float or tuple or list) – 剪切角值,以 -180 到 180 之间的度数表示,顺时针方向。 如果指定了 list 的元组,则第一个值对应于平行于 x 轴的剪切,而 第二个值对应于平行于 Y 轴的剪切。
- resample ( or or , optional) – 可选的重新采样筛选条件。有关更多信息,请参阅筛选条件。
如果省略,或者图像是 PIL 图像且模式为 “1” 或 “P”,则将其设置为 。
如果 input 为 Tensor,则仅支持 和 。
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
PIL.Image.NEAREST
PIL.Image.NEAREST
PIL.Image.BILINEAR
- fillcolor (int) – 输出图像中转换外部区域的可选填充颜色 (Pillow>=5.0.0)。 Tensor input 不支持此选项。输出中变换外部区域的 Fill 值 image 始终为 0。
返回: 转换后的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
center_crop
(img:Torch。Tensor,output_size:List[int]) → torch。张量[来源]¶ 在中心裁剪给定的图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - img (PIL Image or Tensor) – 要裁剪的图像。
- output_size (sequence or int) – 裁剪框的 (height, width) 。如果 int 或序列包含单个 int 它用于两个方向。
返回: 裁剪的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
convert_image_dtype
(图片:Torch。Tensor,dtype: torch.dtype = torch.float32) → torch。张量[来源]¶ 将张量图像转换为给定的图像并相应地缩放值
dtype
参数: - 图像 (Torch.Tensor) – 要转换的图像
- dtype (torch.dpython:type) – 输出的所需数据类型
返回: 转换后的图像
返回类型: 张肌
注意
从较小的整数转换为较大的整数时,最大值不会精确映射。 如果来回转换,则此不匹配不起作用。
dtype
提高: RuntimeError
– 尝试投射到 或 作为 以及尝试转换为 。这些转化可能会导致 overflow 错误,因为浮点无法存储整个范围内的连续整数 整数 。torch.float32
torch.int32
torch.int64
torch.float64
torch.int64
dtype
dtype
-
torchvision.transforms.functional.
crop
(img:Torch。Tensor, top: int, left: int, height: int, width: int) → torch 的 Tensor, top: int, left: int, height: int.张量[来源]¶ 在指定位置和输出大小裁剪给定图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸
参数: 返回: 裁剪的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
erase
(img:Torch。张量, i: int, j: int, h: int, w: int, v: torch.Tensor,就地:bool = False)→ torch。张量[来源]¶ 擦除具有给定值的输入 Tensor Image。
参数: 返回: 擦除的图像。
返回类型: 张量图像
-
torchvision.transforms.functional.
five_crop
(img:Torch。张量,大小:List[int]) → Tuple[torch.Tensor、torch 的 Tensor 和 Torch 的 TTensor、torch 的 Tensor 和 Torch 的 TTensor、torch 的 Tensor 和 Torch 的 TTensor、torch 的 Tensor 和 Torch 的 T张量][来源]¶ 将给定的图像裁剪为四个角,并进行中央裁剪。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
注意
此转换返回一个图像元组,并且可能存在一个 输入和目标数量不匹配,您的回报。
Dataset
参数: - img (PIL Image or Tensor) – 要裁剪的图像。
- size (sequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
返回: - 元组 (TL, TR, BL, BR, CENTER)
相应的左上、右上、左下、右下和居中裁剪。
返回类型:
-
torchvision.transforms.functional.
gaussian_blur
(img:Torch。张量,kernel_size:List[int],sigma:Union[List[float],NoneType] = None)→ torch。张量[来源]¶ 通过给定内核对 img 执行高斯模糊处理。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - img (PIL Image or Tensor) – 要模糊的图像
- kernel_size (sequence of python:ints or int) - 高斯核大小。可以是整数序列
like 或方形内核的单个整数。
在 torchscript 模式下kernel_size由于不支持单个 int,请使用 tuples 或
长度 1 列表:。
(kx, ky)
[ksize, ]
- sigma ( python:floats 或 float 序列,可选 ) - 高斯核标准差。可以是
floats 序列(如)或单个浮点数来定义
在 X/Y 两个方向上具有相同的 sigma。如果为 None,则使用 as 计算它。
Default (默认值) 和 None (无)。在 torchscript 模式下,sigma 作为单个浮点数为
不支持,请使用长度为 1: 的元组或列表。
(sigma_x, sigma_y)
kernel_size
sigma = 0.3 * ((kernel_size - 1) * 0.5 - 1) + 0.8
[sigma, ]
返回: 图像的 Gaussian Blurred 版本。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
hflip
(img:Torch。Tensor) → torch 的 Tensor 中。张量[来源]¶ 水平翻转给定的 PIL 图像或 Tensor。
参数: img (PIL Image or Tensor) – 要翻转的图像。如果 img 是一个 Tensor,它应该为 [..., H, W] 格式, 哪里。。。表示它可以有任意数量的尾随 尺寸。 返回: 水平翻转的图像。 返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
normalize
(张量:Torch。张量,均值:List[float],标准:List[float],就地:bool = False)→ torch。张量[来源]¶ 使用平均值和标准差对张量图像进行归一化。
注意
默认情况下,此转换的行为不合适,即它不会改变 Importing 张量。
看
Normalize
了解更多详情。参数: - tensor (Tensor) - 要归一化的大小 (C, H, W) 或 (B, C, H, W) 的张量图像。
- - mean (sequence) - 每个通道的均值序列。
- std (sequence) - 每个通道的标准差序列。
- inplace (bool,optional) – Bool 使此作就地进行。
返回: 归一化张量图像。
返回类型: 张肌
-
torchvision.transforms.functional.
pad
(img:Torch。张量,填充:List[int],填充:int = 0,padding_mode:str = 'constant')→ torch。张量[来源]¶ 用给定的 “pad” 值在给定的图像的所有侧面填充。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - img (PIL Image or Tensor) – 要填充的图像。
- padding (int or tuple or list) – 每个边框上的填充。如果提供了单个 int
用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充
分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组
这分别是 left、top、right 和 bottom 边框的填充。
在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或
长度 1 列表:。
[padding, ]
- fill (int or str or tuple) – 常量填充的像素填充值。默认值为 0。如果 length 3,分别用于填充 R、G、B 通道。 仅当 padding_mode 为 constant 时,才使用此值。Tensor 仅支持 int 值。
- padding_mode –
填充类型。应为:constant、edge、reflect 或 symmetric。默认值为 constant。 Tensor 输入尚不支持 Mode symmetric。
- 常数:具有常数值的焊盘,该值由 fill 指定
- edge:图像边缘上最后一个值的填充
- reflect:带有图像反射的焊盘(不重复边缘上的最后一个值)填充 [1, 2, 3, 4],在反射模式下两侧有 2 个元素 将导致 [3, 2, 1, 2, 3, 4, 3, 2]
- symmetric: 带有图像反射的焊盘(在边缘重复最后一个值)填充 [1, 2, 3, 4],对称模式下两侧 2 个元素 将产生 [2, 1, 1, 2, 3, 4, 4, 3]
返回: 填充图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
perspective
(img:Torch。张量,起点:List[List[int]],端点:List[List[int]],插值:int = 2,填充:Union[int, NoneType] = None)→ torch。张量[来源]¶ 执行给定图像的透视变换。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - img (PIL Image or Tensor) – 要转换的图像。
- startpoints (list of list of python:ints) – 包含四个列表的列表,每个列表包含两个整数,对应于原始图像的四个角。
[top-left, top-right, bottom-right, bottom-left]
- endpoints (list of list of python:ints) – 包含四个两个整数列表的列表,对应于转换后的图像的四个角。
[top-left, top-right, bottom-right, bottom-left]
- - interpolation (int) - 插值类型。如果 input 为 Tensor,则仅支持 和 。默认值,用于 PIL 图像和 Tensor。
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BILINEAR
- fill (n-tuple or int or float) – 旋转区域之外区域的像素填充值
图像。如果为 int 或 float,则该值分别用于所有波段。
此选项仅适用于 。Tensor 不支持此选项
输入。输出图像中变换外部区域的填充值始终为 0。
pillow>=5.0.0
返回: 转换后的 Image。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
pil_to_tensor
(pic)[来源]¶ 将 a 转换为相同类型的张量。
PIL Image
有关更多详细信息,请参阅。
PILToTensor
参数: pic (PIL Image) - 要转换为张量的图像。 返回: 转换后的图像。 返回类型: 张肌
-
torchvision.transforms.functional.
resize
(img:Torch。张量,大小:List[int],插值:int = 2)→ torch。张量[来源]¶ 将输入图像的大小调整为给定的大小。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
参数: - img (PIL Image or Tensor) – 要调整大小的图像。
- size (sequence or int) – 所需的输出大小。如果 size 是类似于
(h, w) 时,输出大小将与此大小匹配。如果 size 是 int,则
图像的较小边缘将与此数字匹配,保持
纵横比。即,如果 height > width,则 image 将被重新缩放为 。
在 torchscript 模式下,大小为不支持单个 int,请使用 tuples 或
长度 1 列表:。
[size, ]
- interpolation (int, optional) – 由过滤器定义的所需插值枚举。
默认值为 。如果 input 为 Tensor,则仅支持 和 。
PIL.Image.BILINEAR
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
返回: 调整大小的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
resized_crop
(img:Torch。张量,顶部:int,左侧:int,高度:int,宽度:int,大小:List[int],插值:int = 2)→ torch。张量[来源]¶ 裁剪给定的图像并将其调整为所需的大小。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
特别用于
RandomResizedCrop
.参数: - img (PIL Image or Tensor) – 要裁剪的图像。(0,0) 表示图像的左上角。
- top (int) – 裁剪框左上角的垂直分量。
- left (int) – 裁剪框左上角的水平分量。
- height (int) – 裁剪框的高度。
- width (int) – 裁剪框的宽度。
- size (sequence or int) – 所需的输出大小。与 .
resize
- interpolation (int, optional) – 由过滤器定义的所需插值枚举。
默认值为 。如果 input 为 Tensor,则仅支持 和 。
PIL.Image.BILINEAR
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
返回: 裁剪的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
rgb_to_grayscale
(img:Torch。Tensor,num_output_channels:int = 1) → torch。张量[来源]¶ 将 RGB 图像转换为图像的灰度版本。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
注意
请注意,此方法仅支持 RGB 图像作为输入。对于其他色彩空间中的输入, 请考虑将 meth:~torchvision.transforms.functional.to_grayscale 与 PIL Image 一起使用。
参数: - img (PIL Image or Tensor) – 要转换为灰度的 RGB 图像。
- num_output_channels (int) - 输出图像的通道数。值可以是 1 或 3。默认为 1。
返回: - 图像的灰度版本。
如果 num_output_channels = 1 :返回的图像是单通道
如果 num_output_channels = 3 : 返回的图像是 r = g = b 的 3 通道
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
rotate
(img:Torch。张量、角度:浮点、重新采样:int = 0、expand:bool = False、中心:Union[List[int], NoneType] = None、fill: Union[int, NoneType] = None) → torch。张量[来源]¶ 按角度旋转图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。
参数: - img (PIL Image or Tensor) – 要旋转的图像。
- angle (float or int) - 逆时针旋转角度值,以度为单位。
- resample ( or or , optional) – 可选的重新采样筛选条件。有关更多信息,请参阅筛选条件。
如果省略,或者图像具有模式“1”或“P”,则将其设置为 。
PIL.Image.NEAREST
PIL.Image.BILINEAR
PIL.Image.BICUBIC
PIL.Image.NEAREST
- expand (bool, optional) – 可选的扩展标志。 如果为 true,则扩展输出图像,使其足够大以容纳整个旋转的图像。 如果为 false 或省略,则使输出图像与输入图像的大小相同。 请注意,expand 标志假定绕中心旋转且无平移。
- center (list or tuple, optional) – 可选的旋转中心。原点 是左上角。 默认值是图像的中心。
- fill (n-tuple or int or float) – 旋转区域之外区域的像素填充值
图像。如果为 int 或 float,则该值分别用于所有波段。
对于所有波段,默认值为 0。此选项仅适用于 。
Tensor input 不支持此选项。输出中变换外部区域的 Fill 值
image 始终为 0。
pillow>=5.2.0
返回: 旋转的图像。
返回类型: PIL 图像或张量
-
torchvision.transforms.functional.
ten_crop
(img:Torch。张量,大小:List[int],vertical_flip:bool = False)→ List[torch.张量][来源]¶ 从给定图像生成 10 张裁剪图像。 将给定图像裁剪为四个角,中间裁剪加上 这些的 flipped 版本(默认使用水平翻转)。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸
注意
此转换返回一个图像元组,并且可能存在一个 输入和目标数量不匹配,您的回报。
Dataset
参数: 返回: - 元组 (TL, TR, BL, BR, CENTER, tl_flip, tr_flip, bl_flip, br_flip, center_flip)
对应的左上角、右上角、左下角、右下角和 居中裁剪,翻转后的图像也是如此。
返回类型:
-
torchvision.transforms.functional.
to_grayscale
(img, num_output_channels=1)[来源]¶ 将任何模式(RGB、HSV、LAB 等)的 PIL 图像转换为图像的灰度版本。
参数: - img (PIL Image) – 要转换为灰度的 PIL 图像。
- num_output_channels (int) - 输出图像的通道数。值可以是 1 或 3。默认为 1。
返回: - 图像的灰度版本。
如果 num_output_channels = 1 :返回的图像是单通道
如果 num_output_channels = 3 : 返回的图像是 r = g = b 的 3 通道
返回类型: 太平船务图片
-
torchvision.transforms.functional.
to_pil_image
(pic, mode=None)[来源]¶ 将 tensor 或 ndarray 转换为 PIL 图像。
看
ToPILImage
了解更多详情。参数: - pic (Tensor 或 numpy.ndarray) – 要转换为 PIL 图像的图像。
- 模式 (PIL.Image mode) – 输入数据的色彩空间和像素深度(可选)。
返回: 图像转换为 PIL 图像。 返回类型: 太平船务图片
-
torchvision.transforms.functional.
to_tensor
(pic)[来源]¶ 将 or 转换为 tensor。
PIL Image
numpy.ndarray
有关更多详细信息,请参阅。
ToTensor
参数: pic (PIL Image 或 numpy.ndarray) - 要转换为张量的图像。 返回: 转换后的图像。 返回类型: 张肌