Commit
•
9a22524
1
Parent(s):
41c3322
Update README.md
Browse files
README.md
CHANGED
@@ -58,4 +58,47 @@ To transcribe audio files the model can be used as a standalone acoustic model a
|
|
58 |
# take argmax and decode
|
59 |
predicted_ids = torch.argmax(logits, dim=-1)
|
60 |
transcription = tokenizer.batch_decode(predicted_ids)
|
61 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# take argmax and decode
|
59 |
predicted_ids = torch.argmax(logits, dim=-1)
|
60 |
transcription = tokenizer.batch_decode(predicted_ids)
|
61 |
+
```
|
62 |
+
|
63 |
+
## Evaluation
|
64 |
+
|
65 |
+
This code snippet shows how to evaluate **facebook/wav2vec2-large-960h-lv60-self** on LibriSpeech's "clean" and "other" test data.
|
66 |
+
|
67 |
+
```python
|
68 |
+
from datasets import load_dataset
|
69 |
+
from transformers import Wav2Vec2ForMaskedLM, Wav2Vec2Tokenizer
|
70 |
+
import soundfile as sf
|
71 |
+
import torch
|
72 |
+
from jiwer import wer
|
73 |
+
|
74 |
+
|
75 |
+
librispeech_eval = load_dataset("librispeech_asr", "clean", split="test")
|
76 |
+
|
77 |
+
model = Wav2Vec2ForMaskedLM.from_pretrained("facebook/wav2vec2-large-960h-lv60-self").to("cuda")
|
78 |
+
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h-lv60-self")
|
79 |
+
|
80 |
+
def map_to_array(batch):
|
81 |
+
speech, _ = sf.read(batch["file"])
|
82 |
+
batch["speech"] = speech
|
83 |
+
return batch
|
84 |
+
|
85 |
+
librispeech_eval = librispeech_eval.map(map_to_array)
|
86 |
+
|
87 |
+
def map_to_pred(batch):
|
88 |
+
input_values = tokenizer(batch["speech"], return_tensors="pt", padding="longest").input_values
|
89 |
+
with torch.no_grad():
|
90 |
+
logits = model(input_values.to("cuda")).logits
|
91 |
+
|
92 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
93 |
+
transcription = tokenizer.batch_decode(predicted_ids)
|
94 |
+
batch["transcription"] = transcription
|
95 |
+
return batch
|
96 |
+
|
97 |
+
result = librispeech_eval.map(map_to_pred, batched=True, batch_size=16, remove_columns=["speech"])
|
98 |
+
|
99 |
+
print("WER:", wer(result["text"], result["transcription"]))
|
100 |
+
```
|
101 |
+
|
102 |
+
| "clean" | "other" |
|
103 |
+
|---|---|
|
104 |
+
| 2.2 | 5.2 |
|