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
YOLOETrainer(
cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None
)
Bases: DetectionTrainer
A trainer class for YOLOE object detection models.
This class extends DetectionTrainer to provide specialized training functionality for YOLOE models, including custom model initialization, validation, and dataset building with multi-modal support.
Attributes:
Name | Type | Description |
---|---|---|
loss_names |
tuple
|
Names of loss components used during training. |
Methods:
Name | Description |
---|---|
get_model |
Initialize and return a YOLOEModel with specified configuration. |
get_validator |
Return a YOLOEDetectValidator for model validation. |
build_dataset |
Build YOLO dataset with multi-modal support for 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
build_dataset
build_dataset(img_path: str, mode: str = 'train', batch: Optional[int] = None)
Build YOLO Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the folder containing images. |
required |
mode
|
str
|
'train' mode or 'val' mode, users are able to customize different augmentations for each mode. |
'train'
|
batch
|
int
|
Size of batches, this is for rectangular training. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
YOLO dataset configured for training or validation. |
Source code in ultralytics/models/yolo/yoloe/train.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
get_model
get_model(cfg=None, weights=None, verbose: bool = True)
Return a YOLOEModel initialized with the specified configuration and weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
dict | str
|
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
|
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
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 |
|
get_validator
get_validator()
Return a YOLOEDetectValidator for YOLOE model validation.
Source code in ultralytics/models/yolo/yoloe/train.py
85 86 87 88 89 90 |
|
ultralytics.models.yolo.yoloe.train.YOLOEPETrainer
YOLOEPETrainer(cfg=DEFAULT_CFG, overrides=None, _callbacks=None)
Bases: DetectionTrainer
Fine-tune YOLOE model using linear probing approach.
This trainer freezes most model layers and only trains specific projection layers for efficient fine-tuning on new datasets while preserving pretrained features.
Methods:
Name | Description |
---|---|
get_model |
Initialize YOLOEModel with frozen layers except projection layers. |
Source code in ultralytics/engine/trainer.py
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 |
|
get_model
get_model(cfg=None, weights=None, verbose: bool = True)
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
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 |
|
ultralytics.models.yolo.yoloe.train.YOLOETrainerFromScratch
YOLOETrainerFromScratch(
cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None
)
Bases: YOLOETrainer
, WorldTrainerFromScratch
Train YOLOE models from scratch with text embedding support.
This trainer combines YOLOE training capabilities with world training features, enabling training from scratch with text embeddings and grounding datasets.
Methods:
Name | Description |
---|---|
build_dataset |
Build datasets for training with grounding support. |
preprocess_batch |
Process batches with text features. |
generate_text_embeddings |
Generate and cache text embeddings for training. |
Source code in ultralytics/models/yolo/yoloe/train.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
build_dataset
build_dataset(
img_path: Union[List[str], str],
mode: str = "train",
batch: Optional[int] = None,
)
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
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
generate_text_embeddings
generate_text_embeddings(texts: List[str], batch: int, cache_dir: Path)
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_dir
|
Path
|
Directory to save/load cached embeddings. |
required |
Returns:
Type | Description |
---|---|
dict
|
Dictionary mapping text samples to their embeddings. |
Source code in ultralytics/models/yolo/yoloe/train.py
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 |
|
preprocess_batch
preprocess_batch(batch)
Process batch for training, moving text features to the appropriate device.
Source code in ultralytics/models/yolo/yoloe/train.py
194 195 196 197 198 199 200 201 202 |
|
ultralytics.models.yolo.yoloe.train.YOLOEPEFreeTrainer
YOLOEPEFreeTrainer(
cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None
)
Bases: YOLOEPETrainer
, YOLOETrainerFromScratch
Train prompt-free YOLOE model.
This trainer combines linear probing capabilities with from-scratch training for prompt-free YOLOE models that don't require text prompts during inference.
Methods:
Name | Description |
---|---|
get_validator |
Return standard DetectionValidator for validation. |
preprocess_batch |
Preprocess batches without text features. |
set_text_embeddings |
Set text embeddings for datasets (no-op for prompt-free). |
Source code in ultralytics/models/yolo/yoloe/train.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
get_validator
get_validator()
Return a DetectionValidator for YOLO model validation.
Source code in ultralytics/models/yolo/yoloe/train.py
244 245 246 247 248 249 |
|
preprocess_batch
preprocess_batch(batch)
Preprocess a batch of images for YOLOE training, adjusting formatting and dimensions as needed.
Source code in ultralytics/models/yolo/yoloe/train.py
251 252 253 254 |
|
set_text_embeddings
set_text_embeddings(datasets, batch: int)
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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
ultralytics.models.yolo.yoloe.train.YOLOEVPTrainer
YOLOEVPTrainer(
cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None
)
Bases: YOLOETrainerFromScratch
Train YOLOE model with visual prompts.
This trainer extends YOLOETrainerFromScratch to support visual prompt-based training, where visual cues are provided alongside images to guide the detection process.
Methods:
Name | Description |
---|---|
build_dataset |
Build dataset with visual prompt loading transforms. |
preprocess_batch |
Preprocess batches with visual prompts. |
Source code in ultralytics/models/yolo/yoloe/train.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
build_dataset
build_dataset(
img_path: Union[List[str], str],
mode: str = "train",
batch: Optional[int] = None,
)
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
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
preprocess_batch
preprocess_batch(batch)
Preprocess a batch of images for YOLOE training, moving visual prompts to the appropriate device.
Source code in ultralytics/models/yolo/yoloe/train.py
317 318 319 320 321 |
|