ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ содСрТимому

Бсылка для ultralytics/utils/ops.py

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅

Π­Ρ‚ΠΎΡ‚ Ρ„Π°ΠΉΠ» доступСн ΠΏΠΎ адрСсу https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/utils/ops .py. Если Ρ‚Ρ‹ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠΈΠ» ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡƒ, поТалуйста, ΠΏΠΎΠΌΠΎΠ³ΠΈ ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ Π΅Π΅, создав Pull Request πŸ› οΈ. Бпасибо πŸ™!



ultralytics.utils.ops.Profile

Π‘Π°Π·Ρ‹: ContextDecorator

YOLOv8 Класс профиля. Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉ Π΅Π³ΠΎ ΠΊΠ°ΠΊ Π΄Π΅ΠΊΠΎΡ€Π°Ρ‚ΠΎΡ€ с @Profile() ΠΈΠ»ΠΈ ΠΊΠ°ΠΊ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ€ контСкста с 'with Profile():'.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€
from ultralytics.utils.ops import Profile

with Profile(device=device) as dt:
    pass  # slow operation here

print(dt)  # prints "Elapsed time is 9.5367431640625e-07 s"
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
class Profile(contextlib.ContextDecorator):
    """
    YOLOv8 Profile class. Use as a decorator with @Profile() or as a context manager with 'with Profile():'.

    Example:
        ```python
        from ultralytics.utils.ops import Profile

        with Profile(device=device) as dt:
            pass  # slow operation here

        print(dt)  # prints "Elapsed time is 9.5367431640625e-07 s"
        ```
    """

    def __init__(self, t=0.0, device: torch.device = None):
        """
        Initialize the Profile class.

        Args:
            t (float): Initial time. Defaults to 0.0.
            device (torch.device): Devices used for model inference. Defaults to None (cpu).
        """
        self.t = t
        self.device = device
        self.cuda = bool(device and str(device).startswith("cuda"))

    def __enter__(self):
        """Start timing."""
        self.start = self.time()
        return self

    def __exit__(self, type, value, traceback):  # noqa
        """Stop timing."""
        self.dt = self.time() - self.start  # delta-time
        self.t += self.dt  # accumulate dt

    def __str__(self):
        """Returns a human-readable string representing the accumulated elapsed time in the profiler."""
        return f"Elapsed time is {self.t} s"

    def time(self):
        """Get current time."""
        if self.cuda:
            torch.cuda.synchronize(self.device)
        return time.time()

__enter__()

ВрСмя Π½Π°Ρ‡Π°Π»Π°.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def __enter__(self):
    """Start timing."""
    self.start = self.time()
    return self

__exit__(type, value, traceback)

ΠžΡΡ‚Π°Π½ΠΎΠ²ΠΈ врСмя.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def __exit__(self, type, value, traceback):  # noqa
    """Stop timing."""
    self.dt = self.time() - self.start  # delta-time
    self.t += self.dt  # accumulate dt

__init__(t=0.0, device=None)

Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠΉ класс Profile.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
t float

ΠΠ°Ρ‡Π°Π»ΡŒΠ½ΠΎΠ΅ врСмя. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 0,0.

0.0
device device

Устройства, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡ‹Π΅ для Π²Ρ‹Π²ΠΎΠ΄Π° ΠΌΠΎΠ΄Π΅Π»ΠΈ. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - None (cpu).

None
Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def __init__(self, t=0.0, device: torch.device = None):
    """
    Initialize the Profile class.

    Args:
        t (float): Initial time. Defaults to 0.0.
        device (torch.device): Devices used for model inference. Defaults to None (cpu).
    """
    self.t = t
    self.device = device
    self.cuda = bool(device and str(device).startswith("cuda"))

__str__()

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠΎΡ‡ΠΈΡ‚Π°Π΅ΠΌΡƒΡŽ строку, ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΡŽΡ‰ΡƒΡŽ Π½Π°ΠΊΠΎΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΈΡΡ‚Π΅ΠΊΡˆΠ΅Π΅ врСмя Π² ΠΏΡ€ΠΎΡ„Π°ΠΉΠ»Π΅Ρ€Π΅.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def __str__(self):
    """Returns a human-readable string representing the accumulated elapsed time in the profiler."""
    return f"Elapsed time is {self.t} s"

time()

ΠŸΠΎΠ»ΡƒΡ‡ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ врСмя.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def time(self):
    """Get current time."""
    if self.cuda:
        torch.cuda.synchronize(self.device)
    return time.time()



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
def segment2box(segment, width=640, height=640):
    """
    Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy).

    Args:
        segment (torch.Tensor): the segment label
        width (int): the width of the image. Defaults to 640
        height (int): The height of the image. Defaults to 640

    Returns:
        (np.ndarray): the minimum and maximum x and y values of the segment.
    """
    x, y = segment.T  # segment xy
    inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height)
    x = x[inside]
    y = y[inside]
    return (
        np.array([x.min(), y.min(), x.max(), y.max()], dtype=segment.dtype)
        if any(x)
        else np.zeros(4, dtype=segment.dtype)
    )  # xyxy



ultralytics.utils.ops.scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False)

ИзмСни ΠΌΠ°ΡΡˆΡ‚Π°Π± ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΠ²Π°ΡŽΡ‰ΠΈΡ… Ρ€Π°ΠΌΠΎΠΊ (ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ xyxy) с Ρ„ΠΎΡ€ΠΌΡ‹ изобраТСния, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΎΠ½ΠΈ Π±Ρ‹Π»ΠΈ ΠΈΠ·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ ΡƒΠΊΠ°Π·Π°Π½Ρ‹. (img1_shape) Π΄ΠΎ Ρ„ΠΎΡ€ΠΌΡ‹ Π΄Ρ€ΡƒΠ³ΠΎΠ³ΠΎ изобраТСния (img0_shape).

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
img1_shape tuple

Π€ΠΎΡ€ΠΌΠ° изобраТСния, для ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ ΡΠΎΠ·Π΄Π°ΡŽΡ‚ΡΡ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (высота, ΡˆΠΈΡ€ΠΈΠ½Π°).

трСбуСтся
boxes Tensor

ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x1, y1, x2, y2)

трСбуСтся
img0_shape tuple

Ρ„ΠΎΡ€ΠΌΠ° Ρ†Π΅Π»Π΅Π²ΠΎΠ³ΠΎ изобраТСния Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (высота, ΡˆΠΈΡ€ΠΈΠ½Π°).

трСбуСтся
ratio_pad tuple

ΠΊΠΎΡ€Ρ‚Π΅ΠΆ ΠΈΠ· (ratio, pad) для ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΡ ящиков. Если Π½Π΅ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ, Ρ‚ΠΎ коэффициСнт ΠΈ pad Π±ΡƒΠ΄ΡƒΡ‚ рассчитаны Π½Π° основС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ Π² Ρ€Π°Π·ΠΌΠ΅Ρ€Π°Ρ… Π΄Π²ΡƒΡ… ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.

None
padding bool

Если True, Ρ‚ΠΎ прСдполагаСтся, Ρ‡Ρ‚ΠΎ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ основаны Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ, Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½Π½ΠΎΠΌ стилСм yolo . Если False, Ρ‚ΠΎ сдСлай ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠ΅ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡˆΡ‚Π°Π±Π°.

True
xywh bool

Π€ΠΎΡ€ΠΌΠ°Ρ‚ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ - xywh ΠΈΠ»ΠΈ Π½Π΅Ρ‚, ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ=False.

False

ВозвращаСтся:

Имя Вип ОписаниС
boxes Tensor

ΠœΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Π΅ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x1, y1, x2, y2)

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False):
    """
    Rescales bounding boxes (in the format of xyxy by default) from the shape of the image they were originally
    specified in (img1_shape) to the shape of a different image (img0_shape).

    Args:
        img1_shape (tuple): The shape of the image that the bounding boxes are for, in the format of (height, width).
        boxes (torch.Tensor): the bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2)
        img0_shape (tuple): the shape of the target image, in the format of (height, width).
        ratio_pad (tuple): a tuple of (ratio, pad) for scaling the boxes. If not provided, the ratio and pad will be
            calculated based on the size difference between the two images.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.
        xywh (bool): The box format is xywh or not, default=False.

    Returns:
        boxes (torch.Tensor): The scaled bounding boxes, in the format of (x1, y1, x2, y2)
    """
    if ratio_pad is None:  # calculate from img0_shape
        gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
        pad = (
            round((img1_shape[1] - img0_shape[1] * gain) / 2 - 0.1),
            round((img1_shape[0] - img0_shape[0] * gain) / 2 - 0.1),
        )  # wh padding
    else:
        gain = ratio_pad[0][0]
        pad = ratio_pad[1]

    if padding:
        boxes[..., 0] -= pad[0]  # x padding
        boxes[..., 1] -= pad[1]  # y padding
        if not xywh:
            boxes[..., 2] -= pad[0]  # x padding
            boxes[..., 3] -= pad[1]  # y padding
    boxes[..., :4] /= gain
    return clip_boxes(boxes, img0_shape)



ultralytics.utils.ops.make_divisible(x, divisor)

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ блиТайшСС число, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ дСлится Π½Π° Π·Π°Π΄Π°Π½Π½Ρ‹ΠΉ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
x int

Число, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π½ΡƒΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ ΠΊΡ€Π°Ρ‚Π½Ρ‹ΠΌ.

трСбуСтся
divisor int | Tensor

Π”Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ.

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
int

Π‘Π»ΠΈΠΆΠ°ΠΉΡˆΠ΅Π΅ число, ΠΊΡ€Π°Ρ‚Π½ΠΎΠ΅ Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŽ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def make_divisible(x, divisor):
    """
    Returns the nearest number that is divisible by the given divisor.

    Args:
        x (int): The number to make divisible.
        divisor (int | torch.Tensor): The divisor.

    Returns:
        (int): The nearest number divisible by the divisor.
    """
    if isinstance(divisor, torch.Tensor):
        divisor = int(divisor.max())  # to int
    return math.ceil(x / divisor) * divisor



ultralytics.utils.ops.nms_rotated(boxes, scores, threshold=0.45)

NMS для obbs, Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‰Π°Ρ Π½Π° основС probiou ΠΈ fast-nms.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
boxes Tensor

(N, 5), xywhr.

трСбуСтся
scores Tensor

(N, ).

трСбуСтся
threshold float

ΠŸΠΎΡ€ΠΎΠ³ IoU.

0.45

ВозвращаСтся:

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def nms_rotated(boxes, scores, threshold=0.45):
    """
    NMS for obbs, powered by probiou and fast-nms.

    Args:
        boxes (torch.Tensor): (N, 5), xywhr.
        scores (torch.Tensor): (N, ).
        threshold (float): IoU threshold.

    Returns:
    """
    if len(boxes) == 0:
        return np.empty((0,), dtype=np.int8)
    sorted_idx = torch.argsort(scores, descending=True)
    boxes = boxes[sorted_idx]
    ious = batch_probiou(boxes, boxes).triu_(diagonal=1)
    pick = torch.nonzero(ious.max(dim=0)[0] < threshold).squeeze_(-1)
    return sorted_idx[pick]



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

Π€ΠΎΡ€ΠΌΠ° tensor (batch_size, num_classes + 4 + num_masks, num_boxes) содСрТащий прСдсказанныС ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ, классы ΠΈ маски. Π€ΠΎΡ€ΠΌΠ°Ρ‚ 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, Ρ‚ΠΎ Π±ΡƒΠ΄ΡƒΡ‚ Ρ€Π°ΡΡΠΌΠ°Ρ‚Ρ€ΠΈΠ²Π°Ρ‚ΡŒΡΡ всС классы.

None
agnostic bool

Если True, Ρ‚ΠΎ модСль Π½Π΅ зависит ΠΎΡ‚ количСства классов, ΠΈ всС классы Π±ΡƒΠ΄ΡƒΡ‚ Ρ€Π°ΡΡΠΌΠ°Ρ‚Ρ€ΠΈΠ²Π°Ρ‚ΡŒΡΡ ΠΊΠ°ΠΊ ΠΎΠ΄ΠΈΠ½.

False
multi_label bool

Если True, Ρ‚ΠΎ Ρƒ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ нСсколько ярлыков.

False
labels List[List[Union[int, float, Tensor]]]

Бписок списков, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Π²Π½ΡƒΡ‚Ρ€Π΅Π½Π½ΠΈΠΉ список содСрТит ΠΌΠ΅Ρ‚ΠΊΠΈ apriori для Π΄Π°Π½Π½ΠΎΠ³ΠΎ изобраТСния. Бписок Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌΡ‹ΠΉ dataloader'ΠΎΠΌ, Π³Π΄Π΅ каТдая ΠΌΠ΅Ρ‚ΠΊΠ° - ΠΊΠΎΡ€Ρ‚Π΅ΠΆ (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
def 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,  # number of classes (optional)
    max_time_img=0.05,
    max_nms=30000,
    max_wh=7680,
    in_place=True,
    rotated=False,
):
    """
    Perform non-maximum suppression (NMS) on a set of boxes, with support for masks and multiple labels per box.

    Args:
        prediction (torch.Tensor): A tensor of shape (batch_size, num_classes + 4 + num_masks, num_boxes)
            containing the predicted boxes, classes, and masks. The tensor should be in the format
            output by a model, such as YOLO.
        conf_thres (float): The confidence threshold below which boxes will be filtered out.
            Valid values are between 0.0 and 1.0.
        iou_thres (float): The IoU threshold below which boxes will be filtered out during NMS.
            Valid values are between 0.0 and 1.0.
        classes (List[int]): A list of class indices to consider. If None, all classes will be considered.
        agnostic (bool): If True, the model is agnostic to the number of classes, and all
            classes will be considered as one.
        multi_label (bool): If True, each box may have multiple labels.
        labels (List[List[Union[int, float, torch.Tensor]]]): A list of lists, where each inner
            list contains the apriori labels for a given image. The list should be in the format
            output by a dataloader, with each label being a tuple of (class_index, x1, y1, x2, y2).
        max_det (int): The maximum number of boxes to keep after NMS.
        nc (int, optional): The number of classes output by the model. Any indices after this will be considered masks.
        max_time_img (float): The maximum time (seconds) for processing one image.
        max_nms (int): The maximum number of boxes into torchvision.ops.nms().
        max_wh (int): The maximum box width and height in pixels.
        in_place (bool): If True, the input prediction tensor will be modified in place.

    Returns:
        (List[torch.Tensor]): A list of length batch_size, where each element is a tensor of
            shape (num_boxes, 6 + num_masks) containing the kept boxes, with columns
            (x1, y1, x2, y2, confidence, class, mask1, mask2, ...).
    """
    import torchvision  # scope for faster 'import ultralytics'

    # Checks
    assert 0 <= conf_thres <= 1, f"Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0"
    assert 0 <= iou_thres <= 1, f"Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0"
    if isinstance(prediction, (list, tuple)):  # YOLOv8 model in validation model, output = (inference_out, loss_out)
        prediction = prediction[0]  # select only inference output

    bs = prediction.shape[0]  # batch size
    nc = nc or (prediction.shape[1] - 4)  # number of classes
    nm = prediction.shape[1] - nc - 4
    mi = 4 + nc  # mask start index
    xc = prediction[:, 4:mi].amax(1) > conf_thres  # candidates

    # Settings
    # min_wh = 2  # (pixels) minimum box width and height
    time_limit = 2.0 + max_time_img * bs  # seconds to quit after
    multi_label &= nc > 1  # multiple labels per box (adds 0.5ms/img)

    prediction = prediction.transpose(-1, -2)  # shape(1,84,6300) to shape(1,6300,84)
    if not rotated:
        if in_place:
            prediction[..., :4] = xywh2xyxy(prediction[..., :4])  # xywh to xyxy
        else:
            prediction = torch.cat((xywh2xyxy(prediction[..., :4]), prediction[..., 4:]), dim=-1)  # xywh to xyxy

    t = time.time()
    output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs
    for xi, x in enumerate(prediction):  # image index, image inference
        # Apply constraints
        # x[((x[:, 2:4] < min_wh) | (x[:, 2:4] > max_wh)).any(1), 4] = 0  # width-height
        x = x[xc[xi]]  # confidence

        # Cat apriori labels if autolabelling
        if labels and len(labels[xi]) and not rotated:
            lb = labels[xi]
            v = torch.zeros((len(lb), nc + nm + 4), device=x.device)
            v[:, :4] = xywh2xyxy(lb[:, 1:5])  # box
            v[range(len(lb)), lb[:, 0].long() + 4] = 1.0  # cls
            x = torch.cat((x, v), 0)

        # If none remain process next image
        if not x.shape[0]:
            continue

        # Detections matrix nx6 (xyxy, conf, cls)
        box, cls, mask = x.split((4, nc, nm), 1)

        if multi_label:
            i, j = torch.where(cls > conf_thres)
            x = torch.cat((box[i], x[i, 4 + j, None], j[:, None].float(), mask[i]), 1)
        else:  # best class only
            conf, j = cls.max(1, keepdim=True)
            x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres]

        # Filter by class
        if classes is not None:
            x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]

        # Check shape
        n = x.shape[0]  # number of boxes
        if not n:  # no boxes
            continue
        if n > max_nms:  # excess boxes
            x = x[x[:, 4].argsort(descending=True)[:max_nms]]  # sort by confidence and remove excess boxes

        # Batched NMS
        c = x[:, 5:6] * (0 if agnostic else max_wh)  # classes
        scores = x[:, 4]  # scores
        if rotated:
            boxes = torch.cat((x[:, :2] + c, x[:, 2:4], x[:, -1:]), dim=-1)  # xywhr
            i = nms_rotated(boxes, scores, iou_thres)
        else:
            boxes = x[:, :4] + c  # boxes (offset by class)
            i = torchvision.ops.nms(boxes, scores, iou_thres)  # NMS
        i = i[:max_det]  # limit detections

        # # Experimental
        # merge = False  # use merge-NMS
        # if merge and (1 < n < 3E3):  # Merge NMS (boxes merged using weighted mean)
        #     # Update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
        #     from .metrics import box_iou
        #     iou = box_iou(boxes[i], boxes) > iou_thres  # IoU matrix
        #     weights = iou * scores[None]  # box weights
        #     x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True)  # merged boxes
        #     redundant = True  # require redundant detections
        #     if redundant:
        #         i = i[iou.sum(1) > 1]  # require redundancy

        output[xi] = x[i]
        if (time.time() - t) > time_limit:
            LOGGER.warning(f"WARNING ⚠️ NMS time limit {time_limit:.3f}s exceeded")
            break  # time limit exceeded

    return output



ultralytics.utils.ops.clip_boxes(boxes, shape)

Π‘Π΅Ρ€Π΅Ρ‚ список ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ ΠΈ Ρ„ΠΈΠ³ΡƒΡ€Ρƒ (высота, ΡˆΠΈΡ€ΠΈΠ½Π°) ΠΈ прикрСпляСт ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ ΠΊ Ρ„ΠΈΠ³ΡƒΡ€Π΅.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
boxes Tensor

ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ для ΠΎΠ±Ρ€Π΅Π·ΠΊΠΈ

трСбуСтся
shape tuple

Ρ„ΠΎΡ€ΠΌΠ° изобраТСния

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
Tensor | ndarray

ΠžΠ±Ρ€Π΅Π·Π°Π½Π½Ρ‹Π΅ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def clip_boxes(boxes, shape):
    """
    Takes a list of bounding boxes and a shape (height, width) and clips the bounding boxes to the shape.

    Args:
        boxes (torch.Tensor): the bounding boxes to clip
        shape (tuple): the shape of the image

    Returns:
        (torch.Tensor | numpy.ndarray): Clipped boxes
    """
    if isinstance(boxes, torch.Tensor):  # faster individually (WARNING: inplace .clamp_() Apple MPS bug)
        boxes[..., 0] = boxes[..., 0].clamp(0, shape[1])  # x1
        boxes[..., 1] = boxes[..., 1].clamp(0, shape[0])  # y1
        boxes[..., 2] = boxes[..., 2].clamp(0, shape[1])  # x2
        boxes[..., 3] = boxes[..., 3].clamp(0, shape[0])  # y2
    else:  # np.array (faster grouped)
        boxes[..., [0, 2]] = boxes[..., [0, 2]].clip(0, shape[1])  # x1, x2
        boxes[..., [1, 3]] = boxes[..., [1, 3]].clip(0, shape[0])  # y1, y2
    return boxes



ultralytics.utils.ops.clip_coords(coords, shape)

ΠžΠ±Ρ€Π΅ΠΆΡŒ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π»ΠΈΠ½ΠΈΠΉ Π½Π° Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ изобраТСния.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
coords Tensor | ndarray

Бписок ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ Π»ΠΈΠ½ΠΈΠΉ.

трСбуСтся
shape tuple

ΠšΠΎΡ€Ρ‚Π΅ΠΆ Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΡŽΡ‰ΠΈΡ… Ρ€Π°Π·ΠΌΠ΅Ρ€ изобраТСния Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (высота, ΡˆΠΈΡ€ΠΈΠ½Π°).

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
Tensor | ndarray

ΠžΠ±Ρ€Π΅Π·Π°Π½Π½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def clip_coords(coords, shape):
    """
    Clip line coordinates to the image boundaries.

    Args:
        coords (torch.Tensor | numpy.ndarray): A list of line coordinates.
        shape (tuple): A tuple of integers representing the size of the image in the format (height, width).

    Returns:
        (torch.Tensor | numpy.ndarray): Clipped coordinates
    """
    if isinstance(coords, torch.Tensor):  # faster individually (WARNING: inplace .clamp_() Apple MPS bug)
        coords[..., 0] = coords[..., 0].clamp(0, shape[1])  # x
        coords[..., 1] = coords[..., 1].clamp(0, shape[0])  # y
    else:  # np.array (faster grouped)
        coords[..., 0] = coords[..., 0].clip(0, shape[1])  # x
        coords[..., 1] = coords[..., 1].clip(0, shape[0])  # y
    return coords



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
def scale_image(masks, im0_shape, ratio_pad=None):
    """
    Takes a mask, and resizes it to the original image size.

    Args:
        masks (np.ndarray): resized and padded masks/images, [h, w, num]/[h, w, 3].
        im0_shape (tuple): the original image shape
        ratio_pad (tuple): the ratio of the padding to the original image.

    Returns:
        masks (torch.Tensor): The masks that are being returned.
    """
    # Rescale coordinates (xyxy) from im1_shape to im0_shape
    im1_shape = masks.shape
    if im1_shape[:2] == im0_shape[:2]:
        return masks
    if ratio_pad is None:  # calculate from im0_shape
        gain = min(im1_shape[0] / im0_shape[0], im1_shape[1] / im0_shape[1])  # gain  = old / new
        pad = (im1_shape[1] - im0_shape[1] * gain) / 2, (im1_shape[0] - im0_shape[0] * gain) / 2  # wh padding
    else:
        # gain = ratio_pad[0][0]
        pad = ratio_pad[1]
    top, left = int(pad[1]), int(pad[0])  # y, x
    bottom, right = int(im1_shape[0] - pad[1]), int(im1_shape[1] - pad[0])

    if len(masks.shape) < 2:
        raise ValueError(f'"len of masks shape" should be 2 or 3, but got {len(masks.shape)}')
    masks = masks[top:bottom, left:right]
    masks = cv2.resize(masks, (im0_shape[1], im0_shape[0]))
    if len(masks.shape) == 2:
        masks = masks[:, :, None]

    return masks



ultralytics.utils.ops.xyxy2xywh(x)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ ΠΈΠ· Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° (x1, y1, x2, y2) Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ (x, y, width, height), Π³Π΄Π΅ (x1, y1) - это Π»Π΅Π²Ρ‹ΠΉ Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ ΡƒΠ³ΠΎΠ», Π° (x2, y2) - ΠΏΡ€Π°Π²Ρ‹ΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ ΡƒΠ³ΠΎΠ».

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
x ndarray | Tensor

Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x1, y1, x2, y2).

трСбуСтся

ВозвращаСтся:

Имя Вип ОписаниС
y ndarray | Tensor

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x, y, width, height).

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def xyxy2xywh(x):
    """
    Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height) format where (x1, y1) is the
    top-left corner and (x2, y2) is the bottom-right corner.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x, y, width, height) format.
    """
    assert x.shape[-1] == 4, f"input shape last dimension expected 4 but input shape is {x.shape}"
    y = torch.empty_like(x) if isinstance(x, torch.Tensor) else np.empty_like(x)  # faster than clone/copy
    y[..., 0] = (x[..., 0] + x[..., 2]) / 2  # x center
    y[..., 1] = (x[..., 1] + x[..., 3]) / 2  # y center
    y[..., 2] = x[..., 2] - x[..., 0]  # width
    y[..., 3] = x[..., 3] - x[..., 1]  # height
    return y



ultralytics.utils.ops.xywh2xyxy(x)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ ΠΈΠ· Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° (x, y, width, height) Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ (x1, y1, x2, y2), Π³Π΄Π΅ (x1, y1) - это Π»Π΅Π²Ρ‹ΠΉ Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ ΡƒΠ³ΠΎΠ», Π° (x2, y2) - ΠΏΡ€Π°Π²Ρ‹ΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ ΡƒΠ³ΠΎΠ».

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
x ndarray | Tensor

Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x, y, width, height).

трСбуСтся

ВозвращаСтся:

Имя Вип ОписаниС
y ndarray | Tensor

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x1, y1, x2, y2).

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def xywh2xyxy(x):
    """
    Convert bounding box coordinates from (x, y, width, height) format to (x1, y1, x2, y2) format where (x1, y1) is the
    top-left corner and (x2, y2) is the bottom-right corner.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x, y, width, height) format.

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x1, y1, x2, y2) format.
    """
    assert x.shape[-1] == 4, f"input shape last dimension expected 4 but input shape is {x.shape}"
    y = torch.empty_like(x) if isinstance(x, torch.Tensor) else np.empty_like(x)  # faster than clone/copy
    dw = x[..., 2] / 2  # half-width
    dh = x[..., 3] / 2  # half-height
    y[..., 0] = x[..., 0] - dw  # top left x
    y[..., 1] = x[..., 1] - dh  # top left y
    y[..., 2] = x[..., 0] + dw  # bottom right x
    y[..., 3] = x[..., 1] + dh  # bottom right y
    return y



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
def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
    """
    Convert normalized bounding box coordinates to pixel coordinates.

    Args:
        x (np.ndarray | torch.Tensor): The bounding box coordinates.
        w (int): Width of the image. Defaults to 640
        h (int): Height of the image. Defaults to 640
        padw (int): Padding width. Defaults to 0
        padh (int): Padding height. Defaults to 0
    Returns:
        y (np.ndarray | torch.Tensor): The coordinates of the bounding box in the format [x1, y1, x2, y2] where
            x1,y1 is the top-left corner, x2,y2 is the bottom-right corner of the bounding box.
    """
    assert x.shape[-1] == 4, f"input shape last dimension expected 4 but input shape is {x.shape}"
    y = torch.empty_like(x) if isinstance(x, torch.Tensor) else np.empty_like(x)  # faster than clone/copy
    y[..., 0] = w * (x[..., 0] - x[..., 2] / 2) + padw  # top left x
    y[..., 1] = h * (x[..., 1] - x[..., 3] / 2) + padh  # top left y
    y[..., 2] = w * (x[..., 0] + x[..., 2] / 2) + padw  # bottom right x
    y[..., 3] = h * (x[..., 1] + x[..., 3] / 2) + padh  # bottom right y
    return y



ultralytics.utils.ops.xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ ΠΈΠ· Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° (x1, y1, x2, y2) Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ (x, y, width, height, normalized). x, y, ΡˆΠΈΡ€ΠΈΠ½Π° ΠΈ высота Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Ρ‹ ΠΊ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°ΠΌ изобраТСния.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
x ndarray | Tensor

Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x1, y1, x2, y2).

трСбуСтся
w int

Π¨ΠΈΡ€ΠΈΠ½Π° изобраТСния. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 640

640
h int

Высота изобраТСния. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 640

640
clip bool

Если Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ True, Ρ‚ΠΎ боксы Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΠ±Ρ€Π΅Π·Π°Π½Ρ‹ ΠΏΠΎ Π³Ρ€Π°Π½ΠΈΡ†Π°ΠΌ изобраТСния. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ установлСно Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ False

False
eps float

МинимальноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΈ высоты бокса. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ 0,0

0.0

ВозвращаСтся:

Имя Вип ОписаниС
y ndarray | Tensor

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (x, y, ΡˆΠΈΡ€ΠΈΠ½Π°, высота, нормализация)

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0):
    """
    Convert bounding box coordinates from (x1, y1, x2, y2) format to (x, y, width, height, normalized) format. x, y,
    width and height are normalized to image dimensions.

    Args:
        x (np.ndarray | torch.Tensor): The input bounding box coordinates in (x1, y1, x2, y2) format.
        w (int): The width of the image. Defaults to 640
        h (int): The height of the image. Defaults to 640
        clip (bool): If True, the boxes will be clipped to the image boundaries. Defaults to False
        eps (float): The minimum value of the box's width and height. Defaults to 0.0

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in (x, y, width, height, normalized) format
    """
    if clip:
        x = clip_boxes(x, (h - eps, w - eps))
    assert x.shape[-1] == 4, f"input shape last dimension expected 4 but input shape is {x.shape}"
    y = torch.empty_like(x) if isinstance(x, torch.Tensor) else np.empty_like(x)  # faster than clone/copy
    y[..., 0] = ((x[..., 0] + x[..., 2]) / 2) / w  # x center
    y[..., 1] = ((x[..., 1] + x[..., 3]) / 2) / h  # y center
    y[..., 2] = (x[..., 2] - x[..., 0]) / w  # width
    y[..., 3] = (x[..., 3] - x[..., 1]) / h  # height
    return y



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
def xywh2ltwh(x):
    """
    Convert the bounding box format from [x, y, w, h] to [x1, y1, w, h], where x1, y1 are the top-left coordinates.

    Args:
        x (np.ndarray | torch.Tensor): The input tensor with the bounding box coordinates in the xywh format

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xyltwh format
    """
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 0] = x[..., 0] - x[..., 2] / 2  # top left x
    y[..., 1] = x[..., 1] - x[..., 3] / 2  # top left y
    return y



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
def xyxy2ltwh(x):
    """
    Convert nx4 bounding boxes from [x1, y1, x2, y2] to [x1, y1, w, h], where xy1=top-left, xy2=bottom-right.

    Args:
        x (np.ndarray | torch.Tensor): The input tensor with the bounding boxes coordinates in the xyxy format

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xyltwh format.
    """
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 2] = x[..., 2] - x[..., 0]  # width
    y[..., 3] = x[..., 3] - x[..., 1]  # height
    return y



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
def ltwh2xywh(x):
    """
    Convert nx4 boxes from [x1, y1, w, h] to [x, y, w, h] where xy1=top-left, xy=center.

    Args:
        x (torch.Tensor): the input tensor

    Returns:
        y (np.ndarray | torch.Tensor): The bounding box coordinates in the xywh format.
    """
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 0] = x[..., 0] + x[..., 2] / 2  # center x
    y[..., 1] = x[..., 1] + x[..., 3] / 2  # center y
    return y



ultralytics.utils.ops.xyxyxyxy2xywhr(corners)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΏΠ°ΠΊΠ΅Ρ‚Π½Ρ‹Π΅ ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Π΅ Π³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Π΅ боксы (OBB) ΠΈΠ· [xy1, xy2, xy3, xy4] Π² [xywh, rotation]. ЗначСния вращСния ΠΎΠΆΠΈΠ΄Π°ΡŽΡ‚ΡΡ Π² градусах ΠΎΡ‚ 0 Π΄ΠΎ 90.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
corners ndarray | Tensor

Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ ΡƒΠ³Π»Ρ‹ Ρ„ΠΎΡ€ΠΌΡ‹ (n, 8).

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
ndarray | Tensor

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ [cx, cy, w, h, rotation] Ρ„ΠΎΡ€ΠΌΡ‹ (n, 5).

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def xyxyxyxy2xywhr(corners):
    """
    Convert batched Oriented Bounding Boxes (OBB) from [xy1, xy2, xy3, xy4] to [xywh, rotation]. Rotation values are
    expected in degrees from 0 to 90.

    Args:
        corners (numpy.ndarray | torch.Tensor): Input corners of shape (n, 8).

    Returns:
        (numpy.ndarray | torch.Tensor): Converted data in [cx, cy, w, h, rotation] format of shape (n, 5).
    """
    is_torch = isinstance(corners, torch.Tensor)
    points = corners.cpu().numpy() if is_torch else corners
    points = points.reshape(len(corners), -1, 2)
    rboxes = []
    for pts in points:
        # NOTE: Use cv2.minAreaRect to get accurate xywhr,
        # especially some objects are cut off by augmentations in dataloader.
        (x, y), (w, h), angle = cv2.minAreaRect(pts)
        rboxes.append([x, y, w, h, angle / 180 * np.pi])
    return (
        torch.tensor(rboxes, device=corners.device, dtype=corners.dtype)
        if is_torch
        else np.asarray(rboxes, dtype=points.dtype)
    )  # rboxes



ultralytics.utils.ops.xywhr2xyxyxyxy(rboxes)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΏΠ°ΠΊΠ΅Ρ‚Π½Ρ‹Π΅ ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Π΅ Π³Ρ€Π°Π½ΠΈΡ‡Π½Ρ‹Π΅ боксы (OBB) ΠΈΠ· [xywh, rotation] Π² [xy1, xy2, xy3, xy4]. ЗначСния вращСния Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ Π±Ρ‹Ρ‚ΡŒ Π² градусах ΠΎΡ‚ 0 Π΄ΠΎ 90.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
rboxes ndarray | Tensor

ΠšΠΎΡ€ΠΎΠ±ΠΊΠΈ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ [cx, cy, w, h, rotation] Ρ„ΠΎΡ€ΠΌΡ‹ (n, 5) ΠΈΠ»ΠΈ (b, n, 5).

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
ndarray | Tensor

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½Π½Ρ‹Π΅ ΡƒΠ³Π»ΠΎΠ²Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ Ρ„ΠΈΠ³ΡƒΡ€Ρ‹ (n, 4, 2) ΠΈΠ»ΠΈ (b, n, 4, 2).

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def xywhr2xyxyxyxy(rboxes):
    """
    Convert batched Oriented Bounding Boxes (OBB) from [xywh, rotation] to [xy1, xy2, xy3, xy4]. Rotation values should
    be in degrees from 0 to 90.

    Args:
        rboxes (numpy.ndarray | torch.Tensor): Boxes in [cx, cy, w, h, rotation] format of shape (n, 5) or (b, n, 5).

    Returns:
        (numpy.ndarray | torch.Tensor): Converted corner points of shape (n, 4, 2) or (b, n, 4, 2).
    """
    is_numpy = isinstance(rboxes, np.ndarray)
    cos, sin = (np.cos, np.sin) if is_numpy else (torch.cos, torch.sin)

    ctr = rboxes[..., :2]
    w, h, angle = (rboxes[..., i : i + 1] for i in range(2, 5))
    cos_value, sin_value = cos(angle), sin(angle)
    vec1 = [w / 2 * cos_value, w / 2 * sin_value]
    vec2 = [-h / 2 * sin_value, h / 2 * cos_value]
    vec1 = np.concatenate(vec1, axis=-1) if is_numpy else torch.cat(vec1, dim=-1)
    vec2 = np.concatenate(vec2, axis=-1) if is_numpy else torch.cat(vec2, dim=-1)
    pt1 = ctr + vec1 + vec2
    pt2 = ctr + vec1 - vec2
    pt3 = ctr - vec1 - vec2
    pt4 = ctr - vec1 + vec2
    return np.stack([pt1, pt2, pt3, pt4], axis=-2) if is_numpy else torch.stack([pt1, pt2, pt3, pt4], dim=-2)



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
def ltwh2xyxy(x):
    """
    It converts the bounding box from [x1, y1, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right.

    Args:
        x (np.ndarray | torch.Tensor): the input image

    Returns:
        y (np.ndarray | torch.Tensor): the xyxy coordinates of the bounding boxes.
    """
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[..., 2] = x[..., 2] + x[..., 0]  # width
    y[..., 3] = x[..., 3] + x[..., 1]  # height
    return y



ultralytics.utils.ops.segments2boxes(segments)

Он ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ ΠΌΠ΅Ρ‚ΠΊΠΈ сСгмСнтов Π² ΠΌΠ΅Ρ‚ΠΊΠΈ боксов, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ (cls, xy1, xy2, ...) Π² (cls, xywh).

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
segments list

список ΠΎΡ‚Ρ€Π΅Π·ΠΊΠΎΠ², ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΎΡ‚Ρ€Π΅Π·ΠΎΠΊ - список Ρ‚ΠΎΡ‡Π΅ΠΊ, каТдая Ρ‚ΠΎΡ‡ΠΊΠ° - список ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ x, y

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
ndarray

ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ xywh ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ€Π°ΠΌΠΎΠΊ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def segments2boxes(segments):
    """
    It converts segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh)

    Args:
        segments (list): list of segments, each segment is a list of points, each point is a list of x, y coordinates

    Returns:
        (np.ndarray): the xywh coordinates of the bounding boxes.
    """
    boxes = []
    for s in segments:
        x, y = s.T  # segment xy
        boxes.append([x.min(), y.min(), x.max(), y.max()])  # cls, xyxy
    return xyxy2xywh(np.array(boxes))  # cls, xywh



ultralytics.utils.ops.resample_segments(segments, n=1000)

На Π²Ρ…ΠΎΠ΄ поступаСт список ΠΎΡ‚Ρ€Π΅Π·ΠΊΠΎΠ² (n,2), Π° возвращаСтся список ΠΎΡ‚Ρ€Π΅Π·ΠΊΠΎΠ² (n,2), ΡƒΠ²Π΅Π»ΠΈΡ‡Π΅Π½Π½Ρ‹Ρ… Π΄ΠΎ n Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
segments list

список массивов (n,2), Π³Π΄Π΅ n - количСство Ρ‚ΠΎΡ‡Π΅ΠΊ Π² ΠΎΡ‚Ρ€Π΅Π·ΠΊΠ΅.

трСбуСтся
n int

ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ Ρ‚ΠΎΡ‡Π΅ΠΊ, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Π½ΡƒΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅ΡΡΠΌΠΏΠ»ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΡ‚Ρ€Π΅Π·ΠΎΠΊ. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - 1000

1000

ВозвращаСтся:

Имя Вип ОписаниС
segments list

пСрСснятыС сСгмСнты.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def resample_segments(segments, n=1000):
    """
    Inputs a list of segments (n,2) and returns a list of segments (n,2) up-sampled to n points each.

    Args:
        segments (list): a list of (n,2) arrays, where n is the number of points in the segment.
        n (int): number of points to resample the segment to. Defaults to 1000

    Returns:
        segments (list): the resampled segments.
    """
    for i, s in enumerate(segments):
        s = np.concatenate((s, s[0:1, :]), axis=0)
        x = np.linspace(0, len(s) - 1, n)
        xp = np.arange(len(s))
        segments[i] = (
            np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)], dtype=np.float32).reshape(2, -1).T
        )  # segment xy
    return segments



ultralytics.utils.ops.crop_mask(masks, boxes)

Она ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ маску ΠΈ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΡƒΡŽ Ρ€Π°ΠΌΠΊΡƒ ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ маску, ΠΎΠ±Ρ€Π΅Π·Π°Π½Π½ΡƒΡŽ ΠΏΠΎ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠ΅.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
masks Tensor

[n, h, w] tensor масок

трСбуСтся
boxes Tensor

[n, 4] tensor ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ bbox Π² Ρ„ΠΎΡ€ΠΌΠ΅ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
Tensor

Маски ΠΎΠ±Ρ€Π΅Π·Π°ΡŽΡ‚ΡΡ ΠΏΠΎ ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Ρ€Π°ΠΌΠΊΠ΅.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def crop_mask(masks, boxes):
    """
    It takes a mask and a bounding box, and returns a mask that is cropped to the bounding box.

    Args:
        masks (torch.Tensor): [n, h, w] tensor of masks
        boxes (torch.Tensor): [n, 4] tensor of bbox coordinates in relative point form

    Returns:
        (torch.Tensor): The masks are being cropped to the bounding box.
    """
    _, h, w = masks.shape
    x1, y1, x2, y2 = torch.chunk(boxes[:, :, None], 4, 1)  # x1 shape(n,1,1)
    r = torch.arange(w, device=masks.device, dtype=x1.dtype)[None, None, :]  # rows shape(1,1,w)
    c = torch.arange(h, device=masks.device, dtype=x1.dtype)[None, :, None]  # cols shape(1,h,1)

    return masks * ((r >= x1) * (r < x2) * (c >= y1) * (c < y2))



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
def process_mask_upsample(protos, masks_in, bboxes, shape):
    """
    Takes the output of the mask head, and applies the mask to the bounding boxes. This produces masks of higher quality
    but is slower.

    Args:
        protos (torch.Tensor): [mask_dim, mask_h, mask_w]
        masks_in (torch.Tensor): [n, mask_dim], n is number of masks after nms
        bboxes (torch.Tensor): [n, 4], n is number of masks after nms
        shape (tuple): the size of the input image (h,w)

    Returns:
        (torch.Tensor): The upsampled masks.
    """
    c, mh, mw = protos.shape  # CHW
    masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw)
    masks = F.interpolate(masks[None], shape, mode="bilinear", align_corners=False)[0]  # CHW
    masks = crop_mask(masks, bboxes)  # CHW
    return masks.gt_(0.5)



ultralytics.utils.ops.process_mask(protos, masks_in, bboxes, shape, upsample=False)

НалоТи маски Π½Π° ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ Π²Ρ‹Π²ΠΎΠ΄ Π³ΠΎΠ»ΠΎΠ²Ρ‹ маски.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
protos Tensor

tensor Ρ„ΠΎΡ€ΠΌΡ‹ [mask_dim, mask_h, mask_w].

трСбуСтся
masks_in Tensor

tensor Ρ„ΠΎΡ€ΠΌΡ‹ [n, mask_dim], Π³Π΄Π΅ n - количСство масок послС NMS.

трСбуСтся
bboxes Tensor

tensor Ρ„ΠΎΡ€ΠΌΡ‹ [n, 4], Π³Π΄Π΅ n - количСство масок послС НМБ.

трСбуСтся
shape tuple

ΠšΠΎΡ€Ρ‚Π΅ΠΆ Ρ†Π΅Π»Ρ‹Ρ… чисСл, ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΡŽΡ‰ΠΈΡ… Ρ€Π°Π·ΠΌΠ΅Ρ€ Π²Ρ…ΠΎΠ΄Π½ΠΎΠ³ΠΎ изобраТСния Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ (h, w).

трСбуСтся
upsample bool

Π€Π»Π°Π³, ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‰ΠΈΠΉ, Π½ΡƒΠΆΠ½ΠΎ Π»ΠΈ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Ρ‚ΡŒ Ρ€Π°Π·ΠΌΠ΅Ρ€ маски Π΄ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° исходного изобраТСния. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ - False.

False

ВозвращаСтся:

Вип ОписаниС
Tensor

Двоичная маска tensor ΠΈΠΌΠ΅Π΅Ρ‚ Ρ„ΠΎΡ€ΠΌΡƒ [n, h, w], Π³Π΄Π΅ n - количСство масок послС НМБ, Π° h ΠΈ w высота ΠΈ ΡˆΠΈΡ€ΠΈΠ½Π° Π²Ρ…ΠΎΠ΄Π½ΠΎΠ³ΠΎ изобраТСния. Маска накладываСтся Π½Π° ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ€Π°ΠΌΠΊΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def process_mask(protos, masks_in, bboxes, shape, upsample=False):
    """
    Apply masks to bounding boxes using the output of the mask head.

    Args:
        protos (torch.Tensor): A tensor of shape [mask_dim, mask_h, mask_w].
        masks_in (torch.Tensor): A tensor of shape [n, mask_dim], where n is the number of masks after NMS.
        bboxes (torch.Tensor): A tensor of shape [n, 4], where n is the number of masks after NMS.
        shape (tuple): A tuple of integers representing the size of the input image in the format (h, w).
        upsample (bool): A flag to indicate whether to upsample the mask to the original image size. Default is False.

    Returns:
        (torch.Tensor): A binary mask tensor of shape [n, h, w], where n is the number of masks after NMS, and h and w
            are the height and width of the input image. The mask is applied to the bounding boxes.
    """

    c, mh, mw = protos.shape  # CHW
    ih, iw = shape
    masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw)  # CHW
    width_ratio = mw / iw
    height_ratio = mh / ih

    downsampled_bboxes = bboxes.clone()
    downsampled_bboxes[:, 0] *= width_ratio
    downsampled_bboxes[:, 2] *= width_ratio
    downsampled_bboxes[:, 3] *= height_ratio
    downsampled_bboxes[:, 1] *= height_ratio

    masks = crop_mask(masks, downsampled_bboxes)  # CHW
    if upsample:
        masks = F.interpolate(masks[None], shape, mode="bilinear", align_corners=False)[0]  # CHW
    return masks.gt_(0.5)



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
def process_mask_native(protos, masks_in, bboxes, shape):
    """
    It takes the output of the mask head, and crops it after upsampling to the bounding boxes.

    Args:
        protos (torch.Tensor): [mask_dim, mask_h, mask_w]
        masks_in (torch.Tensor): [n, mask_dim], n is number of masks after nms
        bboxes (torch.Tensor): [n, 4], n is number of masks after nms
        shape (tuple): the size of the input image (h,w)

    Returns:
        masks (torch.Tensor): The returned masks with dimensions [h, w, n]
    """
    c, mh, mw = protos.shape  # CHW
    masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw)
    masks = scale_masks(masks[None], shape)[0]  # CHW
    masks = crop_mask(masks, bboxes)  # CHW
    return masks.gt_(0.5)



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
def scale_masks(masks, shape, padding=True):
    """
    Rescale segment masks to shape.

    Args:
        masks (torch.Tensor): (N, C, H, W).
        shape (tuple): Height and width.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.
    """
    mh, mw = masks.shape[2:]
    gain = min(mh / shape[0], mw / shape[1])  # gain  = old / new
    pad = [mw - shape[1] * gain, mh - shape[0] * gain]  # wh padding
    if padding:
        pad[0] /= 2
        pad[1] /= 2
    top, left = (int(pad[1]), int(pad[0])) if padding else (0, 0)  # y, x
    bottom, right = (int(mh - pad[1]), int(mw - pad[0]))
    masks = masks[..., top:bottom, left:right]

    masks = F.interpolate(masks, shape, mode="bilinear", align_corners=False)  # NCHW
    return masks



ultralytics.utils.ops.scale_coords(img1_shape, coords, img0_shape, ratio_pad=None, normalize=False, padding=True)

ΠŸΠ΅Ρ€Π΅ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΡƒΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ сСгмСнта (xy) ΠΈΠ· img1_shape Π² img0_shape.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
img1_shape tuple

Π€ΠΎΡ€ΠΌΠ° изобраТСния, с ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ взяты ΠΊΠΎΠΎΡ€Π΄Ρ‹.

трСбуСтся
coords Tensor

ΠΊΠΎΠΎΡ€Π΄Ρ‹, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π½ΡƒΠΆΠ½ΠΎ ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π² Ρ„ΠΎΡ€ΠΌΠ΅ n,2.

трСбуСтся
img0_shape tuple

Ρ„ΠΎΡ€ΠΌΠ° изобраТСния, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ примСняСтся сСгмСнтация.

трСбуСтся
ratio_pad tuple

ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° изобраТСния ΠΊ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρƒ Π½Π°Π±ΠΈΠ²ΠΊΠΈ.

None
normalize bool

Если True, Ρ‚ΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π±ΡƒΠ΄ΡƒΡ‚ Π½ΠΎΡ€ΠΌΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Ρ‹ ΠΊ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Ρƒ [0, 1]. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ установлСно Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ False.

False
padding bool

Если True, Ρ‚ΠΎ прСдполагаСтся, Ρ‡Ρ‚ΠΎ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ основаны Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ, Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½Π½ΠΎΠΌ стилСм yolo . Если False, Ρ‚ΠΎ сдСлай ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠ΅ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡˆΡ‚Π°Π±Π°.

True

ВозвращаСтся:

Имя Вип ОписаниС
coords Tensor

ΠœΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Π΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None, normalize=False, padding=True):
    """
    Rescale segment coordinates (xy) from img1_shape to img0_shape.

    Args:
        img1_shape (tuple): The shape of the image that the coords are from.
        coords (torch.Tensor): the coords to be scaled of shape n,2.
        img0_shape (tuple): the shape of the image that the segmentation is being applied to.
        ratio_pad (tuple): the ratio of the image size to the padded image size.
        normalize (bool): If True, the coordinates will be normalized to the range [0, 1]. Defaults to False.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.

    Returns:
        coords (torch.Tensor): The scaled coordinates.
    """
    if ratio_pad is None:  # calculate from img0_shape
        gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
        pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2  # wh padding
    else:
        gain = ratio_pad[0][0]
        pad = ratio_pad[1]

    if padding:
        coords[..., 0] -= pad[0]  # x padding
        coords[..., 1] -= pad[1]  # y padding
    coords[..., 0] /= gain
    coords[..., 1] /= gain
    coords = clip_coords(coords, img0_shape)
    if normalize:
        coords[..., 0] /= img0_shape[1]  # width
        coords[..., 1] /= img0_shape[0]  # height
    return coords



ultralytics.utils.ops.regularize_rboxes(rboxes)

РСгуляризируй ΠΏΠΎΠ²Π΅Ρ€Π½ΡƒΡ‚Ρ‹Π΅ ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ Π² Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½Π΅ [0, pi/2].

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
rboxes Tensor

(N, 5), xywhr.

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
Tensor

РСгуляризованныС ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def regularize_rboxes(rboxes):
    """
    Regularize rotated boxes in range [0, pi/2].

    Args:
        rboxes (torch.Tensor): (N, 5), xywhr.

    Returns:
        (torch.Tensor): The regularized boxes.
    """
    x, y, w, h, t = rboxes.unbind(dim=-1)
    # Swap edge and angle if h >= w
    w_ = torch.where(w > h, w, h)
    h_ = torch.where(w > h, h, w)
    t = torch.where(w > h, t, t + math.pi / 2) % math.pi
    return torch.stack([x, y, w_, h_, t], dim=-1)  # regularized boxes



ultralytics.utils.ops.masks2segments(masks, strategy='largest')

Она ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ список масок(n,h,w) ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ список сСгмСнтов(n,xy).

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
masks Tensor

Π²Ρ‹Ρ…ΠΎΠ΄ ΠΌΠΎΠ΄Π΅Π»ΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ прСдставляСт собой tensor Ρ„ΠΎΡ€ΠΌΡ‹ (batch_size, 160, 160)

трСбуСтся
strategy str

'concat' ΠΈΠ»ΠΈ 'largest'. По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ выбираСтся наибольший

'largest'

ВозвращаСтся:

Имя Вип ОписаниС
segments List

список масок сСгмСнтов

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def masks2segments(masks, strategy="largest"):
    """
    It takes a list of masks(n,h,w) and returns a list of segments(n,xy)

    Args:
        masks (torch.Tensor): the output of the model, which is a tensor of shape (batch_size, 160, 160)
        strategy (str): 'concat' or 'largest'. Defaults to largest

    Returns:
        segments (List): list of segment masks
    """
    segments = []
    for x in masks.int().cpu().numpy().astype("uint8"):
        c = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
        if c:
            if strategy == "concat":  # concatenate all segments
                c = np.concatenate([x.reshape(-1, 2) for x in c])
            elif strategy == "largest":  # select largest segment
                c = np.array(c[np.array([len(x) for x in c]).argmax()]).reshape(-1, 2)
        else:
            c = np.zeros((0, 2))  # no segments found
        segments.append(c.astype("float32"))
    return segments



ultralytics.utils.ops.convert_torch2numpy_batch(batch)

ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉ ΠΏΠ°Ρ€Ρ‚ΠΈΡŽ Ρ‚Π΅Π½Π·ΠΎΡ€ΠΎΠ² FP32 torch (0.0-1.0) Π² массив NumPy uint8 (0-255), мСняя раскладку с BCHW Π½Π° BHWC.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
batch Tensor

Π’Ρ…ΠΎΠ΄ tensor партия Ρ„ΠΎΡ€ΠΌΡ‹ (Batch, Channels, Height, Width) ΠΈ dtype torch.float32.

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
ndarray

Π’Ρ‹Π²Π΅Π΄ΠΈ NumPy массив batch Ρ„ΠΎΡ€ΠΌΡ‹ (Batch, Height, Width, Channels) ΠΈ dtype uint8.

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def convert_torch2numpy_batch(batch: torch.Tensor) -> np.ndarray:
    """
    Convert a batch of FP32 torch tensors (0.0-1.0) to a NumPy uint8 array (0-255), changing from BCHW to BHWC layout.

    Args:
        batch (torch.Tensor): Input tensor batch of shape (Batch, Channels, Height, Width) and dtype torch.float32.

    Returns:
        (np.ndarray): Output NumPy array batch of shape (Batch, Height, Width, Channels) and dtype uint8.
    """
    return (batch.permute(0, 2, 3, 1).contiguous() * 255).clamp(0, 255).to(torch.uint8).cpu().numpy()



ultralytics.utils.ops.clean_str(s)

ΠžΡ‡ΠΈΡΡ‚ΠΈ строку, Π·Π°ΠΌΠ΅Π½ΠΈΠ² ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Π΅ символы Π½Π° ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠΈΠ²Π°Π½ΠΈΠ΅ _.

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹:

Имя Π’ΠΈΠΏ ОписаниС По ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ
s str

строка, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π½ΡƒΠΆΠ½ΠΎ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Π΅ символы

трСбуСтся

ВозвращаСтся:

Вип ОписаниС
str

строка, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Π΅ символы Π·Π°ΠΌΠ΅Π½Π΅Π½Ρ‹ Π½Π° символ подчСркивания _

Π˜ΡΡ…ΠΎΠ΄Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ Π² ultralytics/utils/ops.py
def clean_str(s):
    """
    Cleans a string by replacing special characters with underscore _

    Args:
        s (str): a string needing special characters replaced

    Returns:
        (str): a string with special characters replaced by an underscore _
    """
    return re.sub(pattern="[|@#!‘·$€%&()=?ΒΏ^*;:,¨´><+]", repl="_", string=s)





Боздано 2023-11-12, ОбновлСно 2024-05-08
Авторы: Burhan-Q (1), Laughing-q (1), glenn-jocher (4)