dscnn
birdnet_stm32.models.dscnn
¶
DS-CNN (depthwise-separable CNN) model architecture for audio classification.
The model consists of: - An AudioFrontendLayer (from frontend.py) for feature extraction. - A stem convolution to lift channels. - Four stages of depthwise-separable blocks with stride-2 downsampling. - A pooling head (global average, or frequency mean then time max + mean), dropout, and a dense classifier head.
Scaling is controlled via alpha (width multiplier) and depth_multiplier (block repeats). All channel counts are aligned to multiples of 8 for NPU vectorization.
ds_conv_block(x, out_ch, stride_f=1, stride_t=1, name='ds', drop_rate=0.1, dw_kernel_size=3)
¶
Depthwise-separable block (k x k DW + 1x1 PW) with optional residual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor [B, H, W, C]. |
required |
out_ch
|
int
|
Output channels for pointwise conv. |
required |
stride_f
|
int
|
Stride along frequency axis. |
1
|
stride_t
|
int
|
Stride along time axis. |
1
|
name
|
str
|
Base name for layers. |
'ds'
|
drop_rate
|
float
|
Spatial dropout rate after PW BN. |
0.1
|
dw_kernel_size
|
int
|
Square depthwise kernel size. |
3
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor [B, H', W', out_ch]. |
Neither kernel carries weight decay. On the depthwise kernels it killed 54-62% of the depthwise channels during 2.0 training (zero after ReLU6 on all data); on the pointwise kernels (1e-4) it killed 35-49% of them in a model widened in stages 3-4 and left it behind the narrower one. Without either, none died.
Source code in birdnet_stm32/models/dscnn.py
pooling_head(x, head_pooling='gap')
¶
Collapse a [B, F, T, C] feature map into a [B, C] embedding vector.
Global average pooling over frequency and time. (A max + mean over time
was measured and lost; gap is the only head.)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Feature map with static frequency and time dimensions. |
required |
head_pooling
|
str
|
One of |
'gap'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Embedding tensor [B, C]. |
Source code in birdnet_stm32/models/dscnn.py
build_dscnn_model(num_mels, spec_width, sample_rate, chunk_duration, embeddings_size, num_classes, audio_frontend='hybrid', alpha=1.0, depth_multiplier=1, fft_length=512, mag_scale='pwl', raw_magnitude=RELEASE_RAW_MAGNITUDE, raw_overlap=2, raw_bank=RELEASE_RAW_BANK, raw_split_axis=RELEASE_RAW_SPLIT_AXIS, raw_exposure_gain=1.0, raw_exposure_mode='compress', frontend_trainable=False, dropout_rate=0.5, head_pooling='gap', dw_kernel_size=3, stage_widths=None, backbone='dscnn')
¶
Build a DS-CNN model with a selectable audio frontend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_mels
|
int
|
Number of mel bins. |
required |
spec_width
|
int
|
Spectrogram width (frames). |
required |
sample_rate
|
int
|
Sampling rate (Hz). |
required |
chunk_duration
|
int
|
Chunk duration (seconds). |
required |
embeddings_size
|
int
|
Channels in the final embeddings layer. |
required |
num_classes
|
int
|
Number of output classes. |
required |
audio_frontend
|
str
|
'librosa' | 'hybrid' | 'raw'. |
'hybrid'
|
alpha
|
float
|
Width multiplier for the backbone. |
1.0
|
depth_multiplier
|
int
|
Repeats multiplier for DS blocks per stage. |
1
|
fft_length
|
int
|
FFT size for hybrid/librosa paths. |
512
|
mag_scale
|
str
|
Magnitude scaling ('pwl' | 'none'). |
'pwl'
|
raw_magnitude
|
str
|
Quadrature combination for the raw frontend ('alpha_max' | 'l1' | 'halfwave'); ignored by other frontends. |
RELEASE_RAW_MAGNITUDE
|
raw_overlap
|
int
|
Raw analysis window as a multiple of its hop; raw only. |
2
|
raw_bank
|
str
|
'pair' or 'fused' quadrature filterbank; raw only. Defaults to the release layout, as raw_magnitude does. |
RELEASE_RAW_BANK
|
raw_split_axis
|
str
|
'channels' or 'taps': how the raw filterbank is cut into partial convolutions; raw only. Defaults to the release layout. |
RELEASE_RAW_SPLIT_AXIS
|
raw_exposure_gain
|
float
|
Gain G of a second filterbank exposure (> 1 enables it; experimental, fused bank and tap split only). |
1.0
|
raw_exposure_mode
|
str
|
'channels' (the frontend emits both exposures and the stem takes both) or 'compress' (mixed per band into one knee-compressed channel before band_bn). |
'compress'
|
frontend_trainable
|
bool
|
Make frontend sub-layers trainable. |
False
|
dropout_rate
|
float
|
Dropout rate before the classifier head. |
0.5
|
head_pooling
|
str
|
Pooling head, one of |
'gap'
|
dw_kernel_size
|
int
|
Depthwise kernel size in stages 2-4; stage 1, which carries the largest feature map, stays 3x3. |
3
|
stage_widths
|
tuple[int, ...] | list[int] | None
|
Base output channels of the four stages before the
alpha multiplier; defaults to |
None
|
backbone
|
str
|
One of |
'dscnn'
|
Returns:
| Type | Description |
|---|---|
Model
|
Uncompiled DS-CNN Keras model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If raw frontend exceeds STM32N6 input size limit (65536), or on an unknown head_pooling, dw_kernel_size or backbone, or stage_widths that are not four positive widths. |
Source code in birdnet_stm32/models/dscnn.py
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |