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 or inverted-residual blocks with stride-2 downsampling. - Optional squeeze-and-excite (SE) channel attention per block. - Global average pooling (or attention pooling), 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)
¶
Depthwise-separable block (3x3 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
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor [B, H', W', out_ch]. |
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='precomputed', alpha=1.0, depth_multiplier=1, fft_length=512, mag_scale='none', frontend_trainable=False, class_activation='softmax', dropout_rate=0.5, n_mfcc=20, weight_decay=0.0001, use_se=False, se_reduction=4, use_inverted_residual=False, expansion_factor=6, use_attention_pooling=False)
¶
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' | 'mfcc' | 'log_mel' (deprecated: 'precomputed', 'tf'). |
'precomputed'
|
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 ('pcen' | 'pwl' | 'db' | 'none'). |
'none'
|
frontend_trainable
|
bool
|
Make frontend sub-layers trainable. |
False
|
class_activation
|
str
|
'softmax' or 'sigmoid' for the classifier head. |
'softmax'
|
dropout_rate
|
float
|
Dropout rate before the classifier head. |
0.5
|
n_mfcc
|
int
|
Number of MFCC coefficients (only used when audio_frontend='mfcc'). |
20
|
weight_decay
|
float
|
L2 regularization weight for DS-CNN blocks. |
0.0001
|
use_se
|
bool
|
Add SE channel attention after each block. |
False
|
se_reduction
|
int
|
SE channel reduction factor. |
4
|
use_inverted_residual
|
bool
|
Use inverted residual blocks instead of DS blocks. |
False
|
expansion_factor
|
int
|
Expansion factor for inverted residual hidden dim. |
6
|
use_attention_pooling
|
bool
|
Use attention pooling instead of GAP. |
False
|
Returns:
| Type | Description |
|---|---|
Model
|
Uncompiled DS-CNN Keras model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If raw frontend exceeds STM32N6 input size limit (65536). |
Source code in birdnet_stm32/models/dscnn.py
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 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 | |