Reference for ultralytics/utils/ops.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/ops.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.ops.Profile
Profile(t=0.0, device: device = None)
Bases: ContextDecorator
YOLOv8 Profile class. Use as a decorator with @Profile() or as a context manager with 'with Profile():'.
Attributes:
Name | Type | Description |
---|---|---|
t |
float
|
Accumulated time. |
device |
device
|
Device used for model inference. |
cuda |
bool
|
Whether CUDA is being used. |
Examples:
>>> 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"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
Initial time. |
0.0
|
device
|
device
|
Device used for model inference. |
None
|
Source code in ultralytics/utils/ops.py
33 34 35 36 37 38 39 40 41 42 43 |
|
__enter__
__enter__()
Start timing.
Source code in ultralytics/utils/ops.py
45 46 47 48 |
|
__exit__
__exit__(type, value, traceback)
Stop timing.
Source code in ultralytics/utils/ops.py
50 51 52 53 |
|
__str__
__str__()
Returns a human-readable string representing the accumulated elapsed time in the profiler.
Source code in ultralytics/utils/ops.py
55 56 57 |
|
time
time()
Get current time.
Source code in ultralytics/utils/ops.py
59 60 61 62 63 |
|
ultralytics.utils.ops.segment2box
segment2box(segment, width=640, height=640)
Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segment
|
Tensor
|
The segment label. |
required |
width
|
int
|
The width of the image. |
640
|
height
|
int
|
The height of the image. |
640
|
Returns:
Type | Description |
---|---|
ndarray
|
The minimum and maximum x and y values of the segment. |
Source code in ultralytics/utils/ops.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
ultralytics.utils.ops.scale_boxes
scale_boxes(
img1_shape, boxes, img0_shape, ratio_pad=None, padding=True, xywh=False
)
Rescale bounding boxes from img1_shape to img0_shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img1_shape
|
tuple
|
The shape of the image that the bounding boxes are for, in the format of (height, width). |
required |
boxes
|
Tensor
|
The bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2). |
required |
img0_shape
|
tuple
|
The shape of the target image, in the format of (height, width). |
required |
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. |
None
|
padding
|
bool
|
If True, assuming the boxes is based on image augmented by yolo style. If False then do regular rescaling. |
True
|
xywh
|
bool
|
The box format is xywh or not. |
False
|
Returns:
Type | Description |
---|---|
Tensor
|
The scaled bounding boxes, in the format of (x1, y1, x2, y2). |
Source code in ultralytics/utils/ops.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
ultralytics.utils.ops.make_divisible
make_divisible(x, divisor)
Returns the nearest number that is divisible by the given divisor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
int
|
The number to make divisible. |
required |
divisor
|
int | Tensor
|
The divisor. |
required |
Returns:
Type | Description |
---|---|
int
|
The nearest number divisible by the divisor. |
Source code in ultralytics/utils/ops.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
ultralytics.utils.ops.nms_rotated
nms_rotated(boxes, scores, threshold=0.45, use_triu=True)
NMS for oriented bounding boxes using probiou and fast-nms.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
Tensor
|
Rotated bounding boxes, shape (N, 5), format xywhr. |
required |
scores
|
Tensor
|
Confidence scores, shape (N,). |
required |
threshold
|
float
|
IoU threshold. |
0.45
|
use_triu
|
bool
|
Whether to use |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
Indices of boxes to keep after NMS. |
Source code in ultralytics/utils/ops.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
ultralytics.utils.ops.non_max_suppression
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,
end2end=False,
return_idxs=False,
)
Perform non-maximum suppression (NMS) on a set of boxes, with support for masks and multiple labels per box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction
|
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. |
required |
conf_thres
|
float
|
The confidence threshold below which boxes will be filtered out. Valid values are between 0.0 and 1.0. |
0.25
|
iou_thres
|
float
|
The IoU threshold below which boxes will be filtered out during NMS. Valid values are between 0.0 and 1.0. |
0.45
|
classes
|
List[int]
|
A list of class indices to consider. If None, all classes will be considered. |
None
|
agnostic
|
bool
|
If True, the model is agnostic to the number of classes, and all classes will be considered as one. |
False
|
multi_label
|
bool
|
If True, each box may have multiple labels. |
False
|
labels
|
List[List[Union[int, float, 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. |
300
|
nc
|
int
|
The number of classes output by the model. Any indices after this will be considered masks. |
0
|
max_time_img
|
float
|
The maximum time (seconds) for processing one image. |
0.05
|
max_nms
|
int
|
The maximum number of boxes into torchvision.ops.nms(). |
30000
|
max_wh
|
int
|
The maximum box width and height in pixels. |
7680
|
in_place
|
bool
|
If True, the input prediction tensor will be modified in place. |
True
|
rotated
|
bool
|
If Oriented Bounding Boxes (OBB) are being passed for NMS. |
False
|
end2end
|
bool
|
If the model doesn't require NMS. |
False
|
return_idxs
|
bool
|
Return the indices of the detections that were kept. |
False
|
Returns:
Type | Description |
---|---|
List[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, ...). |
Source code in ultralytics/utils/ops.py
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
ultralytics.utils.ops.clip_boxes
clip_boxes(boxes, shape)
Takes a list of bounding boxes and a shape (height, width) and clips the bounding boxes to the shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes
|
Tensor | ndarray
|
The bounding boxes to clip. |
required |
shape
|
tuple
|
The shape of the image. |
required |
Returns:
Type | Description |
---|---|
Tensor | ndarray
|
The clipped boxes. |
Source code in ultralytics/utils/ops.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
|
ultralytics.utils.ops.clip_coords
clip_coords(coords, shape)
Clip line coordinates to the image boundaries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coords
|
Tensor | ndarray
|
A list of line coordinates. |
required |
shape
|
tuple
|
A tuple of integers representing the size of the image in the format (height, width). |
required |
Returns:
Type | Description |
---|---|
Tensor | ndarray
|
Clipped coordinates. |
Source code in ultralytics/utils/ops.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
ultralytics.utils.ops.scale_image
scale_image(masks, im0_shape, ratio_pad=None)
Takes a mask, and resizes it to the original image size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
ndarray
|
Resized and padded masks/images, [h, w, num]/[h, w, 3]. |
required |
im0_shape
|
tuple
|
The original image shape. |
required |
ratio_pad
|
tuple
|
The ratio of the padding to the original image. |
None
|
Returns:
Name | Type | Description |
---|---|---|
masks |
ndarray
|
The masks that are being returned with shape [h, w, num]. |
Source code in ultralytics/utils/ops.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
ultralytics.utils.ops.xyxy2xywh
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input bounding box coordinates in (x1, y1, x2, y2) format. |
required |
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in (x, y, width, height) format. |
Source code in ultralytics/utils/ops.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
ultralytics.utils.ops.xywh2xyxy
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. Note: ops per 2 channels faster than per channel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input bounding box coordinates in (x, y, width, height) format. |
required |
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in (x1, y1, x2, y2) format. |
Source code in ultralytics/utils/ops.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
ultralytics.utils.ops.xywhn2xyxy
xywhn2xyxy(x, w=640, h=640, padw=0, padh=0)
Convert normalized bounding box coordinates to pixel coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The bounding box coordinates. |
required |
w
|
int
|
Width of the image. |
640
|
h
|
int
|
Height of the image. |
640
|
padw
|
int
|
Padding width. |
0
|
padh
|
int
|
Padding height. |
0
|
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | 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. |
Source code in ultralytics/utils/ops.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
ultralytics.utils.ops.xyxy2xywhn
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input bounding box coordinates in (x1, y1, x2, y2) format. |
required |
w
|
int
|
The width of the image. |
640
|
h
|
int
|
The height of the image. |
640
|
clip
|
bool
|
If True, the boxes will be clipped to the image boundaries. |
False
|
eps
|
float
|
The minimum value of the box's width and height. |
0.0
|
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in (x, y, width, height, normalized) format |
Source code in ultralytics/utils/ops.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
ultralytics.utils.ops.xywh2ltwh
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input tensor with the bounding box coordinates in the xywh format |
required |
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in the xyltwh format |
Source code in ultralytics/utils/ops.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|
ultralytics.utils.ops.xyxy2ltwh
xyxy2ltwh(x)
Convert nx4 bounding boxes from [x1, y1, x2, y2] to [x1, y1, w, h], where xy1=top-left, xy2=bottom-right.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input tensor with the bounding boxes coordinates in the xyxy format |
required |
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in the xyltwh format. |
Source code in ultralytics/utils/ops.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
|
ultralytics.utils.ops.ltwh2xywh
ltwh2xywh(x)
Convert nx4 boxes from [x1, y1, w, h] to [x, y, w, h] where xy1=top-left, xy=center.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
the input tensor |
required |
Returns:
Name | Type | Description |
---|---|---|
y |
ndarray | Tensor
|
The bounding box coordinates in the xywh format. |
Source code in ultralytics/utils/ops.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
ultralytics.utils.ops.xyxyxyxy2xywhr
xyxyxyxy2xywhr(x)
Convert batched Oriented Bounding Boxes (OBB) from [xy1, xy2, xy3, xy4] to [xywh, rotation]. Rotation values are returned in radians from 0 to pi/2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
Input box corners [xy1, xy2, xy3, xy4] of shape (n, 8). |
required |
Returns:
Type | Description |
---|---|
ndarray | Tensor
|
Converted data in [cx, cy, w, h, rotation] format of shape (n, 5). |
Source code in ultralytics/utils/ops.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
ultralytics.utils.ops.xywhr2xyxyxyxy
xywhr2xyxyxyxy(x)
Convert batched Oriented Bounding Boxes (OBB) from [xywh, rotation] to [xy1, xy2, xy3, xy4]. Rotation values should be in radians from 0 to pi/2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
Boxes in [cx, cy, w, h, rotation] format of shape (n, 5) or (b, n, 5). |
required |
Returns:
Type | Description |
---|---|
ndarray | Tensor
|
Converted corner points of shape (n, 4, 2) or (b, n, 4, 2). |
Source code in ultralytics/utils/ops.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
ultralytics.utils.ops.ltwh2xyxy
ltwh2xyxy(x)
Convert bounding box from [x1, y1, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray | Tensor
|
The input image. |
required |
Returns:
Type | Description |
---|---|
ndarray | Tensor
|
The xyxy coordinates of the bounding boxes. |
Source code in ultralytics/utils/ops.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
|
ultralytics.utils.ops.segments2boxes
segments2boxes(segments)
Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segments
|
list
|
List of segments, each segment is a list of points, each point is a list of x, y coordinates. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The xywh coordinates of the bounding boxes. |
Source code in ultralytics/utils/ops.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
|
ultralytics.utils.ops.resample_segments
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segments
|
list
|
A list of (n,2) arrays, where n is the number of points in the segment. |
required |
n
|
int
|
Number of points to resample the segment to. |
1000
|
Returns:
Name | Type | Description |
---|---|---|
segments |
list
|
The resampled segments. |
Source code in ultralytics/utils/ops.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
ultralytics.utils.ops.crop_mask
crop_mask(masks, boxes)
Crop masks to bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
Tensor
|
[n, h, w] tensor of masks. |
required |
boxes
|
Tensor
|
[n, 4] tensor of bbox coordinates in relative point form. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Cropped masks. |
Source code in ultralytics/utils/ops.py
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
ultralytics.utils.ops.process_mask
process_mask(protos, masks_in, bboxes, shape, upsample=False)
Apply masks to bounding boxes using the output of the mask head.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protos
|
Tensor
|
A tensor of shape [mask_dim, mask_h, mask_w]. |
required |
masks_in
|
Tensor
|
A tensor of shape [n, mask_dim], where n is the number of masks after NMS. |
required |
bboxes
|
Tensor
|
A tensor of shape [n, 4], where n is the number of masks after NMS. |
required |
shape
|
tuple
|
A tuple of integers representing the size of the input image in the format (h, w). |
required |
upsample
|
bool
|
A flag to indicate whether to upsample the mask to the original image size. |
False
|
Returns:
Type | Description |
---|---|
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. |
Source code in ultralytics/utils/ops.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
|
ultralytics.utils.ops.process_mask_native
process_mask_native(protos, masks_in, bboxes, shape)
Apply masks to bounding boxes using the output of the mask head with native upsampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protos
|
Tensor
|
[mask_dim, mask_h, mask_w]. |
required |
masks_in
|
Tensor
|
[n, mask_dim], n is number of masks after nms. |
required |
bboxes
|
Tensor
|
[n, 4], n is number of masks after nms. |
required |
shape
|
tuple
|
The size of the input image (h,w). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The returned masks with dimensions [h, w, n]. |
Source code in ultralytics/utils/ops.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
|
ultralytics.utils.ops.scale_masks
scale_masks(masks, shape, padding=True)
Rescale segment masks to shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
Tensor
|
(N, C, H, W). |
required |
shape
|
tuple
|
Height and width. |
required |
padding
|
bool
|
If True, assuming the boxes is based on image augmented by yolo style. If False then do regular rescaling. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
Rescaled masks. |
Source code in ultralytics/utils/ops.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
ultralytics.utils.ops.scale_coords
scale_coords(
img1_shape,
coords,
img0_shape,
ratio_pad=None,
normalize=False,
padding=True,
)
Rescale segment coordinates (xy) from img1_shape to img0_shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img1_shape
|
tuple
|
The shape of the image that the coords are from. |
required |
coords
|
Tensor
|
The coords to be scaled of shape n,2. |
required |
img0_shape
|
tuple
|
The shape of the image that the segmentation is being applied to. |
required |
ratio_pad
|
tuple
|
The ratio of the image size to the padded image size. |
None
|
normalize
|
bool
|
If True, the coordinates will be normalized to the range [0, 1]. |
False
|
padding
|
bool
|
If True, assuming the boxes is based on image augmented by yolo style. If False then do regular rescaling. |
True
|
Returns:
Name | Type | Description |
---|---|---|
coords |
Tensor
|
The scaled coordinates. |
Source code in ultralytics/utils/ops.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
|
ultralytics.utils.ops.regularize_rboxes
regularize_rboxes(rboxes)
Regularize rotated boxes in range [0, pi/2].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rboxes
|
Tensor
|
Input boxes of shape(N, 5) in xywhr format. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The regularized boxes. |
Source code in ultralytics/utils/ops.py
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
|
ultralytics.utils.ops.masks2segments
masks2segments(masks, strategy='all')
Convert masks to segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
Tensor
|
The output of the model, which is a tensor of shape (batch_size, 160, 160). |
required |
strategy
|
str
|
'all' or 'largest'. |
'all'
|
Returns:
Type | Description |
---|---|
list
|
List of segment masks. |
Source code in ultralytics/utils/ops.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
|
ultralytics.utils.ops.convert_torch2numpy_batch
convert_torch2numpy_batch(batch: 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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Tensor
|
Input tensor batch of shape (Batch, Channels, Height, Width) and dtype torch.float32. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Output NumPy array batch of shape (Batch, Height, Width, Channels) and dtype uint8. |
Source code in ultralytics/utils/ops.py
855 856 857 858 859 860 861 862 863 864 865 |
|
ultralytics.utils.ops.clean_str
clean_str(s)
Cleans a string by replacing special characters with '_' character.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
A string needing special characters replaced. |
required |
Returns:
Type | Description |
---|---|
str
|
A string with special characters replaced by an underscore _. |
Source code in ultralytics/utils/ops.py
868 869 870 871 872 873 874 875 876 877 878 |
|
ultralytics.utils.ops.empty_like
empty_like(x)
Creates empty torch.Tensor or np.ndarray with same shape as input and float32 dtype.
Source code in ultralytics/utils/ops.py
881 882 883 884 885 |
|