Toshiki Tomihira commited on
Commit
4213eef
1 Parent(s): bbd27e7

First version of the wav2vec2.0 base 960h model and tokenizer

Browse files
Files changed (6) hide show
  1. README.md +112 -0
  2. config.json +55 -0
  3. pytorch_model.bin +3 -0
  4. special_tokens_map.json +1 -0
  5. tokenizer_config.json +1 -0
  6. vocab.json +1 -0
README.md ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ datasets:
4
+ - librispeech_asr
5
+ tags:
6
+ - audio
7
+ - automatic-speech-recognition
8
+ license: apache-2.0
9
+ widget:
10
+ - label: Librispeech sample 1
11
+ src: https://cdn-media.huggingface.co/speech_samples/sample1.flac
12
+ - label: Librispeech sample 2
13
+ src: https://cdn-media.huggingface.co/speech_samples/sample2.flac
14
+ ---
15
+
16
+ # Wav2Vec2-Base-960h
17
+
18
+ [Facebook's Wav2Vec2](https://ai.facebook.com/blog/wav2vec-20-learning-the-structure-of-speech-from-raw-audio/)
19
+
20
+ The base model pretrained and fine-tuned on 960 hours of Librispeech on 16kHz sampled speech audio. When using the model
21
+ make sure that your speech input is also sampled at 16Khz.
22
+
23
+ [Paper](https://arxiv.org/abs/2006.11477)
24
+
25
+ Authors: Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli
26
+
27
+ **Abstract**
28
+
29
+ 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.
30
+
31
+ The original model can be found under https://github.com/pytorch/fairseq/tree/master/examples/wav2vec#wav2vec-20.
32
+
33
+
34
+ # Usage
35
+
36
+ To transcribe audio files the model can be used as a standalone acoustic model as follows:
37
+
38
+ ```python
39
+ from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
40
+ from datasets import load_dataset
41
+ import soundfile as sf
42
+ import torch
43
+
44
+ # load model and tokenizer
45
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
46
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
47
+
48
+ # define function to read in sound file
49
+ def map_to_array(batch):
50
+ speech, _ = sf.read(batch["file"])
51
+ batch["speech"] = speech
52
+ return batch
53
+
54
+ # load dummy dataset and read soundfiles
55
+ ds = load_dataset("patrickvonplaten/librispeech_asr_dummy", "clean", split="validation")
56
+ ds = ds.map(map_to_array)
57
+
58
+ # tokenize
59
+ input_values = tokenizer(ds["speech"][:2], return_tensors="pt", padding="longest").input_values # Batch size 1
60
+
61
+ # retrieve logits
62
+ logits = model(input_values).logits
63
+
64
+ # take argmax and decode
65
+ predicted_ids = torch.argmax(logits, dim=-1)
66
+ transcription = tokenizer.batch_decode(predicted_ids)
67
+ ```
68
+
69
+ ## Evaluation
70
+
71
+ This code snippet shows how to evaluate **facebook/wav2vec2-base-960h** on LibriSpeech's "clean" and "other" test data.
72
+
73
+ ```python
74
+ from datasets import load_dataset
75
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
76
+ import soundfile as sf
77
+ import torch
78
+ from jiwer import wer
79
+
80
+
81
+ librispeech_eval = load_dataset("librispeech_asr", "clean", split="test")
82
+
83
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h").to("cuda")
84
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
85
+
86
+ def map_to_array(batch):
87
+ speech, _ = sf.read(batch["file"])
88
+ batch["speech"] = speech
89
+ return batch
90
+
91
+ librispeech_eval = librispeech_eval.map(map_to_array)
92
+
93
+ def map_to_pred(batch):
94
+ input_values = tokenizer(batch["speech"], return_tensors="pt", padding="longest").input_values
95
+ with torch.no_grad():
96
+ logits = model(input_values.to("cuda")).logits
97
+
98
+ predicted_ids = torch.argmax(logits, dim=-1)
99
+ transcription = tokenizer.batch_decode(predicted_ids)
100
+ batch["transcription"] = transcription
101
+ return batch
102
+
103
+ result = librispeech_eval.map(map_to_pred, batched=True, batch_size=1, remove_columns=["speech"])
104
+
105
+ print("WER:", wer(result["text"], result["transcription"]))
106
+ ```
107
+
108
+ *Result (WER)*:
109
+
110
+ | "clean" | "other" |
111
+ |---|---|
112
+ | 3.4 | 8.6 |
config.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Wav2Vec2ForCTC"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 1,
7
+ "conv_bias": false,
8
+ "conv_dim": [
9
+ 512,
10
+ 512,
11
+ 512,
12
+ 512,
13
+ 512,
14
+ 512,
15
+ 512
16
+ ],
17
+ "conv_kernel": [
18
+ 10,
19
+ 3,
20
+ 3,
21
+ 3,
22
+ 3,
23
+ 2,
24
+ 2
25
+ ],
26
+ "conv_stride": [
27
+ 5,
28
+ 2,
29
+ 2,
30
+ 2,
31
+ 2,
32
+ 2,
33
+ 2
34
+ ],
35
+ "do_stable_layer_norm": false,
36
+ "eos_token_id": 2,
37
+ "feat_extract_activation": "gelu",
38
+ "feat_extract_dropout": 0.0,
39
+ "feat_extract_norm": "group",
40
+ "hidden_act": "gelu",
41
+ "hidden_dropout_prob": 0.1,
42
+ "hidden_size": 768,
43
+ "initializer_range": 0.02,
44
+ "intermediate_size": 3072,
45
+ "layer_norm_eps": 1e-05,
46
+ "model_type": "wav2vec2",
47
+ "num_attention_heads": 12,
48
+ "num_conv_pos_embedding_groups": 16,
49
+ "num_conv_pos_embeddings": 128,
50
+ "num_feat_extract_layers": 7,
51
+ "num_hidden_layers": 12,
52
+ "pad_token_id": 0,
53
+ "transformers_version": "4.3.2",
54
+ "vocab_size": 32
55
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1f24291a5a03b0a905da3ea61278d28760022b24c6a10cb42fea85ea942c45d
3
+ size 377667909
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
1
+ {"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>"}
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
1
+ {"unk_token": "<unk>", "bos_token": "<s>", "eos_token": "</s>", "pad_token": "<pad>", "do_lower_case": false, "return_attention_mask": false, "do_normalize": true}
vocab.json ADDED
@@ -0,0 +1 @@
 
1
+ {"<pad>": 0, "<s>": 1, "</s>": 2, "<unk>": 3, "|": 4, "E": 5, "T": 6, "A": 7, "O": 8, "N": 9, "I": 10, "H": 11, "S": 12, "R": 13, "D": 14, "L": 15, "U": 16, "M": 17, "W": 18, "C": 19, "F": 20, "G": 21, "Y": 22, "P": 23, "B": 24, "V": 25, "K": 26, "'": 27, "X": 28, "J": 29, "Q": 30, "Z": 31}