torchvision.ops¶
torchvision.ops 实现了特定于计算机视觉的操作。
注意
所有运算符都原生支持TorchScript。
-
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])) – 需要执行非极大值抑制的边界框。它们应为(x1, y1, x2, y2)格式。
- 得分 (张量[N]) – 每个框的得分
- iou_threshold (float) – 丢弃所有重叠框的IoU > iou_threshold
Returns: keep – int64 张量,包含经过 NMS 保留的元素的索引, 按得分从高到低排序
返回类型: 张量
-
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)格式
- 得分 (张量[N]) – 每个框的得分
- idxs (Tensor[N]) – 每个框所属类别的索引。
- iou_threshold (float) – 丢弃所有重叠框的IoU > iou_threshold
Returns: keep – int64 张量,包含经过 NMS 保留的元素的索引,并按分数从高到低排序
返回类型: 张量
-
torchvision.ops.remove_small_boxes(boxes: torch.Tensor, min_size: float) → torch.Tensor[source]¶ 移除至少有一边小于 min_size 的框。
Parameters: - 框 (张量[N, 4]) – 以 (x1, y1, x2, y2) 格式的框
- min_size (float) – 最小尺寸
Returns: - indices of the boxes that have both sides
大于 min_size
返回类型: 保持 (Tensor[K])
-
torchvision.ops.clip_boxes_to_image(boxes: torch.Tensor, size: Tuple[int, int]) → torch.Tensor[source]¶ 将框裁剪到大小为size的图像内。
Parameters: - 框 (张量[N, 4]) – 以 (x1, y1, x2, y2) 格式的框
- 大小 (元组[高度, 宽度]) – 图像的大小
Returns: 剪裁后的边界框 (Tensor[N, 4])
-
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 是右下角。
‘xywh’:框通过左上角的坐标(x1, y2)、宽度(w)和高度(h)来表示。
‘cxcywh’:边界框通过中心、宽度和高度来表示,其中 cx 和 cy 是框的中心,w 和 h 是宽度和高度。
Parameters: Returns: 转换为指定格式的框。
返回类型: boxes (张量 [N, 4])
-
torchvision.ops.box_area(boxes: torch.Tensor) → torch.Tensor[source]¶ 计算一组边界框的面积,这些边界框由其 (x1, y1, x2, y2) 坐标指定。
Parameters: 框 (张量[N, 4]) – 需要计算面积的框。它们应该以(x1, y1, x2, y2)格式表示 Returns: 每个框的区域 返回类型: 区域 (张量[N])
-
torchvision.ops.box_iou(boxes1: torch.Tensor, boxes2: torch.Tensor) → torch.Tensor[source]¶ 返回框的交并比(雅卡尔指数)。
两组框都应采用 (x1, y1, x2, y2) 格式。
Parameters: - boxes1 (张量[N, 4]) –
- boxes2 (张量[M, 4]) –
Returns: 包含 boxes1 和 boxes2 中每个元素的成对 IoU 值的 NxM 矩阵
返回类型: iou (张量[N, M])
-
torchvision.ops.generalized_box_iou(boxes1: torch.Tensor, boxes2: torch.Tensor) → torch.Tensor[source]¶ 返回框的广义交并比(雅卡尔指数)。
两组框都应采用 (x1, y1, x2, y2) 格式。
Parameters: - boxes1 (张量[N, 4]) –
- boxes2 (张量[M, 4]) –
Returns: 包含 boxes1 和 boxes2 中每个元素的成对广义_IOU 值的 NxM 矩阵
返回类型: 广义交并比(Tensor[N, M])
-
torchvision.ops.roi_align(input: torch.Tensor, boxes: 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]) – 输入张量
- 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。如果传递单个张量,则第一列应包含批次索引。如果传递张量列表,则每个张量对应批次中第 i 个元素的边界框。
- output_size (int 或 元组[int, int]) – 剪裁操作完成后输出的大小,表示为 (高度, 宽度)
- spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0
- sampling_ratio (int) – 在插值网格中用于计算每个池化输出bin的输出值的采样点数量。如果 > 0,则使用确切的 sampling_ratio x sampling_ratio 网格点。如果 <= 0,则使用自适应数量的网格点(计算为 ceil(roi_width / pooled_w),高度同理)。默认值:-1
- 对齐 (bool) – 如果为False,使用旧实现。 如果为True,通过将像素向左移动0.5来更完美地对齐两个相邻像素索引。 此版本在Detectron2中
Returns: 输出 (张量[K, C, output_size[0], output_size[1]])
-
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]) – 输入张量
- 边界框 (张量[K, 5] 或 列表[张量[L, 4]]) – (x1, y1, x2, y2) 格式的边界框坐标,从中提取区域。如果传递单个张量,则第一列应包含批次索引。如果传递张量列表,则每个张量对应批次中第 i 个元素的边界框。
- output_size (int 或 元组[int, int]) – 剪裁操作完成后输出的大小,表示为 (高度, 宽度)
- spatial_scale (float) – 一个缩放因子,将输入坐标映射到框坐标。默认值:1.0
- sampling_ratio (int) – 在插值网格中用于计算每个池化输出箱的输出值的采样点数量。如果 > 0,则使用确切的 sampling_ratio x sampling_ratio 网格点。如果 <= 0,则使用自适应数量的网格点(计算为 ceil(roi_width / pooled_w),高度同理)。默认值:-1
Returns: 输出 (张量[K, C, output_size[0], output_size[1]])
-
torchvision.ops.roi_pool(input: torch.Tensor, boxes: torch.Tensor, output_size: None, spatial_scale: float = 1.0) → torch.Tensor[source]¶ 执行Fast R-CNN中描述的兴趣区域(RoI)池化操作
Parameters: Returns: 输出 (张量[K, C, 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: Returns: 输出 (张量[K, C, output_size[0], output_size[1]])
-
torchvision.ops.deform_conv2d(input: torch.Tensor, offset: torch.Tensor, weight: torch.Tensor, bias: Union[torch.Tensor, NoneType] = None, stride: Tuple[int, int] = (1, 1), padding: Tuple[int, int] = (0, 0), dilation: Tuple[int, int] = (1, 1)) → torch.Tensor[source]¶ 执行可变形卷积,详见《可变形卷积网络》论文。
Parameters: - 输入 (张量[batch_size, in_channels, in_height, in_width]) – 输入张量
- (张量[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
- 填充 (int 或 Tuple[int, int]) – 每张图像周围的零填充的高度/宽度。默认值:0
- 扩张率 (int 或 Tuple[int, int]) – 卷积核元素之间的间距。默认值:1
Returns: 卷积结果
返回类型: 输出 (张量[批次大小, 输出通道数, 输出高度, 输出宽度])
- Examples::
>>> input = torch.rand(4, 3, 10, 10) >>> kh, kw = 3, 3 >>> weight = torch.rand(5, 3, kh, kw) >>> # offset 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) >>> out = deform_conv2d(input, offset, weight) >>> print(out.shape) >>> # returns >>> torch.Size([4, 5, 8, 8])
-
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.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)[source]¶ 多尺度RoIAlign池化,这对于使用或不使用FPN的检测都非常有用。
它通过FPN论文中提到的启发式方法推断池化规模。
Parameters: 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: Union[torchvision.ops.feature_pyramid_network.ExtraFPNBlock, NoneType] = None)[source]¶ 从一组特征图上添加一个FPN模块。这是基于 “用于目标检测的特征金字塔网络”。
特征图当前应该按深度递增的顺序排列。
模型的输入预计为一个包含特征图的有序字典[Tensor],在这些特征图之上将添加FPN。
Parameters: 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]))]