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
build_transforms
Builds and appends transforms to the list.
Source code in ultralytics/data/dataset.py
cache_labels
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
Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.
Source code in ultralytics/data/dataset.py
collate_fn
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
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
build_transforms
Enhances data transformations with optional text augmentation for multi-modal training.
Source code in ultralytics/data/dataset.py
update_labels_info
Add texts information for multi-modal model training.
Source code in ultralytics/data/dataset.py
ultralytics.data.dataset.GroundingDataset
Bases: YOLODataset
Handles object detection tasks by loading annotations from a specified JSON file, supporting YOLO format.
Source code in ultralytics/data/dataset.py
build_transforms
Configures augmentations for training with optional text loading; hyp
adjusts augmentation intensity.
Source code in ultralytics/data/dataset.py
get_img_files
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.
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. |
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
__getitem__
Returns subset of data and targets corresponding to given indices.
Source code in ultralytics/data/dataset.py
__len__
verify_images
Verify all images in dataset.