trainer
birdnet_stm32.training.trainer
¶
Training loop with cosine LR schedule, early stopping, and checkpointing.
train_model(model, train_dataset, val_dataset, epochs=50, learning_rate=0.001, batch_size=64, patience=10, checkpoint_path='checkpoints/best_model.keras', steps_per_epoch=None, val_steps=None, is_multilabel=False, optimizer='adam', weight_decay=0.0, loss_fn=None, gradient_clip_norm=0.0, class_weights=None, resume=False, extra_callbacks=None)
¶
Train a model with cosine LR schedule, early stopping, and checkpointing.
Monitors val_loss (min). Best model is saved as a full .keras file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Model to train. |
required |
train_dataset
|
Dataset
|
Training dataset (infinite). |
required |
val_dataset
|
Dataset
|
Validation dataset (infinite). |
required |
epochs
|
int
|
Number of epochs. |
50
|
learning_rate
|
float
|
Initial learning rate for cosine schedule. |
0.001
|
batch_size
|
int
|
Unused; kept for API symmetry with data loader. |
64
|
patience
|
int
|
Early stopping patience (epochs). |
10
|
checkpoint_path
|
str
|
Path to save the best .keras model. |
'checkpoints/best_model.keras'
|
steps_per_epoch
|
int | None
|
Training steps per epoch (> 0 required). |
None
|
val_steps
|
int | None
|
Validation steps per epoch (defaults to 1 if <= 0). |
None
|
is_multilabel
|
bool
|
If True, uses binary_crossentropy; else categorical_crossentropy. |
False
|
optimizer
|
str
|
Optimizer name ('adam', 'sgd', or 'adamw'). |
'adam'
|
weight_decay
|
float
|
Weight decay factor (only used by 'adamw'). |
0.0
|
loss_fn
|
str | Loss | None
|
Optional custom loss function. Overrides is_multilabel default. |
None
|
gradient_clip_norm
|
float
|
Max gradient norm for clipping (0 = disabled). |
0.0
|
class_weights
|
dict[int, float] | None
|
Optional class index → weight mapping for imbalanced data. |
None
|
resume
|
bool
|
If True, load optimizer state from a previous run and continue. |
False
|
extra_callbacks
|
list[Callback] | None
|
Additional Keras callbacks (e.g. QAT callback). |
None
|
Returns:
| Type | Description |
|---|---|
History
|
Keras training history. |
Source code in birdnet_stm32/training/trainer.py
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 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 | |
compute_hop_length(sample_rate, chunk_duration, spec_width)
¶
Compute hop length to produce spec_width frames from an input chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_rate
|
int
|
Sampling rate (Hz). |
required |
chunk_duration
|
int
|
Chunk duration (seconds). |
required |
spec_width
|
int
|
Desired number of frames. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Hop length in samples (floor(T / spec_width), at least 1). |