patrickvonplaten
commited on
Commit
•
90a6d44
1
Parent(s):
a5df75d
Update README.md
Browse files
README.md
CHANGED
@@ -59,4 +59,47 @@ To transcribe audio files the model can be used as a standalone acoustic model a
|
|
59 |
# take argmax and decode
|
60 |
predicted_ids = torch.argmax(logits, dim=-1)
|
61 |
transcription = tokenizer.batch_decode(predicted_ids)
|
62 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-large-960h** on LibriSpeech's "clean" and "other" test data.
|
67 |
+
|
68 |
+
```python
|
69 |
+
from datasets import load_dataset
|
70 |
+
from transformers import Wav2Vec2ForMaskedLM, 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 = Wav2Vec2ForMaskedLM.from_pretrained("facebook/wav2vec2-large-960h").to("cuda")
|
79 |
+
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
|
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 |
+
| "clean" | "other" |
|
104 |
+
|---|---|
|
105 |
+
| 3.0 | 6.8 |
|