中文语音识别系列,读者可以借助它快速训练属于自己的中文语音识别模型,或直接使用预训练模型测试效果。

Overview

MASR中文语音识别(pytorch版)

  • 开箱即用
  • 自行训练
  • 使用与训练分离(增量训练)
  • 识别率高

说明:因为每个人电脑机器不同,而且有些安装包安装起来比较麻烦,强烈建议直接用我编译好的docker环境跑 目前docker基础环境为ubuntu-cuda10.1-cudnn7-pytorch1.6.0(电脑没有GPU也能跑~)

docker镜像下载地址,可以直接用命令

docker pull binzhouchn/masr:1.6.0-cuda10.1-cudnn7

当然如果你不想用docker也行,有些包的安装解决方案我已给出
本项目是受masr项目启发重新创建的更加完善并且能直接跑的语音识别项目,环境、数据及数据预处理代码、网络结构及预训练模型都很全

原理

MASR使用的是门控卷积神经网络(Gated Convolutional Network),网络结构类似于Facebook在2016年提出的Wav2letter。但是使用的激活函数不是ReLU或者是HardTanh,而是GLU(门控线性单元)。因此称作门控卷积网络。使用GLU的收敛速度比HardTanh要快。如果你想要研究卷积网络用于语音识别的效果,这个项目可以作为一个参考 以下用字错误率CER来衡量模型的表现,CER=编辑距离/句子长度,越低越好
大致可以理解为1-CER就是识别准确率
模型使用AISHELL-1数据集训练,共178小时的录音,覆盖了4300多个汉字。工业界使用的语音识别系统通常使用至少10倍于本项目的录音数据来训练,同时使用特定场景的语料来训练语言模型,所以和工业界大型企业的识别效果还有些差距。

train.png
上图为验证集的cer随epoch的训练曲线

参考项目地址链接(原项目作者貌似不再维护,预训练模型存在无法下载和代码无法直接跑的问题)

https://github.com/nl8590687/ASRT_SpeechRecognition
https://github.com/nobody132/masr
https://github.com/xxbb1234021/speech_recognition

依赖包

torch==1.6.0
Levenshtein==0.12.0
librosa==0.8.0
warpctc_pytorch==0.2.1
tensorboardX==2.1
ctcdecode==1.0.2
pycorrector==0.3.0 #Chinese Text Error Corrector
sounddevice==0.4.1
pyaudio==0.2.11
Flask==1.1.2
Flask-Cors==3.0.9
tqdm==4.50.2
joblib==1.0.0
werkzeug==1.0.0
gunicorn==20.0.4

----pyaudio安装比较麻烦,解决方案----

#linux下参考

apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0 portaudio19-dev python-all-dev python3-all-dev
pip install pyaudio

#mac下参考

brew update
brew install portaudio
brew link --overwrite portaudio
pip install pyaudio

----ctcdecode安装解决方案----

git clone --recursive https://github.com/parlance/ctcdecode.git
cd ctcdecode
pip install .

如果你是在国外以上语句没有问题,如果不是则需要单独下载openfst-1.6.7.tar.gzboost_1_67_0.tar.gz,解压到third_party目录下,然后setup.py中注释掉以下这两行代码

download_extract('https://sites.google.com/site/openfst/home/openfst-down/openfst-1.6.7.tar.gz',
                 'third_party/openfst-1.6.7.tar.gz')
download_extract('https://dl.bintray.com/boostorg/release/1.67.0/source/boost_1_67_0.tar.gz',
                 'third_party/boost_1_67_0.tar.gz')

再运行pip install .,即可成功~

----导入soundfile时提示 OSError: sndfile library not found----

apt-get install libsndfile1

数据和模型下载地址

AISHELL-1数据集下载
LM语言模型zh_giga.no_cna_cmn.prune01244.klm下载
预训练模型model_cer0_19_l15_pytorch.pth下载

AISHELL-1数据集下载后解压放到data_aishell对应文件夹下

原始数据预处理

预处理方式一

你可以使用自己的数据集来训练模型。你的数据集需要包含至少以下3个文件:

  • train.index
  • dev.index
  • labels.json

train.index和dev.index为索引文件,表示音频文件和标注的对应关系,应具有如下的简单格式:

/path/to/audio/file0.wav,数据预处理
/path/to/audio/file1.wav,小时不识月
...

labels.gz是pkle文件,应包含数据集标注中出现过的所有字符,表示为一个list数组。其中开头首字符必须是无效字符(可任意指定,不和其他字符重复就行),预留给CTC作为blank label;建议索引0为'_',索引28位' '

[
   '_', // 第一个字符表示CTC空字符,可以随便设置,但不要和其他字符重复。
   '小',
   '时',
   '不',
   '识',
   '月',
   ...
]

处理好后放到data_aishell目录下

预处理方式二

train.index和dev.index为索引文件,表示音频文件和标注的对应关系,应具有如下的简单格式:

/path/to/audio/file0.wav,数据 预 处理
/path/to/audio/file1.wav,小时 不识 月
...

labels.gz是pkle文件,应包含数据集标注中出现过的所有字符,表示为一个list数组。其中开头首字符必须是无效字符(可任意指定,不和其他字符重复就行),预留给CTC作为blank label;建议索引0为'_',索引28位' '

[
   '_', // 第一个字符表示CTC空字符,可以随便设置,但不要和其他字符重复。
   '小时',
   '不识',
   '月',
    '预',
    '处理'
   ...
]

注:如果是方式二处理,则data.py中MASRDataset类读取数据后处理的方式要有所改动
原始数据集AISHELL-1已经给我们分好词,也可以自行用jieba分词

数据预处理代码notebook

模型训练(GPU)

氪金玩家可自行训练,目前单卡V100跑每个epoch需要30min

sudo nvidia-docker run -v $PWD/masr:/workspace/masr  -w /workspace/masr binzhouchn/masr:1.6.0-cuda10.1-cudnn7 python -u train.py

也可以用我训练好的模型(并不是训练最好的),下载地址
【*目前SOTA预训练模型可私信向我要】

模型预测(CPU)

如果你是用GPU进行预测,则需要改动models/base.py代码,torch.load去掉map_location参数

预测方式一

可以跑examples下的demo-recognize.py;
不过推荐跑beamdecode.py,CTCBeam解压更加准确,记得修改要识别的wav文件地址

预测方式二

起flask服务进行预测

sudo docker run -d -p 5005:5005 -v $PWD/masr:/workspace/masr  -w /workspace/masr binzhouchn/masr:1.6.0-cuda10.1-cudnn7 gunicorn -b :5005 masr_server:app

录制自己的声音

cd examples
python demo-record-recognize.py

直接跑examples中的demo-record-recognize.py,可以在看到提示「录音中」后开始说话,你有5秒钟的说话时间(可以自己在源码中更改)

使用声学嵌入寻找音近字

你可能听说过词嵌入(word embedding)。作为神经语言模型的底层,词嵌入可以将词映射到一个低维连续空间中,并且使意思相近或相关的词有着较近的cosine距离,通过比较这个距离,可以找出一个词的近义词
我们使用类似的思想将汉字嵌入到一个(较)低维的连续空间中,并使读音相近的汉字有着相近的cosine距离,并根据cosine距离来寻找音近字。如图,我们找到了嵌入空间中离「掉」最近的5个字,它们的读音也确实与「掉」接近

使用MASR获取声学嵌入

使用MASR的预训练模型,我们就能构造出这样一个声学嵌入(acoustic embedding)来。

在MASR的输出层有4335个单元,分别表示4334个汉字(严格地说不完全是汉字)和1个CTC的特殊标记。

在输出层之前有一个隐藏层,包含1000个单元。

连接他们的是一个$4335 \times 1000$的矩阵$W_{ij}$,对于第$i$个汉字,使用向量$W_i$作为它的嵌入向量即可。

使用声学嵌入给了我们直观判断一个声学模型是否合理的方式,你会发现,大多数情况下,MASR的预训练模型给出的音近字都是较为准确的。

自己试试

执行以下文件,多试几个汉字,看准不准。

cd examples
python embedding.py

FURTHER

  1. 不是很推荐pyaudio+wave这个组合,可以自行换掉,用sounddevice+scipy(scipy.io.wavfile)或者pysoundfile
  2. 自行增量训练并更换GatedConv模型以达到更好工业级效果
  3. tensorflow重写[TODO]
  4. 欢迎在dev分支提交优化本项目
You might also like...
Comments
  • 请问如何加载预训练模型

    请问如何加载预训练模型

    `

    if name == "main": vocabulary = joblib.load(LABEL_PATH) vocabulary = "".join(vocabulary) model = GatedConv(vocabulary) pretrained = torch.load("pretrained/model_49.pth") pretrain_dict = pretrained.state_dict() new_state_dict = OrderedDict() for k, v in pretrain_dict.items(): name = k[7:] new_state_dict[name] = v model = model.load_state_dict(new_state_dict) model = nn.DataParallel(model) model.cuda() train(model)

    ` 这是我的代码,加载完模型字典后模型参数都丢失了 image

    image

    opened by csbbbv 0
  • ctcdecode的安装和使用问题

    ctcdecode的安装和使用问题

    我想运行beamdecode.py,但是这需要安装ctcdecode。很多次安装失败后,我发现把pytorch降级成1.0的版本后,ctcdecode 0.4就安装成功了,但是再运行beamdecode.py的时候,又报错说pytorch的版本不对应。当我把pytorch升级到3.6以上时,ctcdecode又开始报错了。请问有人成功运行过ctcdecode这个包吗?

    opened by psuu0001 1
  • LM语言模型与预训练模型的关系

    LM语言模型与预训练模型的关系

    train.py中训练得到的结果对应的是pretrained目录下预训练模型的结果。 LM语言是使用在beamcode.py中的decoder的部分,我并没有看到是如何得到的。 但是LM语言模型在train.py的过程中并没有使用,请问预训练模型与LM语言模型之间是否是一一对应的关系,比如我换了相应的预训练模型我的LM语言模型是否也要发生改变? 或者我的预训练模型不变是否可以直接换成你的70G的LM模型? 希望能得到你的解答

    opened by im73 0
  • 使用Docker无法运行

    使用Docker无法运行

    1.运行环境 win10+docker desktop 3.2.2

    2.运行步骤 1.下载image

    docker pull binzhouchn/masr:1.6.0-cuda10.1-cudnn7

    2.运行项目: docker run -d -p 5005:5005 -v C:\Users\xiaow\Desktop\a\masr:/workspace/masr -w /workspace/masr binzhouchn/masr:1.6.0-cuda10.1-cudnn7 gunicorn -b :5005 masr_server:app

    因为使用windows 运行,去掉sudo,同时去掉$pwd,改为绝对路径

    3.错误结果如图 image

    opened by BleethNie 2
Releases(v1.1.0)
Owner
发送小信号
wechat/qq: 5493897
发送小信号
Official Pytorch implementation of "Learning to Estimate Robust 3D Human Mesh from In-the-Wild Crowded Scenes", CVPR 2022

Learning to Estimate Robust 3D Human Mesh from In-the-Wild Crowded Scenes / 3DCrowdNet News 💪 3DCrowdNet achieves the state-of-the-art accuracy on 3D

Hongsuk Choi 113 Dec 21, 2022
Edison AT is software Depression Assistant personal.

Edison AT Edison AT is software / program Depression Assistant personal. Feature: Analyze emotional real-time from face. Audio Edison(Comingsoon relea

Ananda Rauf 2 Apr 24, 2022
This repo contains the pytorch implementation for Dynamic Concept Learner (accepted by ICLR 2021).

DCL-PyTorch Pytorch implementation for the Dynamic Concept Learner (DCL). More details can be found at the project page. Framework Grounding Physical

Zhenfang Chen 31 Jan 06, 2023
Defense-GAN: Protecting Classifiers Against Adversarial Attacks Using Generative Models (published in ICLR2018)

Defense-GAN: Protecting Classifiers Against Adversarial Attacks Using Generative Models Pouya Samangouei*, Maya Kabkab*, Rama Chellappa [*: authors co

Maya Kabkab 212 Dec 07, 2022
AdelaiDepth is an open source toolbox for monocular depth prediction.

AdelaiDepth is an open source toolbox for monocular depth prediction.

Adelaide Intelligent Machines (AIM) Group 743 Jan 01, 2023
Human POSEitioning System (HPS): 3D Human Pose Estimation and Self-localization in Large Scenes from Body-Mounted Sensors, CVPR 2021

Human POSEitioning System (HPS): 3D Human Pose Estimation and Self-localization in Large Scenes from Body-Mounted Sensors Human POSEitioning System (H

Aymen Mir 66 Dec 21, 2022
Customer Segmentation using RFM

Customer-Segmentation-using-RFM İş Problemi Bir e-ticaret şirketi müşterilerini segmentlere ayırıp bu segmentlere göre pazarlama stratejileri belirlem

Nazli Sener 7 Dec 26, 2021
Simple transformer model for CIFAR10

CIFAR-Transformer Simple transformer model for CIFAR10. Reference: https://www.tensorflow.org/text/tutorials/transformer https://github.com/huggingfac

9 Nov 07, 2022
Facestar dataset. High quality audio-visual recordings of human conversational speech.

Facestar Dataset Description Existing audio-visual datasets for human speech are either captured in a clean, controlled environment but contain only a

Meta Research 87 Dec 21, 2022
ICCV2021 - A New Journey from SDRTV to HDRTV.

ICCV2021 - A New Journey from SDRTV to HDRTV.

XyChen 82 Dec 27, 2022
Pytorch code for "DPFM: Deep Partial Functional Maps" - 3DV 2021 (Oral)

DPFM Code for "DPFM: Deep Partial Functional Maps" - 3DV 2021 (Oral) Installation This implementation runs on python = 3.7, use pip to install depend

Souhaib Attaiki 29 Oct 03, 2022
A Jupyter notebook to play with NVIDIA's StyleGAN3 and OpenAI's CLIP for a text-based guided image generation.

A Jupyter notebook to play with NVIDIA's StyleGAN3 and OpenAI's CLIP for a text-based guided image generation.

Eugenio Herrera 175 Dec 29, 2022
Deep learning (neural network) based remote photoplethysmography: how to extract pulse signal from video using deep learning tools

Deep-rPPG: Camera-based pulse estimation using deep learning tools Deep learning (neural network) based remote photoplethysmography: how to extract pu

Terbe Dániel 138 Dec 17, 2022
Multi-Joint dynamics with Contact. A general purpose physics simulator.

MuJoCo Physics MuJoCo stands for Multi-Joint dynamics with Contact. It is a general purpose physics engine that aims to facilitate research and develo

DeepMind 5.2k Jan 02, 2023
House-GAN++: Generative Adversarial Layout Refinement Network towards Intelligent Computational Agent for Professional Architects

House-GAN++ Code and instructions for our paper: House-GAN++: Generative Adversarial Layout Refinement Network towards Intelligent Computational Agent

122 Dec 28, 2022
一个多语言支持、易使用的 OCR 项目。An easy-to-use OCR project with multilingual support.

AgentOCR 简介 AgentOCR 是一个基于 PaddleOCR 和 ONNXRuntime 项目开发的一个使用简单、调用方便的 OCR 项目 本项目目前包含 Python Package 【AgentOCR】 和 OCR 标注软件 【AgentOCRLabeling】 使用指南 Pytho

AgentMaker 98 Nov 10, 2022
Official PyTorch Implementation for InfoSwap: Information Bottleneck Disentanglement for Identity Swapping

InfoSwap: Information Bottleneck Disentanglement for Identity Swapping Code usage Please check out the user manual page. Paper Gege Gao, Huaibo Huang,

Grace Hešeri 56 Dec 20, 2022
Official code for "Mean Shift for Self-Supervised Learning"

MSF Official code for "Mean Shift for Self-Supervised Learning" Requirements Python = 3.7.6 PyTorch = 1.4 torchvision = 0.5.0 faiss-gpu = 1.6.1 In

UMBC Vision 44 Nov 21, 2022
TorchFlare is a simple, beginner-friendly, and easy-to-use PyTorch Framework train your models effortlessly.

TorchFlare TorchFlare is a simple, beginner-friendly and an easy-to-use PyTorch Framework train your models without much effort. It provides an almost

Atharva Phatak 85 Dec 26, 2022
A Python library that provides a simplified alternative to DBAPI 2

A Python library that provides a simplified alternative to DBAPI 2. It provides a facade in front of DBAPI 2 drivers.

Tony Locke 44 Nov 17, 2021