SenseVoice/README_zh.md
2024-07-23 15:42:09 +08:00

16 KiB
Raw Blame History

SenseVoice

「简体中文」|「English」|「日本語

SenseVoice是具有音频理解能力的音频基础模型包括语音识别ASR、语种识别LID、语音情感识别SER和声学事件分类AEC或声学事件检测AED。本项目提供SenseVoice模型的介绍以及在多个任务测试集上的benchmark以及体验模型所需的环境安装的与推理方式。

文档主页 核心功能

最新动态 Benchmark 环境安装 用法教程 联系我们

模型仓库:modelscopehuggingface

在线体验: modelscope demo, huggingface space

核心功能 🎯

SenseVoice专注于高精度多语言语音识别、情感辨识和音频事件检测

  • 多语言识别: 采用超过40万小时数据训练支持超过50种语言识别效果上优于Whisper模型。
  • 富文本识别:
    • 具备优秀的情感识别,能够在测试数据上达到和超过目前最佳情感识别模型的效果。
    • 支持声音事件检测能力,支持音乐、掌声、笑声、哭声、咳嗽、喷嚏等多种常见人机交互事件进行检测。
  • 高效推理: SenseVoice-Small模型采用非自回归端到端框架推理延迟极低10s音频推理仅耗时70ms15倍优于Whisper-Large。
  • 微调定制: 具备便捷的微调脚本与策略,方便用户根据业务场景修复长尾样本问题。
  • 服务部署: 具有完整的服务部署链路支持多并发请求支持客户端语言有python、c++、html、java与c#等。

最新动态 🔥

  • 2024/7新增加导出 ONNXlibtorch 功能,以及 python 版本 runtimefunasr-onnx-0.4.0funasr-torch-0.1.1
  • 2024/7: SenseVoice-Small 多语言音频理解模型开源,支持中、粤、英、日、韩语的多语言语音识别,情感识别和事件检测能力,具有极低的推理延迟。。
  • 2024/7: CosyVoice致力于自然语音生成支持多语言、音色和情感控制擅长多语言语音生成、零样本语音生成、跨语言语音克隆以及遵循指令的能力。CosyVoice repo and CosyVoice 在线体验.
  • 2024/7: FunASR 是一个基础语音识别工具包提供多种功能包括语音识别ASR、语音端点检测VAD、标点恢复、语言模型、说话人验证、说话人分离和多人对话语音识别等。

Benchmarks 📝

多语言语音识别

我们在开源基准数据集(包括 AISHELL-1、AISHELL-2、Wenetspeech、Librispeech和Common Voice上比较了SenseVoice与Whisper的多语言语音识别性能和推理效率。在中文和粤语识别效果上SenseVoice-Small模型具有明显的效果优势。

情感识别

由于目前缺乏被广泛使用的情感识别测试指标和方法我们在多个测试集的多种指标进行测试并与近年来Benchmark上的多个结果进行了全面的对比。所选取的测试集同时包含中文/英文两种语言以及表演、影视剧、自然对话等多种风格的数据在不进行目标数据微调的前提下SenseVoice能够在测试数据上达到和超过目前最佳情感识别模型的效果。

同时我们还在测试集上对多个开源情感识别模型进行对比结果表明SenseVoice-Large模型可以在几乎所有数据上都达到了最佳效果而SenseVoice-Small模型同样可以在多数数据集上取得超越其他开源模型的效果。

事件检测

尽管SenseVoice只在语音数据上进行训练它仍然可以作为事件检测模型进行单独使用。我们在环境音分类ESC-50数据集上与目前业内广泛使用的BEATS与PANN模型的效果进行了对比。SenseVoice模型能够在这些任务上取得较好的效果但受限于训练数据与训练方式其事件分类效果专业的事件检测模型相比仍然有一定的差距。

推理效率

SenseVoice-small模型采用非自回归端到端架构推理延迟极低。在参数量与Whisper-Small模型相当的情况下比Whisper-Small模型推理速度快5倍比Whisper-Large模型快15倍。同时SenseVoice-small模型在音频时长增加的情况下推理耗时也无明显增加。

安装依赖环境 🐍

pip install -r requirements.txt

用法 🛠️

推理

使用funasr推理

支持任意格式音频输入,支持任意时长输入

from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess

model_dir = "iic/SenseVoiceSmall"


model = AutoModel(
    model=model_dir,
    trust_remote_code=True,
    remote_code="./model.py",  
    vad_model="fsmn-vad",
    vad_kwargs={"max_single_segment_time": 30000},
    device="cuda:0",
)

# en
res = model.generate(
    input=f"{model.model_path}/example/en.mp3",
    cache={},
    language="auto",  # "zh", "en", "yue", "ja", "ko", "nospeech"
    use_itn=True,
    batch_size_s=60,
    merge_vad=True,  #
    merge_length_s=15,
)
text = rich_transcription_postprocess(res[0]["text"])
print(text)

参数说明:

  • model_dir:模型名称,或本地磁盘中的模型路径。
  • trust_remote_code
    • True表示model代码实现从remote_code处加载,remote_code指定model具体代码的位置(例如,当前目录下的model.py支持绝对路径与相对路径以及网络url。
    • False表示model代码实现为 FunASR 内部集成版本,此时修改当前目录下的model.py不会生效因为加载的是funasr内部版本模型代码点击查看
  • vad_model表示开启VADVAD的作用是将长音频切割成短音频此时推理耗时包括了VAD与SenseVoice总耗时为链路耗时如果需要单独测试SenseVoice模型耗时可以关闭VAD模型。
  • vad_kwargs表示VAD模型配置,max_single_segment_time: 表示vad_model最大切割音频时长, 单位是毫秒ms。
  • use_itn:输出结果中是否包含标点与逆文本正则化。
  • batch_size_s 表示采用动态batchbatch中总音频时长单位为秒s。
  • merge_vad:是否将 vad 模型切割的短音频碎片合成,合并后长度为merge_length_s单位为秒s。
  • ban_emo_unk禁用emo_unk标签禁用后所有的句子都会被赋与情感标签。

如果输入均为短音频小于30s并且需要批量化推理为了加快推理效率可以移除vad模型并设置batch_size

model = AutoModel(model=model_dir, trust_remote_code=True, device="cuda:0")

res = model.generate(
    input=f"{model.model_path}/example/en.mp3",
    cache={},
    language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
    use_itn=True,
    batch_size=64, 
)

更多详细用法,请参考 文档

直接推理

支持任意格式音频输入输入音频时长限制在30s以下

from model import SenseVoiceSmall
from funasr.utils.postprocess_utils import rich_transcription_postprocess

model_dir = "iic/SenseVoiceSmall"
m, kwargs = SenseVoiceSmall.from_pretrained(model=model_dir, device="cuda:0")


res = m.inference(
    data_in=f"{kwargs['model_path']}/example/en.mp3",
    language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
    use_itn=False,
    **kwargs,
)

text = rich_transcription_postprocess(res[0][0]["text"])
print(text)

服务部署

Undo

导出与测试

ONNX 与 Libtorch 导出

ONNX

# pip3 install -U funasr funasr-onnx
from pathlib import Path
from funasr_onnx import SenseVoiceSmall
from funasr_onnx.utils.postprocess_utils import rich_transcription_postprocess


model_dir = "iic/SenseVoiceSmall"

model = SenseVoiceSmall(model_dir, batch_size=10, quantize=True)

# inference
wav_or_scp = ["{}/.cache/modelscope/hub/{}/example/en.mp3".format(Path.home(), model_dir)]

res = model(wav_or_scp, language="auto", use_itn=True)
print([rich_transcription_postprocess(i) for i in res])

备注ONNX模型导出到原模型目录中

Libtorch

from pathlib import Path
from funasr_torch import SenseVoiceSmall
from funasr_torch.utils.postprocess_utils import rich_transcription_postprocess


model_dir = "iic/SenseVoiceSmall"

model = SenseVoiceSmall(model_dir, batch_size=10, device="cuda:0")

wav_or_scp = ["{}/.cache/modelscope/hub/{}/example/en.mp3".format(Path.home(), model_dir)]

res = model(wav_or_scp, language="auto", use_itn=True)
print([rich_transcription_postprocess(i) for i in res])

备注Libtorch模型导出到原模型目录中

部署

待完成

微调

安装训练环境

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

数据准备

数据格式需要包括如下几个字段:

{"key": "YOU0000008470_S0000238_punc_itn", "text_language": "<|en|>", "emo_target": "<|NEUTRAL|>", "event_target": "<|Speech|>", "with_or_wo_itn": "<|withitn|>", "target": "Including legal due diligence, subscription agreement, negotiation.", "source": "/cpfs01/shared/Group-speech/beinian.lzr/data/industrial_data/english_all/audio/YOU0000008470_S0000238.wav", "target_len": 7, "source_len": 140}
{"key": "AUD0000001556_S0007580", "text_language": "<|en|>", "emo_target": "<|NEUTRAL|>", "event_target": "<|Speech|>", "with_or_wo_itn": "<|woitn|>", "target": "there is a tendency to identify the self or take interest in what one has got used to", "source": "/cpfs01/shared/Group-speech/beinian.lzr/data/industrial_data/english_all/audio/AUD0000001556_S0007580.wav", "target_len": 18, "source_len": 360}

详细可以参考:data/train_example.jsonl

字段说明:

  • key: 数据唯一ID
  • source:音频文件的路径
  • source_len音频文件的fbank帧数
  • target:音频文件标注文本
  • target_len:音频文件标注文本长度
  • text_language:音频文件的语种标签
  • emo_target:音频文件的情感标签
  • event_target:音频文件的事件标签
  • with_or_wo_itn:标注文本中是否包含标点与逆文本正则化

可以用指令 sensevoice2jsonl 从train_wav.scp、train_text.txt、train_text_language.txt、train_emo_target.txt和train_event_target.txt生成准备过程如下

train_text.txt

左边为数据唯一ID需与train_wav.scp中的ID一一对应 右边为音频文件标注文本,格式如下:

BAC009S0764W0121 甚至出现交易几乎停滞的情况
BAC009S0916W0489 湖北一公司以员工名义贷款数十员工负债千万
asr_example_cn_en 所有只要处理 data 不管你是做 machine learning 做 deep learning 做 data analytics 做 data science 也好 scientist 也好通通都要都做的基本功啊那 again 先先对有一些>也许对
ID0012W0014 he tried to think how it could be

train_wav.scp

左边为数据唯一ID需与train_text.txt中的ID一一对应 右边为音频文件的路径,格式如下

BAC009S0764W0121 https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/BAC009S0764W0121.wav
BAC009S0916W0489 https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/BAC009S0916W0489.wav
asr_example_cn_en https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_cn_en.wav
ID0012W0014 https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_en.wav

train_text_language.txt

左边为数据唯一ID需与train_text_language.txt中的ID一一对应 右边为音频文件的语种标签,支持<|zh|><|en|><|yue|><|ja|><|ko|>,格式如下

BAC009S0764W0121 <|zh|>
BAC009S0916W0489 <|zh|>
asr_example_cn_en <|zh|>
ID0012W0014 <|en|>

train_emo.txt

左边为数据唯一ID需与train_emo.txt中的ID一一对应 右边为音频文件的情感标签,支持<|HAPPY|><|SAD|><|ANGRY|><|NEUTRAL|><|FEARFUL|><|DISGUSTED|><|SURPRISED|>,格式如下

BAC009S0764W0121 <|NEUTRAL|>
BAC009S0916W0489 <|NEUTRAL|>
asr_example_cn_en <|NEUTRAL|>
ID0012W0014 <|NEUTRAL|>

train_event.txt

左边为数据唯一ID需与train_event.txt中的ID一一对应 右边为音频文件的事件标签,支持<|BGM|><|Speech|><|Applause|><|Laughter|><|Cry|><|Sneeze|><|Breath|><|Cough|>,格式如下

BAC009S0764W0121 <|Speech|>
BAC009S0916W0489 <|Speech|>
asr_example_cn_en <|Speech|>
ID0012W0014 <|Speech|>

生成指令

# generate train.jsonl and val.jsonl from wav.scp, text.txt, text_language.txt, emo_target.txt, event_target.txt
sensevoice2jsonl \
++scp_file_list='["../../../data/list/train_wav.scp", "../../../data/list/train_text.txt", "../../../data/list/train_text_language.txt", "../../../data/list/train_emo.txt", "../../../data/list/train_event.txt"]' \
++data_type_list='["source", "target", "text_language", "emo_target", "event_target"]' \
++jsonl_file_out="../../../data/list/train.jsonl"

若无train_text_language.txt、train_emo_target.txt和train_event_target.txt则自动通过使用SenseVoice模型对语种、情感和事件打标。

# generate train.jsonl and val.jsonl from wav.scp and text.txt
sensevoice2jsonl \
++scp_file_list='["../../../data/list/train_wav.scp", "../../../data/list/train_text.txt"]' \
++data_type_list='["source", "target"]' \
++jsonl_file_out="../../../data/list/train.jsonl"

启动训练

注意修改 finetune.shtrain_tool 为你前面安装FunASR路径中funasr/bin/train_ds.py绝对路径

bash finetune.sh

WebUI

python webui.py

联系我们

如果您在使用中遇到问题可以直接在github页面提Issues。欢迎语音兴趣爱好者扫描以下的钉钉群二维码加入社区群进行交流和讨论。

FunAudioLLM FunASR