Reference for ultralytics/data/utils.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/utils.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.utils.HUBDatasetStats
HUBDatasetStats(path='coco8.yaml', task='detect', autodownload=False)
A class for generating HUB dataset JSON and -hub
dataset directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to data.yaml or data.zip (with data.yaml inside data.zip). Default is 'coco8.yaml'. |
'coco8.yaml'
|
task
|
str
|
Dataset task. Options are 'detect', 'segment', 'pose', 'classify'. Default is 'detect'. |
'detect'
|
autodownload
|
bool
|
Attempt to download dataset if not found locally. Default is False. |
False
|
Note
Download *.zip files from https://github.com/ultralytics/hub/tree/main/example_datasets i.e. https://github.com/ultralytics/hub/raw/main/example_datasets/coco8.zip for coco8.zip.
Examples:
>>> from ultralytics.data.utils import HUBDatasetStats
>>> stats = HUBDatasetStats("path/to/coco8.zip", task="detect") # detect dataset
>>> stats = HUBDatasetStats("path/to/coco8-seg.zip", task="segment") # segment dataset
>>> stats = HUBDatasetStats("path/to/coco8-pose.zip", task="pose") # pose dataset
>>> stats = HUBDatasetStats("path/to/dota8.zip", task="obb") # OBB dataset
>>> stats = HUBDatasetStats("path/to/imagenet10.zip", task="classify") # classification dataset
>>> stats.get_json(save=True)
>>> stats.process_images()
Source code in ultralytics/data/utils.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
|
get_json
get_json(save=False, verbose=False)
Return dataset JSON for Ultralytics HUB.
Source code in ultralytics/data/utils.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
process_images
process_images()
Compress images for Ultralytics HUB.
Source code in ultralytics/data/utils.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
|
ultralytics.data.utils.img2label_paths
img2label_paths(img_paths)
Define label paths as a function of image paths.
Source code in ultralytics/data/utils.py
44 45 46 47 |
|
ultralytics.data.utils.check_file_speeds
check_file_speeds(
files, threshold_ms=10, threshold_mb=50, max_files=5, prefix=""
)
Check dataset file access speed and provide performance feedback.
This function tests the access speed of dataset files by measuring ping (stat call) time and read speed. It samples up to 5 files from the provided list and warns if access times exceed the threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files
|
list
|
List of file paths to check for access speed. |
required |
threshold_ms
|
float
|
Threshold in milliseconds for ping time warnings. |
10
|
threshold_mb
|
float
|
Threshold in megabytes per second for read speed warnings. |
50
|
max_files
|
int
|
The maximum number of files to check. |
5
|
prefix
|
str
|
Prefix string to add to log messages. |
''
|
Examples:
>>> from pathlib import Path
>>> image_files = list(Path("dataset/images").glob("*.jpg"))
>>> check_file_speeds(image_files, threshold_ms=15)
Source code in ultralytics/data/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 91 92 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 |
|
ultralytics.data.utils.get_hash
get_hash(paths)
Returns a single hash value of a list of paths (files or dirs).
Source code in ultralytics/data/utils.py
126 127 128 129 130 131 132 133 134 135 136 |
|
ultralytics.data.utils.exif_size
exif_size(img: Image)
Returns exif-corrected PIL size.
Source code in ultralytics/data/utils.py
139 140 141 142 143 144 145 146 147 148 149 150 |
|
ultralytics.data.utils.verify_image
verify_image(args)
Verify one image.
Source code in ultralytics/data/utils.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
ultralytics.data.utils.verify_image_label
verify_image_label(args)
Verify one image-label pair.
Source code in ultralytics/data/utils.py
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 |
|
ultralytics.data.utils.visualize_image_annotations
visualize_image_annotations(image_path, txt_path, label_map)
Visualizes YOLO annotations (bounding boxes and class labels) on an image.
This function reads an image and its corresponding annotation file in YOLO format, then draws bounding boxes around detected objects and labels them with their respective class names. The bounding box colors are assigned based on the class ID, and the text color is dynamically adjusted for readability, depending on the background color's luminance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
The path to the image file to annotate, and it can be in formats supported by PIL. |
required |
txt_path
|
str
|
The path to the annotation file in YOLO format, that should contain one line per object. |
required |
label_map
|
dict
|
A dictionary that maps class IDs (integers) to class labels (strings). |
required |
Examples:
>>> label_map = {0: "cat", 1: "dog", 2: "bird"} # It should include all annotated classes details
>>> visualize_image_annotations("path/to/image.jpg", "path/to/annotations.txt", label_map)
Source code in ultralytics/data/utils.py
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 |
|
ultralytics.data.utils.polygon2mask
polygon2mask(imgsz, polygons, color=1, downsample_ratio=1)
Convert a list of polygons to a binary mask of the specified image size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
imgsz
|
tuple
|
The size of the image as (height, width). |
required |
polygons
|
list[ndarray]
|
A list of polygons. Each polygon is an array with shape [N, M], where N is the number of polygons, and M is the number of points such that M % 2 = 0. |
required |
color
|
int
|
The color value to fill in the polygons on the mask. |
1
|
downsample_ratio
|
int
|
Factor by which to downsample the mask. |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
A binary mask of the specified image size with the polygons filled in. |
Source code in ultralytics/data/utils.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
ultralytics.data.utils.polygons2masks
polygons2masks(imgsz, polygons, color, downsample_ratio=1)
Convert a list of polygons to a set of binary masks of the specified image size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
imgsz
|
tuple
|
The size of the image as (height, width). |
required |
polygons
|
list[ndarray]
|
A list of polygons. Each polygon is an array with shape [N, M], where N is the number of polygons, and M is the number of points such that M % 2 = 0. |
required |
color
|
int
|
The color value to fill in the polygons on the masks. |
required |
downsample_ratio
|
int
|
Factor by which to downsample each mask. |
1
|
Returns:
Type | Description |
---|---|
ndarray
|
A set of binary masks of the specified image size with the polygons filled in. |
Source code in ultralytics/data/utils.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
ultralytics.data.utils.polygons2masks_overlap
polygons2masks_overlap(imgsz, segments, downsample_ratio=1)
Return a (640, 640) overlap mask.
Source code in ultralytics/data/utils.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
ultralytics.data.utils.find_dataset_yaml
find_dataset_yaml(path: Path) -> Path
Find and return the YAML file associated with a Detect, Segment or Pose dataset.
This function searches for a YAML file at the root level of the provided directory first, and if not found, it performs a recursive search. It prefers YAML files that have the same stem as the provided path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
The directory path to search for the YAML file. |
required |
Returns:
Type | Description |
---|---|
Path
|
The path of the found YAML file. |
Source code in ultralytics/data/utils.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
|
ultralytics.data.utils.check_det_dataset
check_det_dataset(dataset, autodownload=True)
Download, verify, and/or unzip a dataset if not found locally.
This function checks the availability of a specified dataset, and if not found, it has the option to download and unzip the dataset. It then reads and parses the accompanying YAML data, ensuring key requirements are met and also resolves paths related to the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
str
|
Path to the dataset or dataset descriptor (like a YAML file). |
required |
autodownload
|
bool
|
Whether to automatically download the dataset if not found. |
True
|
Returns:
Type | Description |
---|---|
dict
|
Parsed dataset information and paths. |
Source code in ultralytics/data/utils.py
378 379 380 381 382 383 384 385 386 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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
|
ultralytics.data.utils.check_cls_dataset
check_cls_dataset(dataset, split='')
Checks a classification dataset such as Imagenet.
This function accepts a dataset
name and attempts to retrieve the corresponding dataset information.
If the dataset is not found locally, it attempts to download the dataset from the internet and save it locally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
str | Path
|
The name of the dataset. |
required |
split
|
str
|
The split of the dataset. Either 'val', 'test', or ''. |
''
|
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the following keys:
|
Source code in ultralytics/data/utils.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
|
ultralytics.data.utils.compress_one_image
compress_one_image(f, f_new=None, max_dim=1920, quality=50)
Compresses a single image file to reduced size while preserving its aspect ratio and quality using either the Python Imaging Library (PIL) or OpenCV library. If the input image is smaller than the maximum dimension, it will not be resized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f
|
str
|
The path to the input image file. |
required |
f_new
|
str
|
The path to the output image file. If not specified, the input file will be overwritten. |
None
|
max_dim
|
int
|
The maximum dimension (width or height) of the output image. |
1920
|
quality
|
int
|
The image compression quality as a percentage. |
50
|
Examples:
>>> from pathlib import Path
>>> from ultralytics.data.utils import compress_one_image
>>> for f in Path("path/to/dataset").rglob("*.jpg"):
>>> compress_one_image(f)
Source code in ultralytics/data/utils.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
ultralytics.data.utils.load_dataset_cache_file
load_dataset_cache_file(path)
Load an Ultralytics *.cache dictionary from path.
Source code in ultralytics/data/utils.py
755 756 757 758 759 760 761 762 |
|
ultralytics.data.utils.save_dataset_cache_file
save_dataset_cache_file(prefix, path, x, version)
Save an Ultralytics dataset *.cache dictionary x to path.
Source code in ultralytics/data/utils.py
765 766 767 768 769 770 771 772 773 774 775 |
|