目录

torchvision.ops

torchvision.ops 实现了特定于计算机视觉的操作。

注意

所有运算符都原生支持TorchScript。

torchvision.ops.batched_nms(boxes: torch.Tensor, scores: torch.Tensor, idxs: torch.Tensor, iou_threshold: float)torch.Tensor[source]

以批量方式执行非最大抑制。

每个索引值对应一个类别,且不同类别的元素之间不会应用NMS。

Parameters
  • 边界框 (张量[N, 4]) – 将在此执行非极大值抑制的边界框。它们应以(x1, y1, x2, y2)格式提供,并包含0 <= x1 < x20 <= y1 < y2

  • 得分 (张量[N]) – 每个框的得分

  • idxs (Tensor[N]) – 每个框所属类别的索引。

  • iou_threshold (float) – 丢弃所有重叠框的IoU > iou_threshold

Returns

int64 张量,其中包含通过 NMS 保留的元素的索引,并按分数从高到低排序。

Return type

张量

torchvision.ops.box_area(boxes: torch.Tensor)torch.Tensor[source]

计算一组边界框的面积,这些边界框由它们的 (x1, y1, x2, y2) 坐标指定。

Parameters

(张量[N, 4]) – 需要计算面积的框。它们应以(x1, y1, x2, y2)格式给出, 0 <= x1 < x20 <= y1 < y2

Returns

每个框的区域

Return type

Tensor[N]

torchvision.ops.box_convert(boxes: torch.Tensor, in_fmt: str, out_fmt: str)torch.Tensor[source]

将框从给定的 in_fmt 转换为 out_fmt。 支持的 in_fmt 和 out_fmt 有:

‘xyxy’:框通过角来表示,x1, y1 是左上角,x2, y2 是右下角。 这是 torchvision 工具所期望的格式。

‘xywh’:框通过左上角的坐标(x1, y2)、宽度(w)和高度(h)来表示。

‘cxcywh’:边界框通过中心、宽度和高度来表示,其中 cx 和 cy 是框的中心,w 和 h 是宽度和高度。

Parameters
  • (张量[N, 4]) – 将被转换的框。

  • in_fmt (str) – 输入框的格式。支持的格式有 [‘xyxy’, ‘xywh’, ‘cxcywh’]。

  • out_fmt (str) – 输出框的格式。支持的格式有 [‘xyxy’, ‘xywh’, ‘cxcywh’]

Returns

转换为指定格式的框。

Return type

张量[N, 4]

torchvision.ops.box_iou(boxes1: torch.Tensor, boxes2: torch.Tensor)torch.Tensor[source]

返回两组框之间的交并比(雅卡尔指数)。

两组框都期望以(x1, y1, x2, y2)格式出现,带有 0 <= x1 < x20 <= y1 < y2

Parameters
  • boxes1 (张量[N, 4]) – 第一个框集合

  • boxes2 (Tensor[M, 4]) – 第二组边界框

Returns

包含 boxes1 和 boxes2 中每个元素的成对 IoU 值的 NxM 矩阵

Return type

张量[N, M]

torchvision.ops.clip_boxes_to_image(boxes: torch.Tensor, size: Tuple[int, int])torch.Tensor[source]

将框裁剪到大小为size的图像内。

Parameters
  • (张量[N, 4]) – 以 (x1, y1, x2, y2) 格式的框,带有 0 <= x1 < x20 <= y1 < y2

  • 大小 (元组[高度, 宽度]) – 图像的大小

Returns

裁剪后的框

Return type

张量[N, 4]

torchvision.ops.deform_conv2d(input: torch.Tensor, offset: torch.Tensor, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, stride: Tuple[int, int] = (1, 1), padding: Tuple[int, int] = (0, 0), dilation: Tuple[int, int] = (1, 1), mask: Optional[torch.Tensor] = None)torch.Tensor[source]

执行可变形卷积v2,如在 可变形卷积网络v2:更可变形,更好结果 中所述,如果 mask 不等于 None,并且 执行可变形卷积,如在 可变形卷积网络 中所述,如果 mask 等于 None

Parameters
  • 输入 (张量[batch_size, in_channels, in_height, in_width]) – 输入张量

  • 偏移量 (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width, out_height, out_width]) – 在卷积核的每个位置应用的偏移量。

  • 权重 (张量[out_channels, in_channels // groups, kernel_height, kernel_width]) – 卷积权重,按组大小 (in_channels // groups) 分组。

  • 偏置 (张量[输出通道数]) – 可选偏置,形状为 (输出通道数,)。默认值:None

  • 步幅 (int元组[int, int]) – 卷积中心之间的距离。默认值:1

  • 填充 (intTuple[int, int]) – 每张图像周围的零填充的高度/宽度。默认值:0

  • 扩张率 (intTuple[int, int]) – 卷积核元素之间的间距。默认值:1

  • 掩码 (张量[batch_size, offset_groups * kernel_height * kernel_width, out_height, out_width]) – 在卷积核的每个位置应用的掩码。默认值:None

Returns

卷积结果

Return type

张量[批次大小, 输出通道, 输出高度, 输出宽度]

Examples::
>>> input = torch.rand(4, 3, 10, 10)
>>> kh, kw = 3, 3
>>> weight = torch.rand(5, 3, kh, kw)
>>> # offset and mask should have the same spatial size as the output
>>> # of the convolution. In this case, for an input of 10, stride of 1
>>> # and kernel size of 3, without padding, the output size is 8
>>> offset = torch.rand(4, 2 * kh * kw, 8, 8)
>>> mask = torch.rand(4, kh * kw, 8, 8)
>>> out = deform_conv2d(input, offset, weight, mask=mask)
>>> print(out.shape)
>>> # returns
>>>  torch.Size([4, 5, 8, 8])
torchvision.ops.generalized_box_iou(boxes1: torch.Tensor, boxes2: torch.Tensor)torch.Tensor[source]

返回两组框之间的广义交并比(雅卡尔指数)。

两组框都期望以(x1, y1, x2, y2)格式出现,带有 0 <= x1 < x20 <= y1 < y2

Parameters
  • boxes1 (张量[N, 4]) – 第一个框集合

  • boxes2 (Tensor[M, 4]) – 第二组边界框

Returns

包含 boxes1 和 boxes2 中每个元素的成对广义 IoU 值的 NxM 矩阵

Return type

张量[N, M]

torchvision.ops.masks_to_boxes(masks: torch.Tensor)torch.Tensor[source]

计算所提供掩码周围的边界框。

返回一个 [N, 4] 张量,包含边界框。这些边界框以 (x1, y1, x2, y2) 格式表示,并且有 0 <= x1 < x20 <= y1 < y2

Parameters

掩码 (张量[N, H, W]) – 需要转换的掩码,其中 N 是掩码的数量 而 (H, W) 是空间维度。

Returns

边界框

Return type

张量[N, 4]

使用 masks_to_boxes 的示例:

torchvision.ops.nms(boxes: torch.Tensor, scores: torch.Tensor, iou_threshold: float)torch.Tensor[source]

根据交并比(IoU)对框执行非极大值抑制(NMS)。

非极大值抑制(NMS)迭代地移除那些与另一个得分更高的框的 IoU 大于 iou_threshold 的低分框。

如果多个框的分数完全相同,并且相对于参考框满足 IoU 标准,则在 CPU 和 GPU 上选择的框不一定相同。这与 PyTorch 中存在重复值时 argsort 的行为类似。

Parameters
  • (张量[N, 4])) – 需要执行NMS的框。它们应以(x1, y1, x2, y2)格式提供,并包含0 <= x1 < x20 <= y1 < y2

  • 得分 (张量[N]) – 每个框的得分

  • iou_threshold (float) – 丢弃所有重叠框的IoU > iou_threshold

Returns

包含被NMS保留的元素索引的int64张量,按得分从高到低排序。

Return type

张量

torchvision.ops.ps_roi_align(input: torch.Tensor, boxes: torch.Tensor, output_size: int, spatial_scale: float = 1.0, sampling_ratio: int = - 1)torch.Tensor[source]

执行轻量级头部R-CNN中提到的位置敏感感兴趣区域(RoI)对齐操作。

Parameters
  • 输入 (张量[N, C, H, W]) – 输入张量,即包含N个元素的一批数据。每个元素包含C个尺寸为H x W的特征图。

  • 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。 坐标必须满足0 <= x1 < x20 <= y1 < y2。 如果传递单个张量,则第一列应包含批次中对应元素的索引,即[0, N - 1]中的数字。 如果传递张量列表,则每个张量将对应于批次中第i个元素的边界框。

  • output_size (int元组[int, int]) – 池化操作完成后输出的大小(以bins或像素为单位),表示为(height, width)。

  • spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0

  • sampling_ratio (int) – 用于计算每个池化输出bin的输出值的插值网格中的采样点数量。如果 > 0,则每个bin使用 sampling_ratio x sampling_ratio 个采样点。如果 <= 0,则使用自适应数量的网格点(计算为 ceil(roi_width / output_width),高度也是如此)。默认值:-1

Returns

池化的RoIs

Return type

Tensor[K, C / (output_size[0] * output_size[1]) , output_size[0] , output_size[1]]

torchvision.ops.ps_roi_pool(input: torch.Tensor, boxes: torch.Tensor, output_size: int, spatial_scale: float = 1.0)torch.Tensor[source]

执行位置敏感的兴趣区域(RoI)池化操作,如 R-FCN 中所述。

Parameters
  • 输入 (张量[N, C, H, W]) – 输入张量,即包含N个元素的一批数据。每个元素包含C个尺寸为H x W的特征图。

  • 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。 坐标必须满足0 <= x1 < x20 <= y1 < y2。 如果传递单个张量,则第一列应包含批次中对应元素的索引,即[0, N - 1]中的数字。 如果传递张量列表,则每个张量将对应于批次中第i个元素的边界框。

  • output_size (int元组[int, int]) – 池化操作完成后输出的大小(以bins或像素为单位),表示为(height, width)。

  • spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0

Returns

池化的RoIs。

Return type

Tensor[K, C / (output_size[0] * output_size[1]) , output_size[0] , output_size[1]]

torchvision.ops.remove_small_boxes(boxes: torch.Tensor, min_size: float)torch.Tensor[source]

移除至少有一边小于 min_size 的框。

Parameters
  • (张量[N, 4]) – 以 (x1, y1, x2, y2) 格式的框,带有 0 <= x1 < x20 <= y1 < y2

  • min_size (float) – 最小尺寸

Returns

具有两侧长度均大于 min_size 的框的索引

Return type

Tensor[K]

torchvision.ops.roi_align(input: torch.Tensor, boxes: Union[torch.Tensor, List[torch.Tensor]], output_size: None, spatial_scale: float = 1.0, sampling_ratio: int = - 1, aligned: bool = False)torch.Tensor[source]

执行Mask R-CNN中描述的带平均池化的感兴趣区域(RoI)对齐操作。

Parameters
  • 输入 (张量[N, C, H, W]) – 输入张量,即包含N个元素的一批数据。每个元素包含C个特征图,维度为H x W。 如果张量是量化过的,我们期望批量大小为N == 1

  • 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。 坐标必须满足0 <= x1 < x20 <= y1 < y2。 如果传递单个张量,则第一列应包含批次中对应元素的索引,即[0, N - 1]中的数字。 如果传递张量列表,则每个张量将对应于批次中第i个元素的边界框。

  • output_size (int元组[int, int]) – 池化操作完成后输出的大小(以bins或像素为单位),表示为(height, width)。

  • spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0

  • sampling_ratio (int) – 用于计算每个池化输出bin的输出值的插值网格中的采样点数量。如果 > 0,则每个bin使用 sampling_ratio x sampling_ratio 个采样点。如果 <= 0,则使用自适应数量的网格点(计算为 ceil(roi_width / output_width),高度也是如此)。默认值:-1

  • 对齐 (bool) – 如果为False,使用旧实现。 如果为True,通过将框坐标向左下平移-0.5以更好地与相邻的两个像素索引对齐。此版本在Detectron2中使用

Returns

池化的RoIs。

Return type

张量[K, C, output_size[0], output_size[1]]

torchvision.ops.roi_pool(input: torch.Tensor, boxes: Union[torch.Tensor, List[torch.Tensor]], output_size: None, spatial_scale: float = 1.0)torch.Tensor[source]

执行Fast R-CNN中描述的兴趣区域(RoI)池化操作

Parameters
  • 输入 (张量[N, C, H, W]) – 输入张量,即包含N个元素的一批数据。每个元素包含C个尺寸为H x W的特征图。

  • 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。 坐标必须满足0 <= x1 < x20 <= y1 < y2。 如果传递单个张量,则第一列应包含批次中对应元素的索引,即[0, N - 1]中的数字。 如果传递张量列表,则每个张量将对应于批次中第i个元素的边界框。

  • output_size (int元组[int, int]) – 剪裁操作完成后输出的大小,表示为 (高度, 宽度)

  • spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0

Returns

池化的RoIs。

Return type

张量[K, C, output_size[0], output_size[1]]

torchvision.ops.sigmoid_focal_loss(inputs: torch.Tensor, targets: torch.Tensor, alpha: float = 0.25, gamma: float = 2, reduction: str = 'none')[source]

原始实现来自 https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/focal_loss.py 。 RetinaNet 中用于密集检测的损失函数:https://arxiv.org/abs/1708.02002

Parameters
  • 输入 – 形状任意的浮点张量。 每个示例的预测值。

  • 目标 – 一个与输入张量形状相同的浮点张量。存储了输入中每个元素的二元分类标签(0表示负类,1表示正类)。

  • alpha – (可选)用于平衡正负样本的加权因子,范围在(0,1)之间,或-1表示忽略。默认值=0.25

  • gamma – 调制因子 (1 - p_t) 的指数,用于平衡简单与困难样本。

  • 缩减 – ‘none’ | ‘mean’ | ‘sum’ ‘none’: 不会对输出应用任何缩减。 ‘mean’: 输出将会被平均。 ‘sum’: 输出将会被求和。

Returns

应用了缩减选项的损失张量。

torchvision.ops.stochastic_depth(input: torch.Tensor, p: float, mode: str, training: bool = True)torch.Tensor[source]

实现了来自“深度网络中的随机深度”用于随机丢弃残差架构的残差分支的随机深度。

Parameters
  • 输入 (张量[N, ..]) – 输入张量,具有任意维度,第一个维度为批次大小即包含N行的批次。

  • p (float) – 输入被置零的概率。

  • 模式 (str) – "batch""row""batch" 随机将整个输入置零,"row" 随机选择批次中的行并置零。

  • 训练 – 如果是 True,应用随机深度。默认值:True

Returns

随机置零的张量。

Return type

张量[N, ..]

class torchvision.ops.RoIAlign(output_size: None, spatial_scale: float, sampling_ratio: int, aligned: bool = False)[source]

查看 roi_align()

class torchvision.ops.PSRoIAlign(output_size: int, spatial_scale: float, sampling_ratio: int)[source]

查看 ps_roi_align()

class torchvision.ops.RoIPool(output_size: None, spatial_scale: float)[source]

查看 roi_pool()

class torchvision.ops.PSRoIPool(output_size: int, spatial_scale: float)[source]

查看 ps_roi_pool()

class torchvision.ops.DeformConv2d(in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = True)[source]

查看 deform_conv2d()

class torchvision.ops.MultiScaleRoIAlign(featmap_names: List[str], output_size: Union[int, Tuple[int], List[int]], sampling_ratio: int, *, canonical_scale: int = 224, canonical_level: int = 4)[source]

多尺度RoIAlign池化,这对于使用或不使用FPN的检测都非常有用。

它通过等式1中指定的启发式方法推断池化的比例,该等式来自特征金字塔网络论文。 它们仅关键字参数canonical_scalecanonical_level分别对应于等式1中的224k0=4,并且具有以下含义:canonical_level是金字塔的目标层级,从中池化一个感兴趣区域与w x h = canonical_scale x canonical_scale

Parameters
  • 特征图名称 (List[str]) – 将用于池化的特征图的名称。

  • output_size (列表[元组[int, int]] 或 列表[int]) – 池化区域的输出大小

  • sampling_ratio (int) – ROIAlign的采样比率

  • 标准尺度 (int, 可选) – LevelMapper 的标准尺度

  • 规范级别 (int, 可选) – 规范级别的LevelMapper参数

Examples:

>>> m = torchvision.ops.MultiScaleRoIAlign(['feat1', 'feat3'], 3, 2)
>>> i = OrderedDict()
>>> i['feat1'] = torch.rand(1, 5, 64, 64)
>>> i['feat2'] = torch.rand(1, 5, 32, 32)  # this feature won't be used in the pooling
>>> i['feat3'] = torch.rand(1, 5, 16, 16)
>>> # create some random bounding boxes
>>> boxes = torch.rand(6, 4) * 256; boxes[:, 2:] += boxes[:, :2]
>>> # original image size, before computing the feature maps
>>> image_sizes = [(512, 512)]
>>> output = m(i, [boxes], image_sizes)
>>> print(output.shape)
>>> torch.Size([6, 5, 3, 3])
class torchvision.ops.FeaturePyramidNetwork(in_channels_list: List[int], out_channels: int, extra_blocks: Optional[torchvision.ops.feature_pyramid_network.ExtraFPNBlock] = None)[source]

从一组特征图上添加一个FPN模块。这是基于 “用于目标检测的特征金字塔网络”

特征图当前应该按深度递增的顺序排列。

模型的输入预计为一个包含特征图的有序字典[Tensor],在这些特征图之上将添加FPN。

Parameters
  • in_channels_list (list[int]) – 每个传递给模块的特征图的通道数

  • out_channels (int) – FPN 表示的通道数

  • extra_blocks (ExtraFPNBlockNone) – 如果提供,将执行额外的操作。它期望以fpn特征、原始特征和原始特征名称作为输入,并返回一个新的特征图列表及其对应的名称。

Examples:

>>> m = torchvision.ops.FeaturePyramidNetwork([10, 20, 30], 5)
>>> # get some dummy data
>>> x = OrderedDict()
>>> x['feat0'] = torch.rand(1, 10, 64, 64)
>>> x['feat2'] = torch.rand(1, 20, 16, 16)
>>> x['feat3'] = torch.rand(1, 30, 8, 8)
>>> # compute the FPN on top of x
>>> output = m(x)
>>> print([(k, v.shape) for k, v in output.items()])
>>> # returns
>>>   [('feat0', torch.Size([1, 5, 64, 64])),
>>>    ('feat2', torch.Size([1, 5, 16, 16])),
>>>    ('feat3', torch.Size([1, 5, 8, 8]))]
class torchvision.ops.StochasticDepth(p: float, mode: str)[source]

查看 stochastic_depth()

文档

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

查看文档

教程

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

查看教程

资源

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

查看资源