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

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
发送小信号
KoCLIP: Korean port of OpenAI CLIP, in Flax

KoCLIP This repository contains code for KoCLIP, a Korean port of OpenAI's CLIP. This project was conducted as part of Hugging Face's Flax/JAX communi

Jake Tae 100 Jan 02, 2023
Classification models 1D Zoo - Keras and TF.Keras

Classification models 1D Zoo - Keras and TF.Keras This repository contains 1D variants of popular CNN models for classification like ResNets, DenseNet

Roman Solovyev 12 Jan 06, 2023
ArtEmis: Affective Language for Art

ArtEmis: Affective Language for Art Created by Panos Achlioptas, Maks Ovsjanikov, Kilichbek Haydarov, Mohamed Elhoseiny, Leonidas J. Guibas Introducti

Panos 268 Dec 12, 2022
MlTr: Multi-label Classification with Transformer

MlTr: Multi-label Classification with Transformer This is official implement of "MlTr: Multi-label Classification with Transformer". Abstract The task

程星 38 Nov 08, 2022
🐦 Quickly annotate data from the comfort of your Jupyter notebook

🐦 pigeon - Quickly annotate data on Jupyter Pigeon is a simple widget that lets you quickly annotate a dataset of unlabeled examples from the comfort

Anastasis Germanidis 647 Jan 05, 2023
A human-readable PyTorch implementation of "Self-attention Does Not Need O(n^2) Memory"

memory_efficient_attention.pytorch A human-readable PyTorch implementation of "Self-attention Does Not Need O(n^2) Memory" (Rabe&Staats'21). def effic

Ryuichiro Hataya 7 Dec 26, 2022
DLFlow is a deep learning framework.

DLFlow是一套深度学习pipeline,它结合了Spark的大规模特征处理能力和Tensorflow模型构建能力。利用DLFlow可以快速处理原始特征、训练模型并进行大规模分布式预测,十分适合离线环境下的生产任务。利用DLFlow,用户只需专注于模型开发,而无需关心原始特征处理、pipeline构建、生产部署等工作。

DiDi 152 Oct 27, 2022
Bravia core script for python

Bravia-Core-Script You need to have a mandatory account If this L3 does not work, try another L3. enjoy

5 Dec 26, 2021
Personal implementation of paper "Approximate Nearest Neighbor Negative Contrastive Learning for Dense Text Retrieval"

Approximate Nearest Neighbor Negative Contrastive Learning for Dense Text Retrieval This repo provides personal implementation of paper Approximate Ne

John 8 Oct 07, 2022
一个目标检测的通用框架(不需要cuda编译),支持Yolo全系列(v2~v5)、EfficientDet、RetinaNet、Cascade-RCNN等SOTA网络。

一个目标检测的通用框架(不需要cuda编译),支持Yolo全系列(v2~v5)、EfficientDet、RetinaNet、Cascade-RCNN等SOTA网络。

Haoyu Xu 203 Jan 03, 2023
A TensorFlow implementation of SOFA, the Simulator for OFfline LeArning and evaluation.

SOFA This repository is the implementation of SOFA, the Simulator for OFfline leArning and evaluation. Keeping Dataset Biases out of the Simulation: A

22 Nov 23, 2022
DLL: Direct Lidar Localization

DLL: Direct Lidar Localization Summary This package presents DLL, a direct map-based localization technique using 3D LIDAR for its application to aeri

Service Robotics Lab 127 Dec 16, 2022
CryptoFrog - My First Strategy for freqtrade

cryptofrog-strategies CryptoFrog - My First Strategy for freqtrade NB: (2021-04-20) You'll need the latest freqtrade develop branch otherwise you migh

Robert Davey 137 Jan 01, 2023
Source code of AAAI 2022 paper "Towards End-to-End Image Compression and Analysis with Transformers".

Towards End-to-End Image Compression and Analysis with Transformers Source code of our AAAI 2022 paper "Towards End-to-End Image Compression and Analy

37 Dec 21, 2022
Instantaneous Motion Generation for Robots and Machines.

Ruckig Instantaneous Motion Generation for Robots and Machines. Ruckig generates trajectories on-the-fly, allowing robots and machines to react instan

Berscheid 374 Dec 23, 2022
🎃 Core identification module of AI powerful point reading system platform.

ppReader-Kernel Intro Core identification module of AI powerful point reading system platform. Usage 硬件: Windows10、GPU:nvdia GTX 1060 、普通RBG相机 软件: con

CrashKing 1 Jan 11, 2022
Implementation of "Bidirectional Projection Network for Cross Dimension Scene Understanding" CVPR 2021 (Oral)

Bidirectional Projection Network for Cross Dimension Scene Understanding CVPR 2021 (Oral) [ Project Webpage ] [ arXiv ] [ Video ] Existing segmentatio

Hu Wenbo 135 Dec 26, 2022
Code for Temporally Abstract Partial Models

Code for Temporally Abstract Partial Models Accompanies the code for the experimental section of the paper: Temporally Abstract Partial Models, Khetar

DeepMind 19 Jul 13, 2022
MolRep: A Deep Representation Learning Library for Molecular Property Prediction

MolRep: A Deep Representation Learning Library for Molecular Property Prediction Summary MolRep is a Python package for fairly measuring algorithmic p

AI-Health @NSCC-gz 83 Dec 24, 2022
ChineseBERT: Chinese Pretraining Enhanced by Glyph and Pinyin Information

ChineseBERT: Chinese Pretraining Enhanced by Glyph and Pinyin Information This repository contains code, model, dataset for ChineseBERT at ACL2021. Ch

413 Dec 01, 2022