Reference for ultralytics/data/dataset.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/dataset.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.dataset.YOLODataset
Bases: BaseDataset
Dataset class for loading object detection and/or segmentation labels in YOLO format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
A dataset YAML dictionary. Defaults to None. |
None
|
task |
str
|
An explicit arg to point current task, Defaults to 'detect'. |
'detect'
|
Returns:
Type | Description |
---|---|
Dataset
|
A PyTorch dataset object that can be used for training an object detection model. |
Source code in ultralytics/data/dataset.py
45 46 47 48 49 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 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 |
|
__init__(*args, data=None, task='detect', **kwargs)
Initializes the YOLODataset with optional configurations for segments and keypoints.
Source code in ultralytics/data/dataset.py
build_transforms(hyp=None)
Builds and appends transforms to the list.
Source code in ultralytics/data/dataset.py
cache_labels(path=Path('./labels.cache'))
Cache dataset labels, check images and read shapes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
Path where to save the cache file. Default is Path('./labels.cache'). |
Path('./labels.cache')
|
Returns:
Type | Description |
---|---|
dict
|
labels. |
Source code in ultralytics/data/dataset.py
close_mosaic(hyp)
Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.
Source code in ultralytics/data/dataset.py
collate_fn(batch)
staticmethod
Collates data samples into batches.
Source code in ultralytics/data/dataset.py
get_labels()
Returns dictionary of labels for YOLO training.
Source code in ultralytics/data/dataset.py
update_labels_info(label)
Custom your label format here.
Note
cls is not with bboxes now, classification and semantic segmentation need an independent cls label Can also support classification and semantic segmentation by adding or removing dict keys there.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.YOLOMultiModalDataset
Bases: YOLODataset
Dataset class for loading object detection and/or segmentation labels in YOLO format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
A dataset YAML dictionary. Defaults to None. |
None
|
task |
str
|
An explicit arg to point current task, Defaults to 'detect'. |
'detect'
|
Returns:
Type | Description |
---|---|
Dataset
|
A PyTorch dataset object that can be used for training an object detection model. |
Source code in ultralytics/data/dataset.py
__init__(*args, data=None, task='detect', **kwargs)
Initializes a dataset object for object detection tasks with optional specifications.
build_transforms(hyp=None)
Enhances data transformations with optional text augmentation for multi-modal training.
Source code in ultralytics/data/dataset.py
update_labels_info(label)
Add texts information for multi modal model training.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.GroundingDataset
Bases: YOLODataset
Source code in ultralytics/data/dataset.py
__init__(*args, task='detect', json_file, **kwargs)
Initializes a GroundingDataset for object detection, loading annotations from a specified JSON file.
Source code in ultralytics/data/dataset.py
build_transforms(hyp=None)
Configures augmentations for training with optional text loading; hyp
adjusts augmentation intensity.
Source code in ultralytics/data/dataset.py
get_img_files(img_path)
get_labels()
Loads annotations from a JSON file, filters, and normalizes bounding boxes for each image.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.YOLOConcatDataset
Bases: ConcatDataset
Dataset as a concatenation of multiple datasets.
This class is useful to assemble different existing datasets.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.SemanticDataset
Bases: BaseDataset
Semantic Segmentation Dataset.
This class is responsible for handling datasets used for semantic segmentation tasks. It inherits functionalities from the BaseDataset class.
Note
This class is currently a placeholder and needs to be populated with methods and attributes for supporting semantic segmentation tasks.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.ClassificationDataset
Extends torchvision ImageFolder to support YOLO classification tasks, offering functionalities like image augmentation, caching, and verification. It's designed to efficiently handle large datasets for training deep learning models, with optional image transformations and caching mechanisms to speed up training.
This class allows for augmentations using both torchvision and Albumentations libraries, and supports caching images in RAM or on disk to reduce IO overhead during training. Additionally, it implements a robust verification process to ensure data integrity and consistency.
Attributes:
Name | Type | Description |
---|---|---|
cache_ram |
bool
|
Indicates if caching in RAM is enabled. |
cache_disk |
bool
|
Indicates if caching on disk is enabled. |
samples |
list
|
A list of tuples, each containing the path to an image, its class index, path to its .npy cache file (if caching on disk), and optionally the loaded image array (if caching in RAM). |
torch_transforms |
callable
|
PyTorch transforms to be applied to the images. |
Source code in ultralytics/data/dataset.py
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 471 472 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 |
|
__getitem__(i)
Returns subset of data and targets corresponding to given indices.
Source code in ultralytics/data/dataset.py
__init__(root, args, augment=False, prefix='')
Initialize YOLO object with root, image size, augmentations, and cache settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root |
str
|
Path to the dataset directory where images are stored in a class-specific folder structure. |
required |
args |
Namespace
|
Configuration containing dataset-related settings such as image size, augmentation
parameters, and cache settings. It includes attributes like |
required |
augment |
bool
|
Whether to apply augmentations to the dataset. Default is False. |
False
|
prefix |
str
|
Prefix for logging and cache filenames, aiding in dataset identification and debugging. Default is an empty string. |
''
|
Source code in ultralytics/data/dataset.py
__len__()
verify_images()
Verify all images in dataset.