efficientnet
birdnet_stm32.models.efficientnet
¶
N6-B0: an EfficientNet-B0 backbone for INT8 on the STM32N6 (2.2).
EfficientNet-B0's inverted-residual MBConv blocks (Tan & Le 2019) behind the project's audio frontends, with the three changes that make B0 survive INT8 on the Neural-ART NPU: ReLU6 instead of SiLU, no squeeze-and-excite, and a bounded residual stream (ReLU6 after each projection and residual add, so the stream stays in [0, 6] as in the DS-CNN blocks). B0 as published lost 14% of its WABAD AP to post-training quantization; N6-B0 trained to 2.2's float WABAD-61 AUPRC 0.465 (2.1 Raw: 0.374), INT8 0.441.
The block table and stochastic depth follow B0 at width and depth 1.0. The top
1x1 conv emits embeddings_size channels (1280), pooled by the same gap
layer as the DS-CNN so the embedding loss and the head fine-tune find their
layers.
For the 2.5 size study the stage table can be any table (parse_blocks), and
a channel-attention block (ChannelAttention, EdgeNeXt's transposed
attention) can follow the last block of chosen stages. On the STM32N6 one such
block after stage 7 of N6-B0 adds 4 ms (36.9 vs 32.9 ms on target), three after
stages 3, 5 and 7 add 11 ms: the per-head matmuls are split per head and the L2
norm runs in float on the Cortex-M55, but everything maps.
ChannelAttention
¶
Bases: Layer
Transposed (cross-covariance) attention of XCiT and EdgeNeXt (arXiv 2206.10589) on [q, k, v].
Each input is [B, F, T, C]. Per head of C / heads channels, q and k
are L2-normalized over the F * T positions, their C x C channel
affinities, scaled by a learned temperature per head, go through a softmax,
and the result mixes the channels of v. Cost grows with positions times
channels squared, not positions squared. Shaped for TFLite: the
normalization and the softmax run over the last axis, transposes stay 4-D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heads
|
int
|
Number of heads; must divide the channel count. |
required |
Source code in birdnet_stm32/models/efficientnet.py
parse_blocks(spec)
¶
A stage table from "e,k,r,c,s/e,k,r,c,s/..." (expand, kernel, repeats, channels, first stride per stage).
Raises:
| Type | Description |
|---|---|
ValueError
|
On a stage that is not five positive integers, an even kernel, or a stride other than 1 or 2 (larger strides fall back to the Cortex-M55 on the STM32N6). |
Source code in birdnet_stm32/models/efficientnet.py
channel_attention_block(x, name)
¶
A bounded residual ChannelAttention block: BN, 1x1 q / k / v, attention, 1x1 projection, BN, add, ReLU6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor [B, F, T, C]. |
required |
name
|
str
|
Base name for layers. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor of the same shape. |
Source code in birdnet_stm32/models/efficientnet.py
mbconv_block(x, out_ch, expand, kernel, stride, drop_rate, name)
¶
MBConv without squeeze-and-excite: 1x1 expand, k x k depthwise, 1x1 project, bounded residual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor [B, F, T, C]. |
required |
out_ch
|
int
|
Output channels of the projection. |
required |
expand
|
int
|
Expansion ratio of the hidden width (1 skips the expand conv). |
required |
kernel
|
int
|
Square depthwise kernel size. |
required |
stride
|
int
|
Depthwise stride on both axes. |
required |
drop_rate
|
float
|
Stochastic-depth rate on the residual branch. |
required |
name
|
str
|
Base name for layers. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor [B, F', T', out_ch]. |
Source code in birdnet_stm32/models/efficientnet.py
efficientnet_b0_backbone(x, embeddings_size, blocks=B0_BLOCKS, attention_stages=())
¶
N6-B0 stem, the MBConv stages and the top conv on a frontend's [B, F, T, C] output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Frontend output. |
required |
embeddings_size
|
int
|
Channels of the top 1x1 conv (the pooled embedding). |
required |
blocks
|
tuple
|
The stage table, (expand, kernel, repeats, output channels, first stride) per stage; B0's by default. |
B0_BLOCKS
|
attention_stages
|
tuple | list
|
1-based stages whose last block is followed by a |
()
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Feature map [B, F/32, T/32, embeddings_size] (for B0's four stride-2 stages after the stem). |
Raises:
| Type | Description |
|---|---|
ValueError
|
On an attention stage outside the table. |