generator
birdnet_stm32.data.generator
¶
Batch generator and tf.data.Dataset wrapper for training and validation.
Provides a Python generator that yields (inputs, one_hot_labels) batches, and a tf.data.Dataset wrapper with static shape signatures.
data_generator(file_paths, classes, batch_size=32, audio_frontend='librosa', sample_rate=24000, max_duration=30, chunk_duration=3, spec_width=128, mixup_alpha=0.2, mixup_probability=0.25, mel_bins=48, fft_length=512, mag_scale='none', random_offset=False, snr_threshold=0.5, spec_augment=False, freq_mask_max=8, time_mask_max=25, n_mfcc=20)
¶
Yield batches of (inputs, one_hot_labels) for training/validation.
Frontends and input shapes
- precomputed/librosa: mel spectrogram -> [B, mel_bins, spec_width, 1]
- hybrid: linear STFT magnitude -> [B, fft_bins, spec_width, 1]
- raw/tf: waveform -> [B, T, 1], peak-normalized to [-1, 1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
Audio file paths. |
required |
classes
|
list[str]
|
Ordered class names for one-hot encoding. |
required |
batch_size
|
int
|
Batch size. |
32
|
audio_frontend
|
str
|
'librosa' | 'hybrid' | 'raw' (deprecated: 'precomputed', 'tf'). |
'librosa'
|
sample_rate
|
int
|
Sampling rate (Hz). |
24000
|
max_duration
|
int
|
Max duration to read per file (seconds). |
30
|
chunk_duration
|
float
|
Chunk duration (seconds). |
3
|
spec_width
|
int
|
Target spectrogram width (frames). |
128
|
mixup_alpha
|
float
|
Mixup strength parameter. |
0.2
|
mixup_probability
|
float
|
Fraction of the batch to apply mixup to. |
0.25
|
mel_bins
|
int
|
Number of mel bins. |
48
|
fft_length
|
int
|
FFT size. |
512
|
mag_scale
|
str
|
'pcen' | 'pwl' | 'db' | 'none'. |
'none'
|
random_offset
|
bool
|
Randomly offset chunk start within file. |
False
|
snr_threshold
|
float
|
Minimum activity threshold for chunk selection. |
0.5
|
spec_augment
|
bool
|
Apply SpecAugment (freq/time masking) to spectrograms. |
False
|
freq_mask_max
|
int
|
Maximum frequency mask width (bins) for SpecAugment. |
8
|
time_mask_max
|
int
|
Maximum time mask width (frames) for SpecAugment. |
25
|
Yields:
| Type | Description |
|---|---|
|
Tuple of (inputs, labels) for a batch. Infinite generator. |
Source code in birdnet_stm32/data/generator.py
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 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 199 200 201 202 203 204 205 | |
load_dataset(file_paths, classes, audio_frontend='precomputed', batch_size=32, spec_width=128, mel_bins=48, **kwargs)
¶
Wrap the Python generator as a tf.data.Dataset with static shapes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
Audio file paths. |
required |
classes
|
list[str]
|
Ordered class names. |
required |
audio_frontend
|
str
|
'librosa' | 'hybrid' | 'raw' (deprecated: 'precomputed', 'tf'). |
'precomputed'
|
batch_size
|
int
|
Batch size. |
32
|
spec_width
|
int
|
Target spectrogram width. |
128
|
mel_bins
|
int
|
Number of mel bins. |
48
|
**kwargs
|
Forwarded to data_generator (sample_rate, chunk_duration, etc.). |
{}
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Infinite tf.data.Dataset of (inputs, labels) with prefetching. |