Training¶
Solver¶
yolo.tasks.detection.solver
¶
Config
dataclass
¶
Source code in yolo/config/config.py
PostProcess
¶
TODO: function document scale back the prediction and do nms for pred_bbox
Source code in yolo/utils/model_utils.py
BaseModel
¶
Bases: LightningModule
Source code in yolo/tasks/detection/solver.py
DetectionValidateModel
¶
Bases: BaseModel
Source code in yolo/tasks/detection/solver.py
DetectionTrainModel
¶
Bases: DetectionValidateModel
Source code in yolo/tasks/detection/solver.py
DetectionInferenceModel
¶
Bases: BaseModel
Source code in yolo/tasks/detection/solver.py
create_dataloader(data_cfg, dataset_cfg, task='train')
¶
Source code in yolo/data/loader.py
create_model(model_cfg, weight_path=True, class_num=80)
¶
Constructs and returns a YOLO model from a model config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
ModelConfig
|
The model configuration (architecture definition). |
required |
weight_path
|
Union[bool, Path]
|
Path to pretrained weights. |
True
|
class_num
|
int
|
Number of output classes. |
80
|
Returns:
| Name | Type | Description |
|---|---|---|
YOLO |
YOLO
|
An instance of the model defined by the given configuration. |
Source code in yolo/model/builder.py
create_loss_function(cfg, vec2box)
¶
Source code in yolo/tasks/detection/loss.py
create_converter(model_version='v9-c', *args, **kwargs)
¶
Source code in yolo/tasks/detection/postprocess.py
to_metrics_format(prediction)
¶
Source code in yolo/tasks/detection/postprocess.py
register(task_type, mode)
¶
Decorator that registers a solver class for a (task_type, mode) pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_type
|
str
|
Task name, e.g. |
required |
mode
|
str
|
Run mode — |
required |
Source code in yolo/tasks/registry.py
create_optimizer(model, optim_cfg)
¶
Create an optimizer for the given model parameters based on the configuration.
Returns:
| Type | Description |
|---|---|
Optimizer
|
An instance of the optimizer configured according to the provided settings. |
Source code in yolo/training/optim.py
create_scheduler(optimizer, schedule_cfg, steps_per_epoch=None, epochs=None)
¶
Create a learning rate scheduler for the given optimizer based on the configuration.
Returns:
| Type | Description |
|---|---|
_LRScheduler
|
An instance of the scheduler configured according to the provided settings. |
Source code in yolo/training/optim.py
draw_bboxes(img, bboxes, *, idx2label=None)
¶
Draw bounding boxes on an image.
Args: - img (PIL Image or torch.Tensor): Image on which to draw the bounding boxes. - bboxes (List of Lists/Tensors): Bounding boxes with [class_id, x_min, y_min, x_max, y_max], where coordinates are normalized [0, 1].
Source code in yolo/utils/drawer.py
Optimizer & Scheduler¶
yolo.training.optim
¶
OptimizerConfig
dataclass
¶
SchedulerConfig
dataclass
¶
YOLO
¶
Bases: Module
A preliminary YOLO (You Only Look Once) model class still under development.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
ModelConfig
|
Configuration for the YOLO model. Expected to define the layers, parameters, and any other relevant configuration details. |
required |
Source code in yolo/model/builder.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
save_load_weights(weights)
¶
Update the model's weights with the provided weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Union[Path, OrderedDict]
|
A OrderedDict containing the new weights. |
required |
Source code in yolo/model/builder.py
WarmupLRPolicy
¶
Base strategy for per-group LR shape during warmup.
Subclass this to define custom warmup curves without touching
WarmupBatchScheduler (Open/Closed Principle).
Source code in yolo/training/optim.py
start_lr(group_idx, initial_lr)
¶
LinearWarmupPolicy
¶
Bases: WarmupLRPolicy
Uniform ramp: all param groups rise from 0 → initial_lr over warmup.
Source code in yolo/training/optim.py
YOLOWarmupPolicy
¶
Bases: WarmupLRPolicy
YOLO-style warmup: bias group drops, all other groups rise.
Group 0 (bias): starts at 10× initial_lr and ramps down to 1×. Groups 1+ (conv, bn): start at 0 and ramp up to 1×.
This mirrors the original lambda2/lambda1 scheme from YOLO.
Source code in yolo/training/optim.py
WarmupBatchScheduler
¶
Bases: _LRScheduler
Batch-level LR and momentum scheduler with epoch-aware warmup.
Wraps an epoch-level scheduler and linearly interpolates LR
across batches within each epoch. Momentum is also interpolated during
warmup epochs.
Must be called with interval="step" in Lightning so that
scheduler.step() fires once per optimizer step.
Source code in yolo/training/optim.py
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 | |
lerp(start, end, step, total=1)
¶
Linearly interpolates between start and end values.
start * (1 - step) + end * step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
The starting value. |
required |
end
|
float
|
The ending value. |
required |
step
|
int
|
The current step in the interpolation process. |
required |
total
|
int
|
The total number of steps. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The interpolated value. |
Source code in yolo/training/optim.py
create_optimizer(model, optim_cfg)
¶
Create an optimizer for the given model parameters based on the configuration.
Returns:
| Type | Description |
|---|---|
Optimizer
|
An instance of the optimizer configured according to the provided settings. |
Source code in yolo/training/optim.py
create_scheduler(optimizer, schedule_cfg, steps_per_epoch=None, epochs=None)
¶
Create a learning rate scheduler for the given optimizer based on the configuration.
Returns:
| Type | Description |
|---|---|
_LRScheduler
|
An instance of the scheduler configured according to the provided settings. |
Source code in yolo/training/optim.py
Callbacks¶
yolo.training.callbacks
¶
logger = logging.getLogger('yolo')
module-attribute
¶
DataConfig
dataclass
¶
Source code in yolo/config/schemas/data.py
SchedulerConfig
dataclass
¶
EMA
¶
Bases: Callback
Exponential Moving Average of model weights as a Lightning Callback.
Keeps a shadow copy of model parameters smoothed over training steps
beta = decay * (1 - exp(-step / tau)) shadow = beta * shadow + (1 - beta) * model
The tau warmup ramps beta up from ~0 at step 0, so early noisy updates don't dominate the shadow. Validation always runs on shadow weights; training weights are swapped back immediately after.
Source code in yolo/training/callbacks.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
setup(trainer, pl_module, stage)
¶
Initialise shadow from the model before training begins.
Source code in yolo/training/callbacks.py
update(pl_module)
¶
Blend model parameters into shadow; copy buffers directly.
Source code in yolo/training/callbacks.py
apply_shadow(pl_module)
¶
Snapshot training weights then load shadow weights into the model.
Source code in yolo/training/callbacks.py
restore(pl_module)
¶
Reload the training-weight snapshot, discarding the shadow swap.
Source code in yolo/training/callbacks.py
GradientAccumulation
¶
Bases: Callback
Source code in yolo/training/callbacks.py
lerp(start, end, step, total=1)
¶
Linearly interpolates between start and end values.
start * (1 - step) + end * step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
float
|
The starting value. |
required |
end
|
float
|
The ending value. |
required |
step
|
int
|
The current step in the interpolation process. |
required |
total
|
int
|
The total number of steps. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The interpolated value. |