torchvision.ops 中¶
torchvision.ops
实现特定于计算机视觉的运算符。
注意
所有 Operator 都对 TorchScript 提供原生支持。
-
torchvision.ops.
nms
(盒子:Torch。Tensor,分数:torch。Tensor,iou_threshold:float) → torch。张量[来源]¶ 根据对盒子执行非极大值抑制 (NMS) 到它们的交集对联合 (IoU)。
NMS 迭代删除具有 IoU 大于 iou_threshold 且另一个 IoU 大于 IoU (更高的分数) 箱。
如果多个框具有完全相同的分数并满足 IoU criterion 相对于引用框,则所选框为 不能保证 CPU 和 GPU 之间相同。这与此类似 到 PyTorch 中存在重复值时 argsort 的行为。
参数: - boxes (Tensor[N, 4])) – 要对其执行 NMS 的框。他们 应采用 (x1, y1, x2, y2) 格式
- scores (Tensor[N]) – 每个框的分数
- iou_threshold (float) – 丢弃所有重叠的 带有 IoU > iou_threshold的盒子
返回: keep – 带有索引的 int64 张量 已保留的元素 按 NMS,按分数降序排序
返回类型: 张肌
-
torchvision.ops.
batched_nms
(盒子:Torch。Tensor,分数:torch。Tensor、idxs:torch 的 Tensor 和 IDXS 的 Tensor 中。Tensor,iou_threshold:float)→ torch。张量[来源]¶ 以批处理方式执行非极大值抑制。
每个索引值对应一个类别,NMS 不会在不同类别的元素之间应用。
参数: - boxes (Tensor[N, 4]) – 将执行 NMS 的框。他们 应采用 (x1, y1, x2, y2) 格式
- scores (Tensor[N]) – 每个框的分数
- idxs (Tensor[N]) – 每个框的类别索引。
- iou_threshold (float) – 丢弃所有重叠的框 使用 IoU > iou_threshold
返回: keep - int64 张量,索引为 NMS 保存的元素,已排序 按分数降序排列
返回类型: 张肌
-
torchvision.ops.
remove_small_boxes
(盒子:Torch。Tensor,min_size:float)→ torch。张量[来源]¶ 移除至少包含一侧小于min_size的箱子。
参数: - boxes (Tensor[N, 4]) – (x1, y1, x2, y2) 格式的框
- min_size (float) – 最小大小
返回: - 具有两侧的框的索引
大于 min_size
返回类型: keep (张量[K])
-
torchvision.ops.
clip_boxes_to_image
(盒子:Torch。Tensor,大小:Tuple[int, int]) → torch。张量[来源]¶ 剪辑框,以便它们位于 size 大小的图像内。
参数: - boxes (Tensor[N, 4]) – (x1, y1, x2, y2) 格式的框
- size (Tuple[height, width]) – 图像的大小
返回: clipped_boxes (张量 [N, 4])
-
torchvision.ops.
box_convert
(盒子:Torch。Tensor、in_fmt:str、out_fmt:str) → torch。张量[来源]¶ 将框从给定in_fmt转换为out_fmt。 支持的 in_fmt 和 out_fmt 包括:
'xyxy':方框由角表示,x1、y1 是左上角,x2、y2 是右下角。
'xywh' :框通过角、宽度和高度表示,x1,y2 是左上角,w、h 是宽度和高度。
'cxcywh' :框通过中心、宽度和高度表示,cx,cy 是框的中心,w,h 是 width 和 height。
参数: 返回: 框转换为转换格式。
返回类型: 框 (Tensor[N, 4])
-
torchvision.ops.
box_area
(盒子:Torch。Tensor) → torch 的 Tensor 中。张量[来源]¶ 计算一组边界框的面积,这些边界框由其 (x1, y1, x2, y2) 坐标。
参数: boxes (Tensor[N, 4]) – 将计算面积的框。他们 应采用 (x1, y1, x2, y2) 格式 返回: 每个框的面积 返回类型: 面积 (Tensor[N])
-
torchvision.ops.
box_iou
(方框 1:火把。Tensor,box2:torch 的 Tensor 和 Tensor 的 Tensor 中。Tensor) → torch 的 Tensor 中。张量[来源]¶ 返回框的交集与联合 (Jaccard index)。
两组框都应采用 (x1, y1, x2, y2) 格式。
参数: - 框1 (张量[N, 4]) –
- 框2 (张量[M, 4]) –
返回: 包含 box1 和 boxes2 中每个元素的成对 IoU 值的 NxM 矩阵
返回类型: iou (张量[N, M])
-
torchvision.ops.
generalized_box_iou
(方框 1:火把。Tensor,box2:torch 的 Tensor 和 Tensor 的 Tensor 中。Tensor) → torch 的 Tensor 中。张量[来源]¶ 返回框的广义交集与联合 (Jaccard index)。
两组框都应采用 (x1, y1, x2, y2) 格式。
参数: - 框1 (张量[N, 4]) –
- 框2 (张量[M, 4]) –
返回: 包含成对 generalized_IoU 值的 NxM 矩阵 对于 box1 和 box2 中的每个元素
返回类型: generalized_iou (张量 [N, M])
-
torchvision.ops.
roi_align
(输入:Torch。张量,框:torch。张量,output_size:无,spatial_scale:float = 1.0,sampling_ratio:int = -1,对齐:bool = False)→ torch。张量[来源]¶ 执行掩码 R-CNN 中描述的感兴趣区域 (RoI) 对齐运算符
参数: - input (Tensor[N, C, H, W]) – 输入张量
- boxes (Tensor[K, 5] 或 List[Tensor[L, 4]]) – (x1, y1, x2, y2) 中的框坐标 格式。如果传递了单个 Tensor,则 然后,第一列应包含 Batch 索引。如果 Tensor 列表 传递,则每个 Tensor 将对应于元素 i 的框 在批次中
- output_size (int or Tuple[int, int]) – 裁剪后输出的大小 执行为 (height, width)
- spatial_scale (float) – 将输入坐标映射到的比例因子 盒子坐标。默认值:1.0
- sampling_ratio (int) – 插值网格中的采样点数量 用于计算每个共用输出箱的输出值。如果> 0,则 然后,正好使用 sampling_ratio x sampling_ratio 个网格点。如果 <= 0,则使用自适应数量的网格点(计算公式为 ceil(roi_width / pooled_w),高度也是如此)。默认值:-1
- aligned (bool) – 如果为 False,则使用传统实现。 如果为 True,则将其像素移动 -0.5,以便更完美地对齐两个相邻像素索引。 Detectron2 中的此版本
返回: 输出 (Tensor[K, C, output_size[0], output_size[1]])
-
torchvision.ops.
ps_roi_align
(输入:Torch。张量,框:torch。张量、output_size:int、spatial_scale:float = 1.0、sampling_ratio:int = -1)→ torch。张量[来源]¶ 执行位置敏感感兴趣区域 (RoI) 对齐运算符 在 Light-Head R-CNN 中被提及。
参数: - input (Tensor[N, C, H, W]) – 输入张量
- boxes (Tensor[K, 5] 或 List[Tensor[L, 4]]) – (x1, y1, x2, y2) 中的框坐标 格式。如果传递了单个 Tensor,则 然后,第一列应包含 Batch 索引。如果 Tensor 列表 传递,则每个 Tensor 将对应于元素 i 的框 在批次中
- output_size (int or Tuple[int, int]) – 裁剪后输出的大小 执行为 (height, width)
- spatial_scale (float) – 将输入坐标映射到的比例因子 盒子坐标。默认值:1.0
- sampling_ratio (int) – 插值网格中的采样点数量 用于计算每个共用输出箱的输出值。如果 > 0 然后,正好使用 sampling_ratio x sampling_ratio 个网格点。 如果 <= 0,则使用自适应数量的网格点(计算公式为 ceil(roi_width / pooled_w),高度也是如此)。默认值:-1
返回: 输出 (Tensor[K, C, output_size[0], output_size[1]])
-
torchvision.ops.
roi_pool
(输入:Torch。张量,框:torch。Tensor,output_size:无,spatial_scale:float = 1.0)→ torch。张量[来源]¶ 执行快速 R-CNN 中描述的感兴趣区域 (RoI) 池运算符
参数: - input (Tensor[N, C, H, W]) – 输入张量
- boxes (Tensor[K, 5] 或 List[Tensor[L, 4]]) – (x1, y1, x2, y2) 中的框坐标 格式。如果传递了单个 Tensor,则 然后,第一列应包含 Batch 索引。如果 Tensor 列表 传递,则每个 Tensor 将对应于元素 i 的框 在批次中
- output_size (int or Tuple[int, int]) – 裁剪后输出的大小 执行为 (height, width)
- spatial_scale (float) – 将输入坐标映射到的比例因子 盒子坐标。默认值:1.0
返回: 输出 (Tensor[K, C, output_size[0], output_size[1]])
-
torchvision.ops.
ps_roi_pool
(输入:Torch。张量,框:torch。Tensor, output_size: int, spatial_scale: float = 1.0) → torch。张量[来源]¶ 执行位置敏感感兴趣区域 (RoI) 池运算符 在 R-FCN 中描述
参数: - input (Tensor[N, C, H, W]) – 输入张量
- boxes (Tensor[K, 5] 或 List[Tensor[L, 4]]) – (x1, y1, x2, y2) 中的框坐标 格式。如果传递了单个 Tensor,则 然后,第一列应包含 Batch 索引。如果 Tensor 列表 传递,则每个 Tensor 将对应于元素 i 的框 在批次中
- output_size (int or Tuple[int, int]) – 裁剪后输出的大小 执行为 (height, width)
- spatial_scale (float) – 将输入坐标映射到的比例因子 盒子坐标。默认值:1.0
返回: 输出 (Tensor[K, C, output_size[0], output_size[1]])
-
torchvision.ops.
deform_conv2d
(输入:Torch。Tensor,偏移量:torch。张量,权重:torch。张量,偏差:Union[torch.张量,NoneType] = 无,步幅:Tuple[int, int] = (1, 1),填充:Tuple[int, int] = (0, 0),膨胀:Tuple[int, int] = (1, 1)) → torch。张量[来源]¶ 执行可变形卷积,如可变形卷积网络中所述
参数: - input (Tensor[batch_size, in_channels, in_height, in_width]) – 输入张量
- (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width, (offset) – out_height, out_width]):要应用于 卷积核。
- weight (Tensor[out_channels, in_channels // 组, kernel_height, kernel_width]) – 卷积权重,分为大小组 (in_channels // 组)
- bias (Tensor[out_channels]) - 形状 (out_channels,) 的可选偏差。默认值:无
- stride (int or Tuple[int, int]) - 卷积中心之间的距离。默认值:1
- padding (int or Tuple[int, int]) – 0 周围填充的高度/宽度 每张图片。默认值:0
- dilation (int or Tuple[int, int]) - 内核元素之间的间距。默认值:1
返回: 卷积的结果
返回类型: 输出 (Tensor[batch_sz, out_channels, out_h, out_w])
- 例子::
>>> 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 (output_size: None, spatial_scale: float, sampling_ratio: int, aligned: bool = False)[来源]
torchvision.ops.
RoIAlign
¶ 查看 roi_align
-
类(output_size:int,spatial_scale:float,sampling_ratio:int)[来源]
torchvision.ops.
PSRoIAlign
¶ 查看 ps_roi_align
-
类(in_channels:int,out_channels:int,kernel_size:int,步幅:int = 1,填充:int = 0,膨胀:int = 1,组:int = 1,偏差:bool = True)[来源]
torchvision.ops.
DeformConv2d
¶ 查看 deform_conv2d
-
类(featmap_names:List[str],output_size:Union[int, Tuple[int], List[int]], sampling_ratio: int)[源代码]
torchvision.ops.
MultiScaleRoIAlign
¶ 多尺度 RoIAlign 池化,对于有或没有 FPN 的检测都很有用。
它通过 FPN 论文中的启发式方法推断池化的规模。
参数: 例子:
>>> 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])
-
类 (in_channels_list: List[int], out_channels: int, extra_blocks: Union[torchvision.ops.feature_pyramid_network.ExtraFPNBlock,NoneType] = None)[来源]
torchvision.ops.
FeaturePyramidNetwork
¶ 模块,该模块从一组特征图的顶部添加 FPN。这是基于“用于对象检测的特征金字塔网络”。
特征图目前应该越来越深入 次序。
模型的输入应为 OrderedDict[Tensor],其中包含 特征图,FPN 将在其上添加。
参数: 例子:
>>> 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]))]