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', weight_decay=0.0001, 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'
|
weight_decay
|
float
|
L2 regularization for DW/PW kernels. |
0.0001
|
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]. |
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.
gap averages over frequency and time. freq_mean_time_maxmean
averages over frequency, then adds the max and the mean over time, so a
call that fills a few frames of the window is not averaged away. It adds no
trainable weights.
The frequency mean is a frozen depthwise convolution with a constant
1/F kernel rather than a pooling op: on the STM32N6 an average that
collapses frequency but keeps time (AveragePool or MEAN alike) falls
back to the Cortex-M55, while the convolution stays on the NPU. For F = 4
the kernel value 0.25 is exact on the per-channel INT8 grid.
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', frontend_trainable=False, dropout_rate=0.5, weight_decay=0.0001, head_pooling='gap', dw_kernel_size=3, stage_widths=None)
¶
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'
|
frontend_trainable
|
bool
|
Make frontend sub-layers trainable. |
False
|
dropout_rate
|
float
|
Dropout rate before the classifier head. |
0.5
|
weight_decay
|
float
|
L2 regularization weight for DS-CNN blocks. |
0.0001
|
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
|
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 or dw_kernel_size, or stage_widths that are not four positive widths. |
Source code in birdnet_stm32/models/dscnn.py
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 | |