Reference for ultralytics/models/yolo/yoloe/train.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/yoloe/train.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.yoloe.train.YOLOETrainer
Bases: DetectionTrainer
A base trainer for YOLOE training.
This method sets up the YOLOE trainer with the provided configuration and overrides, initializing the training environment, model, and callbacks for YOLOE object detection training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict
|
Configuration dictionary with default training settings from DEFAULT_CFG. |
DEFAULT_CFG
|
overrides
|
dict
|
Dictionary of parameter overrides for the default configuration. |
None
|
_callbacks
|
list
|
List of callback functions to be applied during training. |
None
|
Source code in ultralytics/models/yolo/yoloe/train.py
build_dataset
Build YOLO Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the folder containing images. |
required |
mode
|
str
|
|
'train'
|
batch
|
int
|
Size of batches, this is for |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
YOLO dataset configured for training or validation. |
Source code in ultralytics/models/yolo/yoloe/train.py
get_model
Return a YOLOEModel initialized with the specified configuration and weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict | str | None
|
Model configuration. Can be a dictionary containing a 'yaml_file' key, a direct path to a YAML file, or None to use default configuration. |
None
|
weights
|
str | Path | None
|
Path to pretrained weights file to load into the model. |
None
|
verbose
|
bool
|
Whether to display model information during initialization. |
True
|
Returns:
Type | Description |
---|---|
YOLOEModel
|
The initialized YOLOE model. |
Notes
- The number of classes (nc) is hard-coded to a maximum of 80 following the official configuration.
- The nc parameter here represents the maximum number of different text samples in one image, rather than the actual number of classes.
Source code in ultralytics/models/yolo/yoloe/train.py
get_validator
Returns a DetectionValidator for YOLO model validation.
Source code in ultralytics/models/yolo/yoloe/train.py
preprocess_batch
Process batch for training, moving text features to the appropriate device.
ultralytics.models.yolo.yoloe.train.YOLOEPETrainer
Bases: DetectionTrainer
Fine-tune YOLOE model in linear probing way.
Source code in ultralytics/engine/trainer.py
get_model
Return YOLOEModel initialized with specified config and weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict | str
|
Model configuration. |
None
|
weights
|
str
|
Path to pretrained weights. |
None
|
verbose
|
bool
|
Whether to display model information. |
True
|
Returns:
Type | Description |
---|---|
YOLOEModel
|
Initialized model with frozen layers except for specific projection layers. |
Source code in ultralytics/models/yolo/yoloe/train.py
ultralytics.models.yolo.yoloe.train.YOLOETrainerFromScratch
Bases: YOLOETrainer
Train YOLOE models from scratch.
This class extends YOLOETrainer to train YOLOE models from scratch. It inherits all functionality from the parent class while providing specialized initialization for training without pre-trained weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict
|
Configuration dictionary with training parameters. Defaults to DEFAULT_CFG. |
DEFAULT_CFG
|
overrides
|
dict
|
Dictionary of parameter overrides for configuration. |
None
|
_callbacks
|
list
|
List of callback functions to be executed during training. |
None
|
Examples:
>>> from ultralytics.models.yoloe.train import YOLOETrainerFromScratch
>>> trainer = YOLOETrainerFromScratch()
>>> trainer.train()
Source code in ultralytics/models/yolo/yoloe/train.py
build_dataset
Build YOLO Dataset for training or validation.
This method constructs appropriate datasets based on the mode and input paths, handling both standard YOLO datasets and grounding datasets with different formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
List[str] | str
|
Path to the folder containing images or list of paths. |
required |
mode
|
str
|
'train' mode or 'val' mode, allowing customized augmentations for each mode. |
'train'
|
batch
|
int
|
Size of batches, used for rectangular training/validation. |
None
|
Returns:
Type | Description |
---|---|
YOLOConcatDataset | Dataset
|
The constructed dataset for training or validation. |
Source code in ultralytics/models/yolo/yoloe/train.py
final_eval
Perform final evaluation on the validation dataset.
Configures the validator with the appropriate dataset and split before running evaluation.
Returns:
Type | Description |
---|---|
dict
|
Evaluation metrics. |
Source code in ultralytics/models/yolo/yoloe/train.py
generate_text_embeddings
Generate text embeddings for a list of text samples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
List[str]
|
List of text samples to encode. |
required |
batch
|
int
|
Batch size for processing. |
required |
cache_path
|
str | Path
|
Path to save/load cached embeddings. |
'embeddings.pt'
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary mapping text samples to their embeddings. |
Source code in ultralytics/models/yolo/yoloe/train.py
get_dataset
Get train and validation paths from data dictionary.
Processes the data configuration to extract paths for training and validation datasets, handling both YOLO detection datasets and grounding datasets.
Returns:
Type | Description |
---|---|
str
|
Train dataset path. |
str
|
Validation dataset path. |
Raises:
Type | Description |
---|---|
AssertionError
|
If train or validation datasets are not found, or if validation has multiple datasets. |
Source code in ultralytics/models/yolo/yoloe/train.py
plot_training_labels
preprocess_batch
Process batch for training, moving text features to the appropriate device.
Source code in ultralytics/models/yolo/yoloe/train.py
set_text_embeddings
Set text embeddings for datasets to accelerate training by caching category names.
This method collects unique category names from all datasets, then generates and caches text embeddings for these categories to improve training efficiency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datasets
|
List[Dataset]
|
List of datasets from which to extract category names. |
required |
batch
|
int | None
|
Batch size used for processing. |
required |
Notes
This method collects category names from datasets that have the 'category_names' attribute, then uses the first dataset's image path to determine where to cache the generated text embeddings.
Source code in ultralytics/models/yolo/yoloe/train.py
ultralytics.models.yolo.yoloe.train.YOLOEPEFreeTrainer
Bases: YOLOEPETrainer
, YOLOETrainerFromScratch
Train prompt-free YOLOE model.
Source code in ultralytics/models/yolo/yoloe/train.py
get_validator
Returns a DetectionValidator for YOLO model validation.
Source code in ultralytics/models/yolo/yoloe/train.py
preprocess_batch
Preprocesses a batch of images for YOLOE training, adjusting formatting and dimensions as needed.
set_text_embeddings
Set text embeddings for datasets to accelerate training by caching category names.
This method collects unique category names from all datasets, generates text embeddings for them, and caches these embeddings to improve training efficiency. The embeddings are stored in a file in the parent directory of the first dataset's image path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datasets
|
List[Dataset]
|
List of datasets containing category names to process. |
required |
batch
|
int
|
Batch size for processing text embeddings. |
required |
Notes
The method creates a dictionary mapping text samples to their embeddings and stores it at the path specified by 'cache_path'. If the cache file already exists, it will be loaded instead of regenerating the embeddings.
Source code in ultralytics/models/yolo/yoloe/train.py
ultralytics.models.yolo.yoloe.train.YOLOEVPTrainer
Bases: YOLOETrainerFromScratch
Train YOLOE model with visual prompts.
Source code in ultralytics/models/yolo/yoloe/train.py
build_dataset
Build YOLO Dataset for training or validation with visual prompts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
List[str] | str
|
Path to the folder containing images or list of paths. |
required |
mode
|
str
|
'train' mode or 'val' mode, allowing customized augmentations for each mode. |
'train'
|
batch
|
int
|
Size of batches, used for rectangular training/validation. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
YOLO dataset configured for training or validation, with visual prompts for training mode. |
Source code in ultralytics/models/yolo/yoloe/train.py
preprocess_batch
Preprocesses a batch of images for YOLOE training, moving visual prompts to the appropriate device.