FunASR Paraformer Alibaba INTERSPEECH 2022

Paraformer-large模型介绍

Highlights

<strong>ModelScope-FunASR</strong>

<strong>FunASR</strong>希望在语音识别方面建立学术研究和工业应用之间的桥梁。通过支持在ModelScope上发布的工业级语音识别模型的训练和微调,研究人员和开发人员可以更方便地进行语音识别模型的研究和生产,并促进语音识别生态系统的发展。

最新动态 | 环境安装 | 介绍文档 | 中文教程 | 服务部署 | 模型库 | 联系我们

项目介绍

Paraformer是达摩院语音团队提出的一种高效的非自回归端到端语音识别框架。本项目为Paraformer中文通用语音识别模型,采用工业级数万小时的标注音频进行模型训练,保证了模型的通用识别效果。模型可以被应用于语音输入法、语音导航、智能会议纪要等场景。

<p align="center"> <img src="fig/struct.png" alt="Paraformer模型结构" width="500" />

Paraformer模型结构如上图所示,由 Encoder、Predictor、Sampler、Decoder 与 Loss function 五部分组成。Encoder可以采用不同的网络结构,例如self-attention,conformer,SAN-M等。Predictor 为两层FFN,预测目标文字个数以及抽取目标文字对应的声学向量。Sampler 为无可学习参数模块,依据输入的声学向量和目标向量,生产含有语义的特征向量。Decoder 结构与自回归模型类似,为双向建模(自回归为单向建模)。Loss function 部分,除了交叉熵(CE)与 MWER 区分性优化目标,还包括了 Predictor 优化目标 MAE。

其核心点主要有:

更详细的细节见:

如何使用与训练自己的模型

本项目提供的预训练模型是基于大数据训练的通用领域识别模型,开发者可以基于此模型进一步利用ModelScope的微调功能或者本项目对应的Github代码仓库FunASR进一步进行模型的领域定制化。

在Notebook中开发

对于有开发需求的使用者,特别推荐您使用Notebook进行离线处理。先登录ModelScope账号,点击模型页面右上角的“在Notebook中打开”按钮出现对话框,首次使用会提示您关联阿里云账号,按提示操作即可。关联账号后可进入选择启动实例界面,选择计算资源,建立实例,待实例创建完成后进入开发环境,进行调用。

基于ModelScope进行推理

cat wav.scp
asr_example1  data/test/audios/asr_example1.wav
asr_example2  data/test/audios/asr_example2.wav
...
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

inference_pipeline = pipeline(
    task=Tasks.auto_speech_recognition,
    model='damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch')

rec_result = inference_pipeline(audio_in='https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav')
print(rec_result)
rec_result = inference_pipeline(audio_in='https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.pcm', audio_fs=16000)
rec_result = inference_pipeline(audio_in='asr_example_zh.wav')
inference_pipeline = pipeline(
    task=Tasks.auto_speech_recognition,
    model='damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch',
    output_dir='./output_dir')

inference_pipeline(audio_in="wav.scp")

识别结果输出路径结构如下:

tree output_dir/
output_dir/
└── 1best_recog
    ├── rtf
    ├── score
    └── text

1 directory, 3 files

rtf:计算过程耗时统计

score:识别路径得分

text:语音识别结果文件

import soundfile

waveform, sample_rate = soundfile.read("asr_example_zh.wav")
rec_result = inference_pipeline(audio_in=waveform)

可根据使用需求对VAD和PUNC标点模型进行自由组合,使用方式如下:

inference_pipeline = pipeline(
    task=Tasks.auto_speech_recognition,
    model='damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch',
    vad_model='damo/speech_fsmn_vad_zh-cn-16k-common-pytorch',
    punc_model='damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch',
)

若不使用PUNC模型,可配置punc_model="",或不传入punc_model参数,如需加入LM模型,可增加配置lm_model='damo/speech_transformer_lm_zh-cn-common-vocab8404-pytorch',并设置lm_weight和beam_size参数。

长音频版本模型默认开启时间戳,若不使用时间戳,可通过传入参数param_dict['use_timestamp'] = False关闭时间戳,使用方式如下:

param_dict['use_timestamp'] = False
rec_result = inference_pipeline(audio_in='https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav', param_dict=param_dict)
)

基于ModelScope进行微调

AISHELL-1数据集为例,完整数据集已经上传ModelScope,可通过数据集英文名(speech_asr_aishell1_trainsets)搜索:

import os
from modelscope.metainfo import Trainers
from modelscope.trainers import build_trainer
from modelscope.msdatasets.audio.asr_dataset import ASRDataset

def modelscope_finetune(params):
    if not os.path.exists(params.output_dir):
        os.makedirs(params.output_dir, exist_ok=True)
    # dataset split ["train", "validation"]
    ds_dict = ASRDataset.load(params.data_path, namespace='speech_asr')
    kwargs = dict(
        model=params.model,
        data_dir=ds_dict,
        dataset_type=params.dataset_type,
        work_dir=params.output_dir,
        batch_bins=params.batch_bins,
        max_epoch=params.max_epoch,
        lr=params.lr)
    trainer = build_trainer(Trainers.speech_asr_trainer, default_args=kwargs)
    trainer.train()


if __name__ == '__main__':
    from funasr.utils.modelscope_param import modelscope_args
    params = modelscope_args(model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch")
    params.output_dir = "./checkpoint"                      # 模型保存路径
    params.data_path = "speech_asr_aishell1_trainsets"      # 数据路径,可以为modelscope中已上传数据,也可以是本地数据
    params.dataset_type = "small"                           # 小数据量设置small,若数据量大于1000小时,请使用large
    params.batch_bins = 2000                                # batch size,如果dataset_type="small",batch_bins单位为fbank特征帧数,如果dataset_type="large",batch_bins单位为毫秒,
    params.max_epoch = 50                                   # 最大训练轮数
    params.lr = 0.00005                                     # 设置学习率
    
    modelscope_finetune(params)

可将上述代码保存为py文件(如finetune.py),直接python finetune.py运行;若使用多卡进行训练,如下命令:

CUDA_VISIBLE_DEVICES=1,2 python -m torch.distributed.launch --nproc_per_node 2 finetune.py > log.txt 2>&1
params.data_path = "speech_asr_aishell1_trainsets"

私有数据集格式按如下准备:

tree ./example_data/
./example_data/
├── validation
│   ├── text
│   └── wav.scp
└── train
    ├── text
    └── wav.scp
2 directories, 4 files

其中,text文件中存放音频标注(中文按照字分开,英文按照词分开),wav.scp文件中存放wav音频绝对路径,样例如下:

cat ./example_data/text
BAC009S0002W0122 而 对 楼 市 成 交 抑 制 作 用 最 大 的 限 购
BAC009S0002W0123 也 成 为 地 方 政 府 的 眼 中 钉
english_example_1 hello world
english_example_2 go swim 去 游 泳

cat ./example_data/wav.scp
BAC009S0002W0122 /mnt/data/wav/train/S0002/BAC009S0002W0122.wav
BAC009S0002W0123 /mnt/data/wav/train/S0002/BAC009S0002W0123.wav
english_example_1 /mnt/data/wav/train/S0002/english_example_1.wav
english_example_2 /mnt/data/wav/train/S0002/english_example_2.wav

在本地机器中开发

基于ModelScope进行微调和推理

支持基于ModelScope上数据集及私有数据集进行定制微调和推理,使用方式同Notebook中开发。

基于FunASR进行微调和推理

FunASR框架支持魔搭社区开源的工业级的语音识别模型的training & finetuning,使得研究人员和开发者可以更加便捷的进行语音识别模型的研究和生产,目前已在Github开源:https://github.com/alibaba-damo-academy/FunASR 。若在使用过程中遇到任何问题,欢迎联系我们:联系方式

FunASR框架安装

pip3 install -U modelscope
git clone https://github.com/alibaba/FunASR.git && cd FunASR
pip3 install -e ./

基于FunASR进行推理

接下来会以私有数据集为例,介绍如何在FunASR框架中使用Paraformer-large进行推理以及微调。

cd egs_modelscope/paraformer/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch
python demo.py

基于FunASR进行微调

cd egs_modelscope/paraformer/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch
python finetune.py

若修改输出路径、数据路径、采样率、batch_size等配置及使用多卡训练,可参照在Notebook开发中私有数据微调部分的代码,修改finetune.py文件中配置。

Benchmark

结合大数据、大模型优化的Paraformer在一序列语音识别的benchmark上获得当前SOTA的效果,以下展示学术数据集AISHELL-1、AISHELL-2、WenetSpeech,公开评测项目SpeechIO TIOBE白盒测试场景的效果。在学术界常用的中文语音识别评测任务中,其表现远远超于目前公开发表论文中的结果,远好于单独封闭数据集上的模型。

AISHELL-1

AISHELL-1 test w/o LM w/ LM
<div style="width: 150pt">Espnet</div> <div style="width: 150pt">4.90</div> <div style="width: 150pt">4.70</div>
<div style="width: 150pt">Wenet</div> <div style="width: 150pt">4.61</div> <div style="width: 150pt">4.36</div>
<div style="width: 150pt">K2</div> <div style="width: 150pt">-</div> <div style="width: 150pt">4.26</div>
<div style="width: 150pt">Blockformer</div> <div style="width: 150pt">4.29</div> <div style="width: 150pt">4.05</div>
<div style="width: 150pt">Paraformer-large</div> <div style="width: 150pt">1.95</div> <div style="width: 150pt">1.68</div>

AISHELL-2

dev_ios test_android test_ios test_mic
<div style="width: 150pt">Espnet</div> <div style="width: 70pt">5.40</div> <div style="width: 70pt">6.10</div> <div style="width: 70pt">5.70</div> <div style="width: 70pt">6.10</div>
<div style="width: 150pt">WeNet</div> <div style="width: 70pt">-</div> <div style="width: 70pt">-</div> <div style="width: 70pt">5.39</div> <div style="width: 70pt">-</div>
<div style="width: 150pt">Paraformer-large</div> <div style="width: 70pt">2.80</div> <div style="width: 70pt">3.13</div> <div style="width: 70pt">2.85</div> <div style="width: 70pt">3.06</div>

Wenetspeech

dev test_meeting test_net
<div style="width: 150pt">Espnet</div> <div style="width: 100pt">9.70</div> <div style="width: 100pt">15.90</div> <div style="width: 100pt">8.80</div>
<div style="width: 150pt">WeNet</div> <div style="width: 100pt">8.60</div> <div style="width: 100pt">17.34</div> <div style="width: 100pt">9.26</div>
<div style="width: 150pt">K2</div> <div style="width: 100pt">7.76</div> <div style="width: 100pt">13.41</div> <div style="width: 100pt">8.71</div>
<div style="width: 150pt">Paraformer-large</div> <div style="width: 100pt">3.57</div> <div style="width: 100pt">6.97</div> <div style="width: 100pt">6.74</div>

SpeechIO TIOBE

Paraformer-large模型结合Transformer-LM模型做shallow fusion,在公开评测项目SpeechIO TIOBE白盒测试场景上获得当前SOTA的效果,目前Transformer-LM模型已在ModelScope上开源,以下展示SpeechIO TIOBE白盒测试场景without LM、with Transformer-LM的效果:

testset w/o LM w/ LM
<div style="width: 200pt">SPEECHIO_ASR_ZH00001</div> <div style="width: 150pt">0.49</div> <div style="width: 150pt">0.35</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00002</div> <div style="width: 150pt">3.23</div> <div style="width: 150pt">2.86</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00003</div> <div style="width: 150pt">1.13</div> <div style="width: 150pt">0.80</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00004</div> <div style="width: 150pt">1.33</div> <div style="width: 150pt">1.10</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00005</div> <div style="width: 150pt">1.41</div> <div style="width: 150pt">1.18</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00006</div> <div style="width: 150pt">5.25</div> <div style="width: 150pt">4.85</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00007</div> <div style="width: 150pt">5.51</div> <div style="width: 150pt">4.97</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00008</div> <div style="width: 150pt">3.69</div> <div style="width: 150pt">3.18</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH00009</div> <div style="width: 150pt">3.02</div> <div style="width: 150pt">2.78</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000010</div> <div style="width: 150pt">3.35</div> <div style="width: 150pt">2.99</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000011</div> <div style="width: 150pt">1.54</div> <div style="width: 150pt">1.25</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000012</div> <div style="width: 150pt">2.06</div> <div style="width: 150pt">1.68</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000013</div> <div style="width: 150pt">2.57</div> <div style="width: 150pt">2.25</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000014</div> <div style="width: 150pt">3.86</div> <div style="width: 150pt">3.08</div>
<div style="width: 200pt">SPEECHIO_ASR_ZH000015</div> <div style="width: 150pt">3.34</div> <div style="width: 150pt">2.67</div>

使用方式以及适用范围

运行范围

使用方式

使用范围与目标场景

模型局限性以及可能的偏差

考虑到特征提取流程和工具以及训练工具差异,会对CER的数据带来一定的差异(<0.1%),推理GPU环境差异导致的RTF数值差异。

相关论文以及引用信息

@inproceedings{gao2022paraformer,
  title={Paraformer: Fast and Accurate Parallel Transformer for Non-autoregressive End-to-End Speech Recognition},
  author={Gao, Zhifu and Zhang, Shiliang and McLoughlin, Ian and Yan, Zhijie},
  booktitle={INTERSPEECH},
  year={2022}
}