参考资料 ultralytics/utils/ops.py
备注
该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/utils/ops .py。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!
ultralytics.utils.ops.Profile
垒球 ContextDecorator
YOLOv8 Profile 类。使用 @Profile() 作为装饰器,或使用 "with Profile(): "作为上下文管理器。
示例
源代码 ultralytics/utils/ops.py
__enter__()
__exit__(type, value, traceback)
__init__(t=0.0, device=None)
初始化 Profile 类。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
t |
float
|
初始时间。默认为 0.0。 |
0.0
|
device |
device
|
用于模型推理的设备。默认为 "无"(cpu)。 |
None
|
源代码 ultralytics/utils/ops.py
__str__()
ultralytics.utils.ops.segment2box(segment, width=640, height=640)
将 1 个段落标签转换为 1 个方框标签,并应用内部图像约束,即 (xy1, xy2, ...)转换为 (xyxy)。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
segment |
Tensor
|
分段标签 |
所需 |
width |
int
|
图像的宽度。默认为 640 |
640
|
height |
int
|
图像的高度。默认为 640 |
640
|
返回:
类型 | 说明 |
---|---|
ndarray
|
段的最小和最大 x 值和 y 值。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False)
将边界框(默认格式为 xyxy)从最初指定的图像形状(img1_shape)调整为不同图像形状(img0_shape)。 形状(img1_shape)调整为不同图像的形状(img0_shape)。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
img1_shape |
tuple
|
边界框的图像形状,格式为(高、宽)。 |
所需 |
boxes |
Tensor
|
图像中物体的边界框,格式为 (x1, y1, x2, y2) |
所需 |
img0_shape |
tuple
|
目标图像的形状,格式为(高、宽)。 |
所需 |
ratio_pad |
tuple
|
的元组(ratio, pad),用于缩放方框。如果未提供,将根据两幅图像的尺寸差来计算比率和垫值。 将根据两幅图像的尺寸差来计算。 |
None
|
padding |
bool
|
如果为 True,则假定方框是基于由yolo 样式增强的图像。如果为 False,则进行常规 重新缩放。 |
True
|
xywh |
bool
|
方框格式是否为 xywh,默认为假。 |
False
|
返回:
名称 | 类型 | 说明 |
---|---|---|
boxes |
Tensor
|
按比例缩放的边界框,格式为 (x1, y1, x2, y2) |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.make_divisible(x, divisor)
返回能被给定除数整除的最接近的数字。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
int
|
能被整除的数字。 |
所需 |
divisor |
int | Tensor
|
除数。 |
所需 |
返回:
类型 | 说明 |
---|---|
int
|
能被除数整除的最近的数。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.nms_rotated(boxes, scores, threshold=0.45)
用于 obbs 的 NMS,由 probiou 和 fast-nms 提供支持。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
boxes |
Tensor
|
(N, 5), xywhr. |
所需 |
scores |
Tensor
|
(N, ). |
所需 |
threshold |
float
|
IoU 临界值。 |
0.45
|
返回:
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300, nc=0, max_time_img=0.05, max_nms=30000, max_wh=7680, in_place=True, rotated=False)
对一组盒子执行非最大抑制 (NMS),支持每个盒子的掩码和多个标签。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
prediction |
Tensor
|
形状为(batch_size、num_classes + 4 + num_masks、num_boxes)的tensor 包含预测的方框、类和掩码。tensor 的格式应为 模型输出的格式,如YOLO 。 |
所需 |
conf_thres |
float
|
置信度阈值,低于该值将被过滤掉。 有效值介于 0.0 和 1.0 之间。 |
0.25
|
iou_thres |
float
|
IoU 阈值,低于此阈值的方框将在 NMS 期间被过滤掉。 有效值介于 0.0 和 1.0 之间。 |
0.45
|
classes |
List[int]
|
要考虑的类别索引列表。如果为 "无",则将考虑所有类别。 |
None
|
agnostic |
bool
|
如果为 True,则模型与类的数量无关,所有类都将被视为一个。 类将被视为一个。 |
False
|
multi_label |
bool
|
如果为 True,则每个方框可以有多个标签。 |
False
|
labels |
List[List[Union[int, float, Tensor]]]
|
列表,其中每个内 列表包含给定图像的先验标签。列表格式应为 格式,每个标签为一个元组(class_index, x1, y1, x2, y2)。 |
()
|
max_det |
int
|
NMS 后保留的最大箱数。 |
300
|
nc |
int
|
模型输出的类别数。之后的任何指数都将被视为掩码。 |
0
|
max_time_img |
float
|
处理一幅图像的最长时间(秒)。 |
0.05
|
max_nms |
int
|
输入 torchvision.ops.nms() 的最大方框数。 |
30000
|
max_wh |
int
|
最大方框宽度和高度(以像素为单位)。 |
7680
|
in_place |
bool
|
如果为 True,输入预测结果tensor 将被就地修改。 |
True
|
返回:
类型 | 说明 |
---|---|
List[Tensor]
|
长度为 batch_size 的列表,其中每个元素都是tensor 的 形状(num_boxes, 6 + num_masks),其中包含保留的方框,列有 (x1, y1, x2, y2, confidence, class, mask1, mask2, ...)。 |
源代码 ultralytics/utils/ops.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
ultralytics.utils.ops.clip_boxes(boxes, shape)
获取边界框列表和形状(高度、宽度),并将边界框剪切到形状上。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
boxes |
Tensor
|
要剪切的边框 |
所需 |
shape |
tuple
|
图像的形状 |
所需 |
返回:
类型 | 说明 |
---|---|
Tensor | ndarray
|
剪切盒 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.clip_coords(coords, shape)
将线条坐标剪切到图像边界。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
coords |
Tensor | ndarray
|
线条坐标列表。 |
所需 |
shape |
tuple
|
一个整数元组,表示图像的大小,格式为(高、宽)。 |
所需 |
返回:
类型 | 说明 |
---|---|
Tensor | ndarray
|
剪切坐标 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.scale_image(masks, im0_shape, ratio_pad=None)
获取蒙版,并将其调整为原始图像大小。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
masks |
ndarray
|
调整大小和填充的蒙版/图像,[h, w, num]/[h, w, 3]。 |
所需 |
im0_shape |
tuple
|
原始图像形状 |
所需 |
ratio_pad |
tuple
|
填充与原始图像的比例。 |
None
|
返回:
名称 | 类型 | 说明 |
---|---|---|
masks |
Tensor
|
退回的面具。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xyxy2xywh(x)
将边界框坐标从 (x1, y1, x2, y2) 格式转换为 (x, y, width, height) 格式,其中 (x1, y1) 为左上角,(x2, y2) 为右下角。 为左上角,(x2, y2) 为右下角。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入的边界框坐标,格式为 (x1, y1, x2, y2)。 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
边界框坐标,格式为(x、y、宽、高)。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xywh2xyxy(x)
将边界框坐标从 (x, y, width, height) 格式转换为 (x1, y1, x2, y2) 格式,其中 (x1, y1) 为左上角,(x2, y2) 为右下角。 为左上角,(x2, y2) 为右下角。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入的边界框坐标,格式为(x、y、宽、高)。 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
边界框坐标,格式为 (x1, y1, x2, y2)。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xywhn2xyxy(x, w=640, h=640, padw=0, padh=0)
将归一化边界框坐标转换为像素坐标。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
边界框坐标。 |
所需 |
w |
int
|
图像宽度。默认为 640 |
640
|
h |
int
|
图像的高度。默认为 640 |
640
|
padw |
int
|
填充宽度。默认为 0 |
0
|
padh |
int
|
填充高度。默认为 0 |
0
|
返回值: y (np.ndarray |torch.Tensor):边界框的坐标,格式为 [x1, y1, x2, y2] 其中 x1,y1 是边界框的左上角,x2,y2 是边界框的右下角。
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0)
将边界框坐标从 (x1, y1, x2, y2) 格式转换为 (x, y, width, height, normalized) 格式、 宽度和高度归一化为图像尺寸。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入的边界框坐标,格式为 (x1, y1, x2, y2)。 |
所需 |
w |
int
|
图像的宽度。默认为 640 |
640
|
h |
int
|
图像的高度。默认为 640 |
640
|
clip |
bool
|
如果为 True,方框将剪切到图像边界。默认为假 |
False
|
eps |
float
|
方框宽度和高度的最小值。默认为 0.0 |
0.0
|
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
以(x、y、宽、高、归一化)格式表示的边界框坐标 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xywh2ltwh(x)
将边界框格式从 [x, y, w, h] 转换为 [x1, y1, w, h],其中 x1, y1 为左上角坐标。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入tensor 和 xywh 格式的边界框坐标 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
xyltwh 格式的边界框坐标 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xyxy2ltwh(x)
将 nx4 边界框从 [x1, y1, x2, y2] 转换为 [x1, y1, w, h],其中 xy1= 左上角,xy2= 右下角。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入tensor ,其中包含 xyxy 格式的边界框坐标 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
xyltwh 格式的边界框坐标。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.ltwh2xywh(x)
将 nx4 个方框从 [x1, y1, w, h] 转换为 [x, y, w, h],其中 xy1=左上角,xy=中间。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
Tensor
|
输入tensor |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
xywh 格式的边界框坐标。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xyxyxyxy2xywhr(corners)
将[xy1, xy2, xy3, xy4]分批定向边框 (OBB) 转换为[xywh, rotation]。旋转值 以 0 至 90 度为单位。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
corners |
ndarray | Tensor
|
输入形状为 (n, 8) 的角。 |
所需 |
返回:
类型 | 说明 |
---|---|
ndarray | Tensor
|
以 [cx、cy、w、h、旋转] 格式转换形状为 (n,5) 的数据。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.xywhr2xyxyxyxy(rboxes)
将 [xywh, rotation] 的批次定向边框 (OBB) 转换为 [xy1, xy2, xy3, xy4]。旋转值应 以 0 至 90 度为单位。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
rboxes |
ndarray | Tensor
|
形状为 (n, 5) 或 (b, n, 5) 的 [cx, cy, w, h, rotation] 格式方框。 |
所需 |
返回:
类型 | 说明 |
---|---|
ndarray | Tensor
|
转换形状为 (n, 4, 2) 或 (b, n, 4, 2) 的角点。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.ltwh2xyxy(x)
它将边界框从 [x1, y1, w, h] 转换为 [x1, y1, x2, y2],其中 xy1= 左上角,xy2= 右下角。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
ndarray | Tensor
|
输入图像 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
y |
ndarray | Tensor
|
边界框的 xyxy 坐标。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.segments2boxes(segments)
它将段落标签转换为方框标签,即 (cls, xy1, xy2, ...)转换为 (cls, xywh)
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
segments |
list
|
段列表,每个段是点列表,每个点是 x、y 坐标列表 |
所需 |
返回:
类型 | 说明 |
---|---|
ndarray
|
边界框的 xywh 坐标。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.resample_segments(segments, n=1000)
输入一个线段列表(n,2),并返回一个向上采样到 n 个点的线段列表(n,2)。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
segments |
list
|
(n,2) 数组的列表,其中 n 是线段中的点数。 |
所需 |
n |
int
|
对片段重新采样的点数。默认为 1000 |
1000
|
返回:
名称 | 类型 | 说明 |
---|---|---|
segments |
list
|
重新取样的片段。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.crop_mask(masks, boxes)
它接收一个遮罩和一个边界框,然后返回一个按边界框裁剪的遮罩。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
masks |
Tensor
|
[n, h, w]tensor 掩码 |
所需 |
boxes |
Tensor
|
[n, 4]tensor 相对点形式的 bbox 坐标 |
所需 |
返回:
类型 | 说明 |
---|---|
Tensor
|
遮罩被裁剪为边界框。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.process_mask_upsample(protos, masks_in, bboxes, shape)
获取遮罩头的输出,并将遮罩应用于边界框。这样生成的蒙版质量更高 但速度较慢。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
protos |
Tensor
|
[mask_dim, mask_h, mask_w] (屏蔽尺寸、屏蔽高度、屏蔽宽度 |
所需 |
masks_in |
Tensor
|
[n,mask_dim],n 为 nms 后的屏蔽数 |
所需 |
bboxes |
Tensor
|
[n,4],n 是 nms 后的屏蔽数 |
所需 |
shape |
tuple
|
输入图像的大小 (h,w) |
所需 |
返回:
类型 | 说明 |
---|---|
Tensor
|
上采样掩码 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.process_mask(protos, masks_in, bboxes, shape, upsample=False)
使用遮罩头的输出将遮罩应用到边界框。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
protos |
Tensor
|
形状为 [mask_dim, mask_h, mask_w] 的tensor 。 |
所需 |
masks_in |
Tensor
|
形状为 [n, mask_dim]的tensor ,其中 n 是 NMS 后的掩码数。 |
所需 |
bboxes |
Tensor
|
形状为 [n, 4] 的tensor ,其中 n 是 NMS 后的掩码数。 |
所需 |
shape |
tuple
|
表示输入图像大小的整数元组,格式为 (h,w)。 |
所需 |
upsample |
bool
|
指示是否将掩码上采样到原始图像大小的标志。默认为假。 |
False
|
返回:
类型 | 说明 |
---|---|
Tensor
|
二进制掩码tensor 的形状为 [n,h,w],其中 n 是 NMS 后的掩码数,h 和 w 是输入图像的高度和宽度。 是输入图像的高度和宽度。掩码应用于边界框。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.process_mask_native(protos, masks_in, bboxes, shape)
它采用遮罩头的输出,在上采样到边界框后对其进行裁剪。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
protos |
Tensor
|
[mask_dim, mask_h, mask_w] (屏蔽尺寸、屏蔽高度、屏蔽宽度 |
所需 |
masks_in |
Tensor
|
[n,mask_dim],n 为 nms 后的屏蔽数 |
所需 |
bboxes |
Tensor
|
[n,4],n 是 nms 后的屏蔽数 |
所需 |
shape |
tuple
|
输入图像的大小 (h,w) |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
masks |
Tensor
|
返回的掩码尺寸为 [h、w、n] |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.scale_masks(masks, shape, padding=True)
根据形状调整段屏蔽的比例。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
masks |
Tensor
|
(N、C、H、W)。 |
所需 |
shape |
tuple
|
高度和宽度 |
所需 |
padding |
bool
|
如果为 True,则假定方框是基于由yolo 样式增强的图像。如果为 False,则进行常规 重新缩放。 |
True
|
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.scale_coords(img1_shape, coords, img0_shape, ratio_pad=None, normalize=False, padding=True)
将 img1_shape 的线段坐标(xy)调整为 img0_shape。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
img1_shape |
tuple
|
坐标来源于图像的形状。 |
所需 |
coords |
Tensor
|
形状 n,2 要缩放的坐标。 |
所需 |
img0_shape |
tuple
|
进行分割的图像的形状。 |
所需 |
ratio_pad |
tuple
|
图像大小与填充图像大小的比率。 |
None
|
normalize |
bool
|
如果为 True,坐标将归一化为 [0, 1] 范围。默认为假。 |
False
|
padding |
bool
|
如果为 True,则假定方框是基于由yolo 样式增强的图像。如果为 False,则进行常规 重新缩放。 |
True
|
返回:
名称 | 类型 | 说明 |
---|---|---|
coords |
Tensor
|
缩放坐标。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.regularize_rboxes(rboxes)
在 [0, pi/2] 范围内规范化旋转方框。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
rboxes |
Tensor
|
(N, 5), xywhr. |
所需 |
返回:
类型 | 说明 |
---|---|
Tensor
|
规范化方框。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.masks2segments(masks, strategy='largest')
它接收掩码(n,h,w)列表,并返回段(n,xy)列表
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
masks |
Tensor
|
模型的输出,即tensor 的形状 (batch_size, 160, 160) |
所需 |
strategy |
str
|
连接 "或 "最大"。默认为最大 |
'largest'
|
返回:
名称 | 类型 | 说明 |
---|---|---|
segments |
List
|
段掩码列表 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.convert_torch2numpy_batch(batch)
将一批 FP32torch 张量(0.0-1.0)转换为 NumPy uint8 数组(0-255),从 BCHW 布局转换为 BHWC 布局。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
batch |
Tensor
|
输入tensor 形状批次(批次、通道、高度、宽度)和 dtypetorch.float32。 |
所需 |
返回:
类型 | 说明 |
---|---|
ndarray
|
输出 NumPy 数组批次的形状(批次、高度、宽度、通道)和 dtype uint8。 |
源代码 ultralytics/utils/ops.py
ultralytics.utils.ops.clean_str(s)
用下划线 _ 替换特殊字符,清理字符串
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
s |
str
|
需要替换特殊字符的字符串 |
所需 |
返回:
类型 | 说明 |
---|---|
str
|
用下划线 _ 代替特殊字符的字符串 |