模型:
facebook/wav2vec2-base-100h
该基础模型在16kHz采样率的Librispeech上进行了预训练和微调,共计100小时的语音音频。在使用该模型时,请确保输入的语音也是以16kHz进行采样。
作者:Alexei Baevski,Henry Zhou,Abdelrahman Mohamed,Michael Auli
摘要
我们首次展示了仅从语音音频中学习强大的表示,然后在转写语音上进行微调,可以超越最佳的半监督方法,而且概念上更简单。wav2vec 2.0在潜空间中屏蔽语音输入,并解决了在共同学习的量化的潜空间表示上定义的对比任务。在使用Librispeech的所有标注数据上进行的实验,clean/other测试集的识别错误率分别为1.8/3.3。当将标注数据减少到一小时时,wav2vec 2.0在100小时子集上的性能超过了先前的最新技术,同时使用了100倍少的标注数据。仅使用十分钟的标注数据,在53000小时的未标注数据上进行预训练的结果仍然达到了4.8/8.2的识别错误率。这证明了在有限的标注数据情况下进行语音识别的可行性。
原始模型可以在 https://github.com/pytorch/fairseq/tree/master/examples/wav2vec#wav2vec-20 处找到。
要转录音频文件,可以将该模型作为独立的声学模型使用,如下所示:
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC from datasets import load_dataset import soundfile as sf import torch # load model and processor processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-100h") model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-100h") # define function to read in sound file def map_to_array(batch): speech, _ = sf.read(batch["file"]) batch["speech"] = speech return batch # load dummy dataset and read soundfiles ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation") ds = ds.map(map_to_array) # tokenize input_values = processor(ds[0]["audio"]["array"], return_tensors="pt", padding="longest").input_values # Batch size 1 # retrieve logits logits = model(input_values).logits # take argmax and decode predicted_ids = torch.argmax(logits, dim=-1) transcription = processor.batch_decode(predicted_ids)
以下代码片段展示了如何对facebook/wav2vec2-base-100h在LibriSpeech的"clean"和"other"测试数据上进行评估。
from datasets import load_dataset from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor import soundfile as sf import torch from jiwer import wer librispeech_eval = load_dataset("librispeech_asr", "clean", split="test") model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-100h").to("cuda") processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-100h") def map_to_pred(batch): input_values = processor(batch["audio"]["array"], return_tensors="pt", padding="longest").input_values with torch.no_grad(): logits = model(input_values.to("cuda")).logits predicted_ids = torch.argmax(logits, dim=-1) transcription = processor.batch_decode(predicted_ids) batch["transcription"] = transcription return batch result = librispeech_eval.map(map_to_pred, batched=True, batch_size=1, remove_columns=["speech"]) print("WER:", wer(result["text"], result["transcription"]))
结果(识别错误率):
"clean" | "other" |
---|---|
6.1 | 13.5 |