目录

torchvision.transforms

转换是常见的图像转换。它们可以使用Compose. 此外,还有torchvision.transforms.functional模块。 函数转换提供对转换的精细控制。 如果您必须构建更复杂的转换管道,这将非常有用 (例如,在分段任务的情况下)。

所有转换都接受 PIL Image、Tensor Image 或批量 Tensor Image 作为输入。Tensor Image 是一个具有形状的张量,其中 是多个通道,是图像的高度和宽度。批次 Tensor Images 是形状的张量,其中 是批次中的图像数量。Deterministic 或 应用于该批次的 Tensor Images 的随机变换与该批次的所有图像的变换相同。(C, H, W)CHW(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.SequentialCompose.

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.TensorPIL.Image

对于要与 一起使用的任何自定义转换,它们应派生自 。torch.jit.scripttorch.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.TensorPIL.Image

在 PIL Image 和 torch.*Tensor 上进行转换

class size[来源]torchvision.transforms.CenterCrop

在中心裁剪给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

参数:sizesequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
forward(img[来源]
参数:imgPIL Image or Tensor) – 要裁剪的图像。
返回:裁剪的图像。
返回类型:PIL 图像或张量
类别亮度=0对比度=0饱和度=0色调=0[来源]torchvision.transforms.ColorJitter

随机更改图像的亮度、对比度和饱和度。

参数:
  • brightnessfloat or tuple of python:floatminmax) - 亮度抖动的程度。 brightness_factor 从 [max(0, 1 - brightness), 1 + brightness] 中统一选择 或给定的 [min, max]。应为非负数。
  • contrastfloat or tuple of python:floatminmax) – 抖动对比度的程度。 contrast_factor 从 [max(0, 1 - contrast), 1 + contrast] 中统一选择 或给定的 [min, max]。应为非负数。
  • saturationfloat or tuple of python:floatminmax) – 抖动饱和度的程度。 saturation_factor 从 [max(0, 1 - saturation), 1 + saturation] 中统一选择 或给定的 [min, max]。应为非负数。
  • huefloat or tuple of python:floatminmax) – 色调抖动的程度。 hue_factor 是从 [-hue, hue] 或给定的 [min, max] 中统一选择的。 应具有 0<= 色相 <= 0.5 或 -0.5 <= 最小值 <= 最大值 <= 0.5。
forward(img[来源]
参数:imgPIL Image or Tensor) – 输入图像。
返回:颜色抖动图像。
返回类型:PIL 图像或张量
静态亮度对比度饱和度色调[来源]get_params

获取要应用于图像的随机转换。

参数与 __init__ 的参数相同。

返回:Transform,用于随机调整亮度、对比度和 饱和度(按随机顺序)。
class size[来源]torchvision.transforms.FiveCrop

将给定的图像裁剪为四个角,并进行中央裁剪。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

注意

此转换返回一个图像元组,并且 inputs 并定位您的 Dataset 返回。有关如何处理的示例,请参见下文 这。

参数:sizesequence 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
forward(img[来源]
参数:imgPIL Image or Tensor) – 要裁剪的图像。
返回:5 张图像的元组。Image 可以是 PIL Image 或 Tensor
num_output_channels=1[来源]torchvision.transforms.Grayscale

将图像转换为灰度。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., 3, H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:num_output_channelsint) – 输出图像所需的(1 或 3)个通道数
返回:
输入的灰度版本。
  • 如果 :返回的图像是单通道num_output_channels == 1
  • 如果 : 返回的图像是 3 通道,其中 r == g == bnum_output_channels == 3
返回类型:太平船务图片
forward(img[来源]
参数:imgPIL Image or Tensor) – 要转换为灰度的图像。
返回:灰度图像。
返回类型:PIL 图像或张量
paddingfill=0padding_mode='constant'[来源]torchvision.transforms.Pad

用给定的 “pad” 值在给定的图像的所有侧面填充。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • paddingint or tuple or list) – 每个边框上的填充。如果提供了单个 int 用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充 分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组 这分别是 left、top、right 和 bottom 边框的填充。 在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或 长度 1 列表:。[padding, ]
  • fillint or tuple) – 常量填充的像素填充值。默认值为 0。如果 length 3,分别用于填充 R、G、B 通道。 仅当 padding_mode 为 constant 时,才使用此值
  • padding_modestr) –

    填充类型。应为: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]
forward(img[来源]
参数:imgPIL Image or Tensor) – 要填充的图像。
返回:填充图像。
返回类型:PIL 图像或张量
class degreestranslate=Nonescale=Noneshear=Noneresample =0fillcolor=0[来源]torchvision.transforms.RandomAffine

图像的随机仿射变换保持中心不变。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。

参数:
  • degreessequence or float or int) - 可供选择的度数范围。 如果 degrees 是一个数字而不是序列,如 (min, max),则度的范围 将为 (-degrees, +degrees)。设置为 0 可停用旋转。
  • translatetupleoptional) - 水平最大绝对分数的元组 和垂直翻译。例如 translate=(a, b),然后水平偏移 在 -img_width * a < dx < img_width * a 范围内随机采样,垂直偏移为 在 -img_height * b < dy < img_height * b 范围内随机采样。默认情况下不会翻译。
  • scaletupleoptional) - 缩放因子间隔,例如 (a, b),则 scale 为 从范围 A <= scale <= b 中随机采样。默认情况下将保持原始比例。
  • shearsequence or float or intoptional) – 可供选择的度数范围。 如果 shear 是一个数字,则为平行于范围内 x 轴的剪切 (-shear, +shear) 将应用。否则,如果 shear 是一个元组或 2 个值的列表,则 shear 平行于 范围 (shear[0], shear[1])。否则,如果 shear 是一个 4 个值的元组或列表,则 将应用 x 轴剪切输入 (shear[0], shear[1]) 和 y 轴剪切输入 (shear[2], shear[3])。 默认情况下不会应用剪切。
  • resampleintoptional) – 可选的重采样筛选条件。有关更多信息,请参阅筛选条件。 如果省略,或者图像具有模式“1”或“P”,则将其设置为 。 如果 input 为 Tensor,则仅支持 和 。PIL.Image.NEARESTPIL.Image.NEARESTPIL.Image.BILINEAR
  • fillcolortuple or int) – 区域的可选填充颜色 (RGB 图像的 Tuple 和灰度的 int 在输出图像中的变换之外 (Pillow>=5.0.0)。Tensor 不支持此选项 输入。输出图像中变换外部区域的填充值始终为 0。
forward(img[来源]
img (PIL Image or Tensor):要转换的图像。
返回:仿射变换图像。
返回类型:PIL 图像或张量
static 度数: List[float], 翻译: Union[List[float], NoneType], scale_ranges: Union[List[float], NoneType], 剪切: Union[List[float[float], NoneType], img_size: List[int] → Tuple[float, Tuple[int, int], float, Tuple[float, float]][源]get_params

获取仿射变换的参数

返回:传递给仿射变换的 params
转换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.TensorPIL.Image

参数:
  • transformslisttupletorch.nn.Module) – 转换列表
  • pfloat) – 概率
class sizepadding=Nonepad_if_needed=Falsefill=0padding_mode='constant'[来源]torchvision.transforms.RandomCrop

在随机位置裁剪给定的图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:
  • sizesequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
  • paddingintsequence可选) – 每个边框上的可选填充 的图像。默认值为 None。如果提供了单个 int 用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充 分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组 这分别是 left、top、right 和 bottom 边框的填充。 在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或 长度 1 列表:。[padding, ]
  • pad_if_neededboolean) – 如果小于 所需的大小来避免引发异常。由于裁剪已完成 填充后,填充似乎以随机偏移量完成。
  • fillint or tuple) – 常量填充的像素填充值。默认值为 0。如果 length 3,分别用于填充 R、G、B 通道。 仅当 padding_mode 为 constant 时,才使用此值
  • padding_modestr) –

    填充类型。应为: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]
forward(img[来源]
参数:imgPIL Image or Tensor) – 要裁剪的图像。
返回:裁剪的图像。
返回类型:PIL 图像或张量
静态IMG:Torch。张量,output_size:元组[int, int] →元组[int, int, int, int][来源]get_params

获取随机裁剪的参数。crop

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。
  • output_sizetuple) – 作物的预期输出大小。
返回:

要传递给 Random Crop 的参数 (i, j, h, w)。crop

返回类型:

等级 P=0.1[来源]torchvision.transforms.RandomGrayscale

将图像随机转换为灰度,概率为 p(默认为 0.1)。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., 3, H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:pfloat) - 图像应转换为灰度的概率。
返回:输入图像的灰度版本,概率为 p 且未更改 与概率 (1-p)。 - 如果输入图像为 1 通道:灰度版本为 1 通道 - 如果输入图像为 3 通道:灰度版本为 3 通道,其中 r == g == b
返回类型:PIL 图像或张量
forward(img[来源]
参数:imgPIL Image or Tensor) – 要转换为灰度的图像。
返回:随机灰度图像。
返回类型:PIL 图像或张量
等级 P=0.5[来源]torchvision.transforms.RandomHorizontalFlip

以给定的概率随机水平翻转给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:pfloat) - 图像被翻转的概率。默认值为 0.5
forward(img[来源]
参数:imgPIL Image or Tensor) – 要翻转的图像。
返回:随机翻转的图像。
返回类型:PIL 图像或张量
distortion_scale=0.5,p=0.5插值=2填充=0[来源]torchvision.transforms.RandomPerspective

以给定的概率对给定图像执行随机透视变换。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。

参数:
  • distortion_scalefloat) – 控制失真程度的参数,范围从 0 到 1。 默认值为 0.5。
  • pfloat) - 图像被转换的概率。默认值为 0.5。
  • - interpolationint) - 插值类型。如果 input 为 Tensor,则仅支持 和 。默认值,用于 PIL 图像和 Tensor。PIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BILINEAR
  • filln-tuple or int or float) – 旋转区域之外区域的像素填充值 图像。如果为 int 或 float,则该值分别用于所有波段。默认值为 0。 此选项仅适用于 。Tensor 不支持此选项 输入。输出图像中变换外部区域的填充值始终为 0。pillow>=5.0.0
forward(img[来源]
参数:imgPIL Image or Tensor) – 要透视变换的图像。
返回:随机变换的图像。
返回类型:PIL 图像或张量
static width: intheight: intdistortion_scale: float → Tuple[List[List[int]], List[List[int]]][源]get_params

获取随机透视变换的参数。perspective

参数:
  • - widthint) - 图像的宽度。
  • heightint) - 图像的高度。
  • distortion_scalefloat) – 控制失真程度的参数,范围从 0 到 1。
返回:

包含原始图像的 [top-left, top-right, bottom-right, bottom-left] 的列表, 包含转换后的图像的 [top-left, top-right, bottom-right, bottom-left] 的列表。

大小比例=(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 网络。

参数:
  • sizeintsequence) - 每条边的预期输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,则方形输出大小为 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。(size, size)
  • scaletuple of python:float) - 裁剪的原点大小的大小范围
  • ratiotuple of python:float) - 裁剪的原点纵横比的纵横比范围。
  • interpolationint) – 由过滤器定义的所需插值枚举。 默认值为 。如果 input 为 Tensor,则仅支持 和 。PIL.Image.BILINEARPIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BICUBIC
forward(img[来源]
参数:imgPIL Image or Tensor) – 要裁剪和调整大小的图像。
返回:随机裁剪和调整大小的图像。
返回类型:PIL 图像或张量
静态IMG:Torch。张量,比例:List[float],比率:List[float] → Tuple[int, int, int, int] [来源]get_params

获取随机大小的裁剪的参数。crop

参数:
  • imgPIL Image or Tensor) – 输入图像。
  • scalelist) - 裁剪的原点大小的比例范围
  • ratiolist) - 裁剪的原始纵横比的纵横比范围
返回:

要传递给 random 的参数 (i, j, h, w)crop

大小裁剪。

返回类型:

class degreesresample=Falseexpand=Falsecenter=Nonefill=None[来源]torchvision.transforms.RandomRotation

按角度旋转图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导维度。

参数:
  • degreessequence or float or int) - 可供选择的度数范围。 如果 degrees 是一个数字而不是序列,如 (min, max),则度的范围 将为 (-degrees, +degrees)。
  • resampleintoptional) – 可选的重采样筛选条件。有关更多信息,请参阅筛选条件。 如果省略,或者图像具有模式“1”或“P”,则将其设置为 PIL。Image.NEAREST 的 如果 input 为 Tensor,则仅支持 和 。PIL.Image.NEARESTPIL.Image.BILINEAR
  • expandbooloptional) – 可选的扩展标志。 如果为 true,则扩展输出,使其足够大以容纳整个旋转的图像。 如果为 false 或省略,则使输出图像与输入图像的大小相同。 请注意,expand 标志假定绕中心旋转且无平移。
  • centerlist or tupleoptional) – 可选的旋转中心 (x, y)。原点 是左上角。 默认值是图像的中心。
  • filln-tuple or int or float) – 旋转区域之外区域的像素填充值 图像。如果为 int 或 float,则该值分别用于所有波段。 对于所有波段,默认值为 0。此选项仅适用于 Pillow>=5.2.0。 Tensor input 不支持此选项。输出中变换外部区域的 Fill 值 image 始终为 0。
forward(img[来源]
参数:imgPIL Image or Tensor) – 要旋转的图像。
返回:旋转的图像。
返回类型:PIL 图像或张量
static 度数: List[float] → float[source]get_params

获取随机旋转的参数。rotate

返回:angle 参数进行随机旋转。rotate
返回类型:
class *args**kwargs[来源]torchvision.transforms.RandomSizedCrop

注意:此转换已弃用,取而代之的是 RandomResizedCrop。

等级 P=0.5[来源]torchvision.transforms.RandomVerticalFlip

以给定的概率随机垂直翻转给定的图像。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:pfloat) - 图像被翻转的概率。默认值为 0.5
forward(img[来源]
参数:imgPIL Image or Tensor) – 要翻转的图像。
返回:随机翻转的图像。
返回类型:PIL 图像或张量
class sizeinterpolation=2[来源]torchvision.transforms.Resize

将输入图像的大小调整为给定的大小。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • sizesequence or int) – 所需的输出大小。如果 size 是类似于 (h, w) 时,output size 将与此匹配。如果 size 是 int,则 图像的较小边缘将与此数字匹配。 即,如果 height > width,则 image 将被重新缩放为 (尺寸 * 高度 / 宽度、大小)。 在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或 长度 1 列表:。[size, ]
  • interpolationintoptional) – 由过滤器定义的所需插值枚举。 默认值为 。如果 input 为 Tensor,则仅支持 和 。PIL.Image.BILINEARPIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BICUBIC
forward(img[来源]
参数:imgPIL Image or Tensor) – 要缩放的图像。
返回:重新缩放的图像。
返回类型:PIL 图像或张量
class *args**kwargs[来源]torchvision.transforms.Scale

注意:此转换已弃用,取而代之的是 Resize。

class sizevertical_flip=False[来源]torchvision.transforms.TenCrop

将给定的图像裁剪为四个角,并进行中央裁剪加上翻转的 这些(默认使用水平翻转)。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

注意

此转换返回一个图像元组,并且 inputs 并定位您的 Dataset 返回。有关如何处理的示例,请参见下文 这。

参数:
  • sizesequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
  • vertical_flipbool) – 使用垂直翻转而不是水平翻转

>>> 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
forward(img[来源]
参数:imgPIL Image or Tensor) – 要裁剪的图像。
返回:10 张图像的元组。Image 可以是 PIL Image 或 Tensor
kernel_sizesigma=(0.12.0)[来源]torchvision.transforms.GaussianBlur

使用随机选择的高斯模糊来模糊图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., C, H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:
  • kernel_sizeint or sequence) - 高斯核的大小。
  • sigmafloat or tuple of python:floatminmax) – 用于的标准差 创建 kernel 以执行模糊处理。如果浮点数,则 sigma 是固定的。如果它是 tuple 的浮点数 (min, max) 中,sigma 被均匀随机地随机选择位于 给定的范围。
返回:

输入图像的高斯模糊版本。

返回类型:

PIL 图像或张量

forward(img:Torch。Tensor) → torch 的 Tensor 中。张量[来源]
参数:imgPIL Image or Tensor) – 要模糊的图像。
返回:高斯模糊图像
返回类型:PIL 图像或张量
static sigma_min: floatsigma_max: float → float[来源]get_params

选择 sigma 进行随机高斯模糊。

参数:
  • sigma_min (float) (float) ( (float) (浮点数)) – 可为模糊内核选择的最小标准差。
  • sigma_max (float) (float) ( (float) (浮点数)) – 可选择用于模糊内核的最大标准差。
返回:

要传递的标准差来计算高斯模糊的内核。

返回类型:

仅在 PIL 图像上变换

转换[来源]torchvision.transforms.RandomChoice

应用从列表中随机选取的单个转换。此转换不支持 torchscript。

转换[来源]torchvision.transforms.RandomOrder

以随机顺序应用转换列表。此转换不支持 torchscript。

仅在 torch.*Tensor 上进行转换

transformation_matrixmean_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_matrixTensor) – 张量 [D x D], D = C x H x W
  • mean_vectorTensor) – 张量 [D], D = C x H x W
forward(张量:Torch。Tensor) → torch 的 Tensor 中。张量[来源]
参数:tensorTensor) - 要白化的张量图像。
返回:转换后的图像。
返回类型:张肌
meanstdinplace=False[来源]torchvision.transforms.Normalize

使用平均值和标准差对张量图像进行归一化。 给定 mean: 和 std: 对于通道,此转换将对 input 的每个通道进行归一化,即(mean[1],...,mean[n])(std[1],..,std[n])ntorch.*Tensoroutput[channel] = (input[channel] - mean[channel]) / std[channel]

注意

这种转换的行为不合适,即它不会改变 Importing 张量。

参数:
  • - meansequence) - 每个通道的均值序列。
  • stdsequence) - 每个通道的标准差序列。
  • inplacebool,optional) – Bool 就地执行此作。
forward(张量:Torch。Tensor) → torch 的 Tensor 中。张量[来源]
参数:- tensorTensor) - 要归一化的张量图像。
返回:归一化张量图像。
返回类型:张肌
p=0.5scale=(0.020.33)ratio=(0.33.3)value=0inplace=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(),
>>> ])
forward(img[来源]
参数:imgTensor) - 要擦除的张量图像。
返回:擦除的 Tensor 图像。
返回类型:img (张量)
静态IMG:Torch。张量,比例:Tuple[float, float], ratio: Tuple[float, float], 值: Union[List[float], NoneType] = None → Tuple[int, int, int, int, int, torch.张量][来源]get_params

获取用于随机擦除的参数。erase

参数:
  • imgTensor) - 要擦除的张量图像。
  • scaletuple or list) - 擦除区域与输入图像的比例范围。
  • ratiotuple or list) - 擦除区域的纵横比范围。
  • valuelist, optional) (value (list, optional) (值可选) – 擦除值。如果为 None,则将其解释为 “random” (使用随机值擦除每个像素)。如果为 1,则将其解释为数字, 即 .len(value)value[0]
返回:

要传递给的参数 (I, J, H, W, V) 进行随机擦除。erase

返回类型:

dtype: torch.dtype → 无[来源]torchvision.transforms.ConvertImageDtype

将张量图像转换为给定的图像并相应地缩放值dtype

参数:dtypetorch.dpython:type) – 输出的所需数据类型

注意

从较小的整数转换为较大的整数时,最大值不会精确映射。 如果来回转换,则此不匹配不起作用。dtype

提高:RuntimeError– 尝试投射到 或 作为 以及尝试转换为 。这些转化可能会导致 overflow 错误,因为浮点无法存储整个范围内的连续整数 整数 。torch.float32torch.int32torch.int64torch.float64torch.int64dtypedtype

转换转换

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 个通道,则 由数据类型(即 、 、 )确定。modeNonemodeRGBAmodeRGBmodeLAmodeintfloatshort
[来源]torchvision.transforms.ToTensor

将 or 转换为 tensor。此转换不支持 torchscript。PIL Imagenumpy.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],因此在以下情况下不应使用此转换 变换目标图像蒙版。请参阅有关实现图像遮罩转换的参考资料

泛型变换

lambd[来源]torchvision.transforms.Lambda

将用户定义的 lambda 应用为转换。此转换不支持 torchscript。

参数:lambdfunction) – 用于转换的 lambda/function。

函数转换

函数转换可让您对转换管道进行精细控制。 与上述转换相反,函数转换不包含随机数 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。张量[来源]

调整图像的亮度。

参数:
  • imgPIL Image or Tensor) – 要调整的图像。
  • brightness_factor (float) (浮动) – 调整亮度的程度。可以是 任何非负数。0 表示黑色图像,1 表示 原始图像 2 时,亮度增加 2 倍。
返回:

亮度调整图像。

返回类型:

PIL 图像或张量

torchvision.transforms.functional.adjust_contrast(img:Torch。Tensor,contrast_factor:float→ torch。张量[来源]

调整图像的对比度。

参数:
  • imgPIL 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 校正

参数:
  • imgPIL Image or Tensor) – 要调整的 PIL 图像。
  • gammafloat) - 非负实数,与方程式中相同。 大于 1 的 Gamma 会使阴影更暗, 而小于 1 的 Gamma 会使暗区更亮。
  • - gainfloat) - 常数乘数。
返回:

Gamma 校正调整后的图像。

返回类型:

PIL 图像或张量

torchvision.transforms.functional.adjust_hue(img:Torch。Tensor,hue_factor:float→ torch。张量[来源]

调整图像的色相。

通过将图像转换为 HSV 和 周期性地改变色相通道 (H) 中的强度。 然后,图像将转换回原始图像模式。

hue_factor 是 H 通道中的偏移量,并且必须位于 区间 [-0.5, 0.5]。

有关更多详细信息,请参阅 Hue

参数:
  • imgPIL Image or Tensor) – 要调整的图像。
  • hue_factorfloat) – 偏移色调通道的量。应为 [-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。张量[来源]

调整图像的颜色饱和度。

参数:
  • imgPIL 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] 形状,其中 ...表示任意数量的前导维度。

参数:
  • imgPIL Image or Tensor) – 要转换的图像。
  • anglefloat or int) - 顺时针方向上-180到180之间的旋转角度,以度为单位。
  • translatelist or tuple of python:integers) – 水平和垂直平移 (旋转后平移)
  • scalefloat) – 整体比例
  • shearfloat or tuple or list) – 剪切角值,以 -180 到 180 之间的度数表示,顺时针方向。 如果指定了 list 的元组,则第一个值对应于平行于 x 轴的剪切,而 第二个值对应于平行于 Y 轴的剪切。
  • resample ( or or , optional) – 可选的重新采样筛选条件。有关更多信息,请参阅筛选条件。 如果省略,或者图像是 PIL 图像且模式为 “1” 或 “P”,则将其设置为 。 如果 input 为 Tensor,则仅支持 和 。PIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BICUBICPIL.Image.NEARESTPIL.Image.NEARESTPIL.Image.BILINEAR
  • fillcolorint) – 输出图像中转换外部区域的可选填充颜色 (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] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。
  • output_sizesequence or int) – 裁剪框的 (height, width) 。如果 int 或序列包含单个 int 它用于两个方向。
返回:

裁剪的图像。

返回类型:

PIL 图像或张量

torchvision.transforms.functional.convert_image_dtype(图片:Torch。Tensor,dtype: torch.dtype = torch.float32) → torch。张量[来源]

将张量图像转换为给定的图像并相应地缩放值dtype

参数:
  • 图像Torch.Tensor) – 要转换的图像
  • dtypetorch.dpython:type) – 输出的所需数据类型
返回:

转换后的图像

返回类型:

张肌

注意

从较小的整数转换为较大的整数时,最大值不会精确映射。 如果来回转换,则此不匹配不起作用。dtype

提高:RuntimeError– 尝试投射到 或 作为 以及尝试转换为 。这些转化可能会导致 overflow 错误,因为浮点无法存储整个范围内的连续整数 整数 。torch.float32torch.int32torch.int64torch.float64torch.int64dtypedtype
torchvision.transforms.functional.crop(img:Torch。Tensortop: intleft: intheight: intwidth: int → torch 的 Tensor, top: int, left: int, height: int.张量[来源]

在指定位置和输出大小裁剪给定图像。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导 尺寸

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。(0,0) 表示图像的左上角。
  • topint) – 裁剪框左上角的垂直分量。
  • leftint) – 裁剪框左上角的水平分量。
  • heightint) – 裁剪框的高度。
  • widthint) – 裁剪框的宽度。
返回:

裁剪的图像。

返回类型:

PIL 图像或张量

torchvision.transforms.functional.erase(img:Torch。张量i: intj: inth: intw: intv: torch.Tensor就地:bool = False→ torch。张量[来源]

擦除具有给定值的输入 Tensor Image。

参数:
  • imgTensor Image) – 要擦除的大小 (C, H, W) 的张量图像
  • iint) - i in (i,j) 即左上角的坐标。
  • jint) - (i,j) 中的 j,即左上角的坐标。
  • hint) - 擦除区域的高度。
  • wint) - 擦除区域的宽度。
  • v – 擦除值。
  • inplacebooloptional) – 用于就地作。默认情况下设置为 False。
返回:

擦除的图像。

返回类型:

张量图像

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

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。
  • sizesequence 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] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • imgPIL Image or Tensor) – 要模糊的图像
  • kernel_sizesequence of python:ints or int) - 高斯核大小。可以是整数序列 like 或方形内核的单个整数。 在 torchscript 模式下kernel_size由于不支持单个 int,请使用 tuples 或 长度 1 列表:。(kx, ky)[ksize, ]
  • sigmapython:floatsfloat 序列可选 ) - 高斯核标准差。可以是 floats 序列(如)或单个浮点数来定义 在 X/Y 两个方向上具有相同的 sigma。如果为 None,则使用 as 计算它。 Default (默认值) 和 None (无)。在 torchscript 模式下,sigma 作为单个浮点数为 不支持,请使用长度为 1: 的元组或列表。(sigma_x, sigma_y)kernel_sizesigma = 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。

参数:imgPIL Image or Tensor) – 要翻转的图像。如果 img 是一个 Tensor,它应该为 [..., H, W] 格式, 哪里。。。表示它可以有任意数量的尾随 尺寸。
返回:水平翻转的图像。
返回类型:PIL 图像或张量
torchvision.transforms.functional.normalize(张量:Torch。张量,均值:List[float],标准:List[float],就地:bool = False→ torch。张量[来源]

使用平均值和标准差对张量图像进行归一化。

注意

默认情况下,此转换的行为不合适,即它不会改变 Importing 张量。

Normalize了解更多详情。

参数:
  • tensorTensor) - 要归一化的大小 (C, H, W) 或 (B, C, H, W) 的张量图像。
  • - meansequence) - 每个通道的均值序列。
  • stdsequence) - 每个通道的标准差序列。
  • inplacebool,optional) – Bool 使此作就地进行。
返回:

归一化张量图像。

返回类型:

张肌

torchvision.transforms.functional.pad(img:Torch。张量,填充:List[int],填充:int = 0,padding_mode:str = 'constant'→ torch。张量[来源]

用给定的 “pad” 值在给定的图像的所有侧面填充。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • imgPIL Image or Tensor) – 要填充的图像。
  • paddingint or tuple or list) – 每个边框上的填充。如果提供了单个 int 用于填充所有边框。如果提供了长度为 2 的 Tuples,则这是填充 分别在 left/right 和 top/bottom 上。如果提供了长度为 4 的元组 这分别是 left、top、right 和 bottom 边框的填充。 在 torchscript 模式下,由于不支持单个 int 填充,请使用 tuples 或 长度 1 列表:。[padding, ]
  • fillint 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] 形状,其中 ...表示任意数量的前导维度。

参数:
  • imgPIL Image or Tensor) – 要转换的图像。
  • startpointslist of list of python:ints) – 包含四个列表的列表,每个列表包含两个整数,对应于原始图像的四个角。[top-left, top-right, bottom-right, bottom-left]
  • endpointslist of list of python:ints) – 包含四个两个整数列表的列表,对应于转换后的图像的四个角。[top-left, top-right, bottom-right, bottom-left]
  • - interpolationint) - 插值类型。如果 input 为 Tensor,则仅支持 和 。默认值,用于 PIL 图像和 Tensor。PIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BILINEAR
  • filln-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

参数:picPIL Image) - 要转换为张量的图像。
返回:转换后的图像。
返回类型:张肌
torchvision.transforms.functional.resize(img:Torch。张量,大小:List[int],插值:int = 2)→ torch。张量[来源]

将输入图像的大小调整为给定的大小。 图像可以是 PIL 图像或 torch 张量,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

参数:
  • imgPIL Image or Tensor) – 要调整大小的图像。
  • sizesequence or int) – 所需的输出大小。如果 size 是类似于 (h, w) 时,输出大小将与此大小匹配。如果 size 是 int,则 图像的较小边缘将与此数字匹配,保持 纵横比。即,如果 height > width,则 image 将被重新缩放为 。 在 torchscript 模式下,大小为不支持单个 int,请使用 tuples 或 长度 1 列表:。[size, ]
  • interpolationintoptional) – 由过滤器定义的所需插值枚举。 默认值为 。如果 input 为 Tensor,则仅支持 和 。PIL.Image.BILINEARPIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BICUBIC
返回:

调整大小的图像。

返回类型:

PIL 图像或张量

torchvision.transforms.functional.resized_crop(img:Torch。张量,顶部:int,左侧:int,高度:int,宽度:int,大小:List[int],插值:int = 2)→ torch。张量[来源]

裁剪给定的图像并将其调整为所需的大小。 图像可以是 PIL 图像或 Tensor,在这种情况下,它是预期的 具有 [..., H, W] 形状,其中 ...表示任意数量的前导尺寸

特别用于RandomResizedCrop.

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。(0,0) 表示图像的左上角。
  • topint) – 裁剪框左上角的垂直分量。
  • leftint) – 裁剪框左上角的水平分量。
  • heightint) – 裁剪框的高度。
  • widthint) – 裁剪框的宽度。
  • sizesequence or int) – 所需的输出大小。与 .resize
  • interpolationintoptional) – 由过滤器定义的所需插值枚举。 默认值为 。如果 input 为 Tensor,则仅支持 和 。PIL.Image.BILINEARPIL.Image.NEARESTPIL.Image.BILINEARPIL.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 一起使用。

参数:
  • imgPIL Image or Tensor) – 要转换为灰度的 RGB 图像。
  • num_output_channelsint) - 输出图像的通道数。值可以是 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] 形状,其中 ...表示任意数量的前导维度。

参数:
  • imgPIL Image or Tensor) – 要旋转的图像。
  • anglefloat or int) - 逆时针旋转角度值,以度为单位。
  • resample ( or or , optional) – 可选的重新采样筛选条件。有关更多信息,请参阅筛选条件。 如果省略,或者图像具有模式“1”或“P”,则将其设置为 。PIL.Image.NEARESTPIL.Image.BILINEARPIL.Image.BICUBICPIL.Image.NEAREST
  • expandbooloptional) – 可选的扩展标志。 如果为 true,则扩展输出图像,使其足够大以容纳整个旋转的图像。 如果为 false 或省略,则使输出图像与输入图像的大小相同。 请注意,expand 标志假定绕中心旋转且无平移。
  • centerlist or tupleoptional) – 可选的旋转中心。原点 是左上角。 默认值是图像的中心。
  • filln-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

参数:
  • imgPIL Image or Tensor) – 要裁剪的图像。
  • sizesequence or int) – 作物的所需输出大小。如果 size 是 int 而不是像 (h, w) 这样的序列,方形裁剪 (size, size) 是 䍬。如果提供长度为 1 的元组或列表,则它将被解释为 (size[0], size[0])。
  • vertical_flipbool) – 使用垂直翻转而不是水平翻转
返回:

元组 (TL, TR, BL, BR, CENTER, tl_flip, tr_flip, bl_flip, br_flip, center_flip)

对应的左上角、右上角、左下角、右下角和 居中裁剪,翻转后的图像也是如此。

返回类型:

torchvision.transforms.functional.to_grayscale(imgnum_output_channels=1[来源]

将任何模式(RGB、HSV、LAB 等)的 PIL 图像转换为图像的灰度版本。

参数:
  • imgPIL Image) – 要转换为灰度的 PIL 图像。
  • num_output_channelsint) - 输出图像的通道数。值可以是 1 或 3。默认为 1。
返回:

图像的灰度版本。

如果 num_output_channels = 1 :返回的图像是单通道

如果 num_output_channels = 3 : 返回的图像是 r = g = b 的 3 通道

返回类型:

太平船务图片

torchvision.transforms.functional.to_pil_image(picmode=None[来源]

将 tensor 或 ndarray 转换为 PIL 图像。

ToPILImage了解更多详情。

参数:
  • picTensornumpy.ndarray) – 要转换为 PIL 图像的图像。
  • 模式PIL.Image mode) – 输入数据的色彩空间和像素深度(可选)。
返回:图像转换为 PIL 图像。
返回类型:太平船务图片
torchvision.transforms.functional.to_tensor(pic[来源]

将 or 转换为 tensor。PIL Imagenumpy.ndarray

有关更多详细信息,请参阅。ToTensor

参数:picPIL Imagenumpy.ndarray) - 要转换为张量的图像。
返回:转换后的图像。
返回类型:张肌
torchvision.transforms.functional.vflip(img:Torch。Tensor) → torch 的 Tensor 中。张量[来源]

垂直翻转给定的 PIL Image 或 torch Tensor。

参数:imgPIL Image or Tensor) – 要翻转的图像。如果 img 是一个 Tensor,它应该为 [..., H, W] 格式, 哪里。。。表示它可以有任意数量的尾随 尺寸。
返回:垂直翻转的图像。
返回类型:太平船务图片

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源