patrickvonplaten commited on
Commit
7f6699a
1 Parent(s): 3769631

correct readme

Browse files

Files changed (1) hide show
  1. README.md +107 -1
README.md CHANGED
@@ -1 +1,107 @@
1
- Work in Progess (WIP)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ datasets:
4
+ - librispeech_asr
5
+ tags:
6
+ - audio
7
+ - automatic-speech-recognition
8
+ license: apache-2.0
9
+ ---
10
+
11
+ # Wav2Vec2-Base-100h
12
+
13
+ [Facebook's Wav2Vec2](https://ai.facebook.com/blog/wav2vec-20-learning-the-structure-of-speech-from-raw-audio/)
14
+
15
+ The base model pretrained and fine-tuned on 100 hours of Librispeech on 16kHz sampled speech audio. When using the model
16
+ make sure that your speech input is also sampled at 16Khz.
17
+
18
+ [Paper](https://arxiv.org/abs/2006.11477)
19
+
20
+ Authors: Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli
21
+
22
+ **Abstract**
23
+
24
+ We show for the first time that learning powerful representations from speech audio alone followed by fine-tuning on transcribed speech can outperform the best semi-supervised methods while being conceptually simpler. wav2vec 2.0 masks the speech input in the latent space and solves a contrastive task defined over a quantization of the latent representations which are jointly learned. Experiments using all labeled data of Librispeech achieve 1.8/3.3 WER on the clean/other test sets. When lowering the amount of labeled data to one hour, wav2vec 2.0 outperforms the previous state of the art on the 100 hour subset while using 100 times less labeled data. Using just ten minutes of labeled data and pre-training on 53k hours of unlabeled data still achieves 4.8/8.2 WER. This demonstrates the feasibility of speech recognition with limited amounts of labeled data.
25
+
26
+ The original model can be found under https://github.com/pytorch/fairseq/tree/master/examples/wav2vec#wav2vec-20.
27
+
28
+
29
+ # Usage
30
+
31
+ To transcribe audio files the model can be used as a standalone acoustic model as follows:
32
+
33
+ ```python
34
+ from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
35
+ from datasets import load_dataset
36
+ import soundfile as sf
37
+ import torch
38
+
39
+ # load model and tokenizer
40
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-100h")
41
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-100h")
42
+
43
+ # define function to read in sound file
44
+ def map_to_array(batch):
45
+ speech, _ = sf.read(batch["file"])
46
+ batch["speech"] = speech
47
+ return batch
48
+
49
+ # load dummy dataset and read soundfiles
50
+ ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
51
+ ds = ds.map(map_to_array)
52
+
53
+ # tokenize
54
+ input_values = tokenizer(ds["speech"][:2], return_tensors="pt", padding="longest").input_values # Batch size 1
55
+
56
+ # retrieve logits
57
+ logits = model(input_values).logits
58
+
59
+ # take argmax and decode
60
+ predicted_ids = torch.argmax(logits, dim=-1)
61
+ transcription = tokenizer.batch_decode(predicted_ids)
62
+ ```
63
+
64
+ ## Evaluation
65
+
66
+ This code snippet shows how to evaluate **facebook/wav2vec2-base-100h** on LibriSpeech's "clean" and "other" test data.
67
+
68
+ ```python
69
+ from datasets import load_dataset
70
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
71
+ import soundfile as sf
72
+ import torch
73
+ from jiwer import wer
74
+
75
+
76
+ librispeech_eval = load_dataset("librispeech_asr", "clean", split="test")
77
+
78
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-100h").to("cuda")
79
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-100h")
80
+
81
+ def map_to_array(batch):
82
+ speech, _ = sf.read(batch["file"])
83
+ batch["speech"] = speech
84
+ return batch
85
+
86
+ librispeech_eval = librispeech_eval.map(map_to_array)
87
+
88
+ def map_to_pred(batch):
89
+ input_values = tokenizer(batch["speech"], return_tensors="pt", padding="longest").input_values
90
+ with torch.no_grad():
91
+ logits = model(input_values.to("cuda")).logits
92
+
93
+ predicted_ids = torch.argmax(logits, dim=-1)
94
+ transcription = tokenizer.batch_decode(predicted_ids)
95
+ batch["transcription"] = transcription
96
+ return batch
97
+
98
+ result = librispeech_eval.map(map_to_pred, batched=True, batch_size=16, remove_columns=["speech"])
99
+
100
+ print("WER:", wer(result["text"], result["transcription"]))
101
+ ```
102
+
103
+ *Result (WER)*:
104
+
105
+ | "clean" | "other" |
106
+ |---|---|
107
+ | 6.1 | 13.5 |