File size: 8,515 Bytes
31d3b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
---
license: mit
library_name: rcdl
pipeline_tag: audio-classification
tags:
- rnpu
- rockchip
- rk3588
- npu
- edge
- audio-tagging
- audioset
- efficientat
- audio
---

# EfficientAT mn10_as for RK3588: audio tagging, AudioSet's 527 classes

Compiled model (`.rnpu`) for the **Rockchip RK3588 / RK3588S** NPU, ready to
load: no PyTorch, no ONNX export, no compiler run on your side. One fp16 graph
for the own NPU stack of [**RCDL**](https://github.com/ruisv/rcdl), a C++17 /
Python library for vision and speech on these boards. RCDL's compiler built it
from ONNX, and its runtime runs it by driving the board's rknpu kernel driver
directly, so librknnrt and rknn-toolkit2 are not needed
([how the stack works](https://github.com/ruisv/rcdl/blob/main/docs/OPEN_NPU.md)).
It needs RCDL 0.3.1 or newer.

What is in a recording: speech, music, animals, alarms and the rest of
AudioSet's 527 classes, each as an independent probability.

Upstream: [EfficientAT](https://github.com/fschmid56/EfficientAT), the `mn10_as`
model trained on AudioSet.

> [!TIP]
> **Redistributable, including commercially.** EfficientAT is MIT on the code
> and the weights. The class names in `audioset_labels.csv` are AudioSet's
> ontology, which is CC BY-SA 4.0. See [Licence](#licence).

## Files

| file | what it is | size |
|---|---|---|
| `efficientat_mn10_as_1000_fp16.rnpu` | `[1,1,128,1000]` log-mel (10 s at 32 kHz) → `[1,527]` sigmoid probabilities | 9.8 MB |
| `audioset_labels.csv` | AudioSet's `class_labels_indices.csv`: the 527 class names in index order | < 0.1 MB |

The front end is EfficientAT's own, rewritten in RCDL and run on the CPU:
16 kHz audio upsampled by 2 with a windowed sinc, pre-emphasis 0.97, a centred
STFT (1024, Hann 800, hop 320), a Kaldi mel bank over 0–15 kHz, then
(log(x + 1e-5) + 4.5) / 5. It is within 2.2e-5 of the original.

## Measured on RK3588S and RK3588

| | RK3588S | RK3588 |
|---|---|---|
| an AISHELL-1 sentence → 527 probabilities | 111 ms | 109 ms |
| of which the model run | 26.1 ms | 25.5 ms |
| that speech | Speech 0.81 | the same |
| a 1 kHz tone | Beep, bleep 0.86 | the same |
| white noise | Static 0.47 | the same |

RCDL 0.3.0 conda packages, RCDL's own NPU runtime, one NPU core, NPU driver
0.9.8; BSP kernel 6.1.118 on the RK3588S, 6.1.141 on the RK3588. The CPU
governor was `performance`, with the process pinned to the four A76 cores. Under
the boards' default `schedutil`, tasks that alternate NPU and CPU work measured
up to about four times slower, with the same results.

On AISHELL-1 clip 1, the probabilities are the float model's to 0.002, with
Speech first at 0.81.

## Use it

On the board (linux-aarch64):

```bash
conda create -n rcdl -c https://mirrors.ruis.ai/conda -c conda-forge rcdl
conda activate rcdl
curl -LO https://huggingface.co/ruisv/rcdl-efficientat/resolve/main/efficientat_mn10_as_1000_fp16.rnpu
curl -LO https://huggingface.co/ruisv/rcdl-efficientat/resolve/main/audioset_labels.csv
```

```python
import wave, numpy as np, rcdl

def read(path):                                      # 16 kHz mono PCM16
    with wave.open(path) as w:
        assert (w.getframerate(), w.getnchannels(), w.getsampwidth()) == (16000, 1, 2)
        return np.frombuffer(w.readframes(w.getnframes()), np.int16)

tagger = rcdl.AudioTagger.from_files("efficientat_mn10_as_1000_fp16.rnpu", "audioset_labels.csv")
for name in ("zh.wav", "en.wav"):
    tags = tagger.top(read(name), k=3)               # [(class name, probability)]
    print(f"{name}: " + ", ".join(f"{label} {p:.2f}" for label, p in tags))
```

## What to know before deploying

**The probabilities are independent.** Each class has its own sigmoid, so
several can be high at once and they do not sum to 1. `tagger.probabilities(pcm)`
gives all 527; set a threshold per class you care about.

**A call covers 10 s, and most of its time is the CPU front end.** A shorter
recording is padded with silence to 10 s; a longer one is tagged in 10 s windows
5 s apart, and each class keeps its highest probability.

**16 kHz or 32 kHz mono.** Pass `sample_rate=32000` for 32 kHz audio; other
rates are refused. The samples may be float in [-1, 1] or int16.

## Licence

**MIT**, on the EfficientAT code and the `mn10_as` weights, which were trained
on AudioSet. `audioset_labels.csv` is AudioSet's `class_labels_indices.csv`:
its class names are AudioSet's ontology, under **CC BY-SA 4.0**, so passing the
file on keeps that attribution and that licence.

**RCDL itself is Apache-2.0 and is unrelated to these terms.** It is a
general-purpose runtime that loads any `.rnpu` or `.rknn`. The licences above
apply to *these weights and these files* only.

The compiler flags of the build are in RCDL's
[`scripts/fetch_models.sh`](https://github.com/ruisv/rcdl/blob/main/scripts/fetch_models.sh),
and its input contract in
[`docs/MODELS.md`](https://github.com/ruisv/rcdl/blob/main/docs/MODELS.md).

## 中文说明

这是 EfficientAT `mn10_as` 面向**瑞芯微 RK3588 / RK3588S** NPU 的编译模型(`.rnpu`),
下载即可加载:不需要 PyTorch,不用导出 ONNX,也不用自己跑编译器。这一个 fp16 计算图是为
[RCDL](https://github.com/ruisv/rcdl) 的自研 NPU 栈编译的:RCDL 的编译器从 ONNX 生成它,
RCDL 的运行时直接驱动板子上的 rknpu 内核驱动来执行,不需要 librknnrt,也不需要
rknn-toolkit2([自研栈介绍](https://github.com/ruisv/rcdl/blob/main/docs/OPEN_NPU.zh.md))。
需要 RCDL 0.3.1 或更新版本。

用来判断录音里有什么声音:语音、音乐、动物、警报等 AudioSet 的 527 类,每一类各给一个独立的
概率。上游是 [EfficientAT](https://github.com/fschmid56/EfficientAT) 在 AudioSet 上训练的
`mn10_as` 模型。

> [!TIP]
> **可以再分发,包括商用。** EfficientAT 的代码和权重都是 MIT 许可。`audioset_labels.csv`
> 里的类名来自 AudioSet 的本体(ontology),许可为 CC BY-SA 4.0。

### 文件

| 文件 | 内容 | 大小 |
|---|---|---|
| `efficientat_mn10_as_1000_fp16.rnpu` | `[1,1,128,1000]` log-mel(32 kHz 下 10 s)→ `[1,527]` sigmoid 概率 | 9.8 MB |
| `audioset_labels.csv` | AudioSet 的 `class_labels_indices.csv`:按序号排列的 527 个类名 | < 0.1 MB |

前端是 EfficientAT 自己的那一套,在 RCDL 里重写、由 CPU 运行:16 kHz 音频用加窗 sinc 上采样
两倍,预加重 0.97,中心对齐的 STFT(1024,Hann 800,hop 320),0–15 kHz 的 Kaldi mel 滤波器组,
再算 (log(x + 1e-5) + 4.5) / 5;与原版相差不超过 2.2e-5。

### 实测

- 一句 AISHELL-1 普通话 → 527 个概率:RK3588S 上 111 ms(其中模型 26.1 ms),RK3588 上
  109 ms(25.5 ms)。
- 这段语音得到 Speech 0.81;1 kHz 纯音得到 Beep, bleep 0.86;白噪声得到 Static 0.47。两块板结果相同。
- 测量条件:RCDL 0.3.0 的 conda 包,自研 NPU 运行时,单个 NPU 核,NPU 驱动 0.9.8;
  RK3588S 为 BSP 内核 6.1.118,RK3588 为 6.1.141。CPU 调频策略为 `performance`,进程绑在
  四个 A76 核上;在板子默认的 `schedutil` 下,NPU 与 CPU 交替工作的任务最多会慢到约四倍,
  结果不变。
- 在 AISHELL-1 第 1 段音频上,各类概率与浮点模型相差不超过 0.002,排第一的是 Speech(0.81)。

### 用法

用法见上面的代码:装好 RCDL、下载模型和类名表,对 `zh.wav` 和 `en.wav` 各打印最可能的三类。

### 部署前须知

- **各类概率相互独立。** 每一类各有一个 sigmoid,几类可以同时很高,加起来也不等于 1。
  `tagger.probabilities(pcm)` 返回全部 527 个,按关心的类别各自设阈值。
- **一次调用覆盖 10 s,大部分时间花在 CPU 前端上。** 不足 10 s 的录音补静音;更长的按 10 s
  窗口、5 s 间隔逐窗打标签,每一类取各窗中的最高概率。
- **16 kHz 或 32 kHz 单声道。** 32 kHz 音频传 `sample_rate=32000`,其他采样率会被拒绝。采样
  可以是 float([-1, 1])或 int16。

### 许可

EfficientAT 的代码和 `mn10_as` 权重为 **MIT**,模型在 AudioSet 上训练。`audioset_labels.csv`
是 AudioSet 的 `class_labels_indices.csv`,类名属于 AudioSet 的本体,许可为 **CC BY-SA 4.0**:
转发这个文件时要保留署名,并沿用同一许可。

**RCDL 本身是 Apache-2.0,与上述条款无关。** 它是通用运行时,能加载任何 `.rnpu` 或
`.rknn`;上述许可只适用于*这些权重和这些文件*。