# FastASR
这是一个用C++实现ASR推理的项目,它依赖很少,安装也很简单,推理速度很快,在树莓派4B等ARM平台也可以流畅的运行。
支持的模型是由Google的Transformer模型中优化而来,数据集是开源wenetspeech(10000+小时)或阿里私有数据集(60000+小时), 所以识别效果也很好,可以媲美许多商用的ASR软件。
## 项目简介
目前本项目实现了4个模型,3个非流式模型,1个流式模型,如下表所示。
| 名称 | 来源 | 数据集 | 模型 | 语言 |
|:----------------:|:--------------------------------------------------------------------------------------------------------------------------:|:-------------------:|:-----------------------------------:|:-----:|
| paraformer | [阿里达摩院](https://modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/summary) | 私有数据集(60000h) | Paraformer-large | zh+en |
| k2_rnnt2 | [kaldi2](https://github.com/k2-fsa/icefall/tree/master/egs/wenetspeech/ASR) | WenetSpeech(10000h) | pruned_transducer_stateless2 | zh |
| conformer | [paddlespeech](https://github.com/PaddlePaddle/PaddleSpeech/releases/tag/r1.0.1) | WenetSpeech(10000h) | conformer_wenetspeech-zh-16k | zh |
| conformer_online | [paddlespeech](https://github.com/PaddlePaddle/PaddleSpeech/releases/tag/r1.0.1) | WenetSpeech(10000h) | conformer_online_wenetspeech-zh-16k | zh |
* **非流式模型**:每次识别是以句子为单位,所以实时性会差一些,但准确率会高一些。
* **流式模型**:模型的输入是语音流,并实时返回语音识别的结果,但是准确率会下降些。
conformer_online是流式模型,其它模型为非流式模型。
目前通过使用VAD技术, 非流式模型支持大段的长语音识别。
上面提到的这些模型都是基于深度学习框架(paddlepaddle或pytorch)实现的, 本身的性能已经很不错了,即使在没有GPU的个人电脑上运行,
也能满足实时性的要求(如:时长为10s的语音,推理时间小于10s,即可满足实时性)。
但是要把深度学习模型部署在ARM平台,会遇到两个方面的困难。
* 不容易安装,需要自己编译一些组件。
* 执行效率很慢,无法满足实时性的要求。
因此就有这个项目,它由纯C++编写,仅实现了模型的推理过程。
* **语言优势**: 由于C++和Python不同,是编译型语言,编译器会根据编译选项针对不同平台的CPU进行优化,更适合在不同CPU平台上面部署,充分利用CPU的计算资源。
* **独立**: 实现不依赖于现有的深度学习框架如pytorch、paddle、tensorflow等。
* **依赖少**: 项目仅使用了两个第三方库libfftw3和libopenblas,并无其他依赖,所以在各个平台的可移植行很好,通用性很强。
* **效率高**:算法中大量使用指针,减少原有算法中reshape和permute的操作,减少不必要的数据拷贝,从而提升算法性能。
针对C++用户和python用户,本项目分别生成了静态库libfastasr.a和PyFastASR.XXX模块,调用方法可以参考example目录中的例子。
### 未完成工作
* 量化和压缩模型
## python安装
目前fastasr在个平台的支持情况如下表, 其他未支持的平台可通过源码编译获得对应的whl包。
| | macOS Intel | Windows 64bit | Windows 32bit | Linux x86 | Linux x64 | Linux aarch64 |
|---------------|----|-----|-----|----|-----|----|
| CPython 3.6 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.7 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.8 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.9 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.10 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.11 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
可通过pip直接安装
```
pip install fastasr
```
## 源码编译安装指南
### Ubuntu 安装依赖
安装依赖库libfftw3
```shell
sudo apt-get install libfftw3-dev libfftw3-single3
```
安装依赖库libopenblas
```shell
sudo apt-get install libopenblas-dev
```
安装python环境
```shell
sudo apt-get install python3 python3-dev
```
### MacOS 安装依赖
安装依赖库fftw
```shell
sudo brew install fftw
```
安装依赖库openblas
```shell
sudo brew install openblas
```
### 编译源码
#### Build for Linux
下载最新版的源码
```shell
git clone https://github.com/chenkui164/FastASR.git
```
编译最新版的源码,
```shell
cd FastASR/
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
```
编译python的whl安装包
```shell
cd FastASR/
python -m build
```
#### Build for Windows
[Windows编译指南](win/readme.md)
使用VisualStudio 2022打开CMakeLists.txt,选择Release编译。
需要在vs2022安装linux开发组件。
### 下载预训练模型
#### paraformer预训练模型下载
进入FastASR/models/paraformer_cli文件夹,用于存放下载的预训练模型.
```shell
cd ../models/paraformer_cli
```
从modelscope官网下载预训练模型,预训练模型所在的[仓库地址](https://modelscope.cn/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/files)
也可通过命令一键下载。
```shell
wget --user-agent="Mozilla/5.0" -c "https://www.modelscope.cn/api/v1/models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/repo?Revision=v1.0.4&FilePath=model.pb"
mv repo\?Revision\=v1.0.4\&FilePath\=model.pb model.pb
```
将用于Python的模型转换为C++的,这样更方便通过内存映射的方式直接读取参数,加快模型读取速度。
```shell
../scripts/paraformer_convert.py model.pb
```
查看转换后的参数文件wenet_params.bin的md5码,md5码为c77bc27e5758ebdc28a9024460e48602,表示转换正确。
```
md5sum -b wenet_params.bin
```
#### k2_rnnt2预训练模型下载
进入FastASR/models/k2_rnnt2_cli文件夹,用于存放下载的预训练模型.
```shell
cd ../models/k2_rnnt2_cli
```
从huggingface官网下载预训练模型,预训练模型所在的[仓库地址](https://huggingface.co/luomingshuang/icefall_asr_wenetspeech_pruned_transducer_stateless2)
也可通过命令一键下载。
```shell
wget -c https://huggingface.co/luomingshuang/icefall_asr_wenetspeech_pruned_transducer_stateless2/resolve/main/exp/pretrained_epoch_10_avg_2.pt
```
将用于Python的模型转换为C++的,这样更方便通过内存映射的方式直接读取参数,加快模型读取速度。
```shell
../scripts/k2_rnnt2_convert.py pretrained_epoch_10_avg_2.pt
```
查看转换后的参数文件wenet_params.bin的md5码,md5码为33a941f3c1a20a5adfb6f18006c11513,表示转换正确。
```
md5sum -b wenet_params.bin
```
#### conformer_wenetspeech-zh-16k预训练模型下载
进入FastASR/models/paddlespeech_cli文件夹,用于存放下载的预训练模型.
```shell
cd ../models/paddlespeech_cli
```
从PaddleSpeech官网下载预训练模型,如果之前已经在运行过PaddleSpeech,
则可以不用下载,它已经在目录`~/.paddlespeech/models/conformer_wenetspeech-zh-16k`中。
```shell
wget -c https://paddlespeech.bj.bcebos.com/s2t/wenetspeech/asr1_conformer_wenetspeech_ckpt_0.1.1.model.tar.gz
```
将压缩包解压wenetspeech目录下
```
mkdir wenetspeech
tar -xzvf asr1_conformer_wenetspeech_ckpt_0.1.1.model.tar.gz -C wenetspeech
```
将用于Python的模型转换为C++的,这样更方便通过内存映射的方式直接读取参数,加快模型读取速度。
```shell
../scripts/paddlespeech_convert.py wenetspeech/exp/conformer/checkpoints/