patrickvonplaten
commited on
Commit
•
d396308
1
Parent(s):
51a82c0
Update README.md
Browse files
README.md
CHANGED
@@ -9,4 +9,55 @@ tags:
|
|
9 |
license: apache-2.0
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
license: apache-2.0
|
10 |
---
|
11 |
|
12 |
+
## Evaluation on Common Voice FR Test
|
13 |
+
|
14 |
+
```python
|
15 |
+
import torchaudio
|
16 |
+
from datasets import load_dataset, load_metric
|
17 |
+
from transformers import (
|
18 |
+
Wav2Vec2ForCTC,
|
19 |
+
Wav2Vec2Processor,
|
20 |
+
)
|
21 |
+
import torch
|
22 |
+
import re
|
23 |
+
import sys
|
24 |
+
|
25 |
+
model_name = "facebook/wav2vec2-large-xlsr-53-french"
|
26 |
+
device = "cuda"
|
27 |
+
|
28 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"]' # noqa: W605
|
29 |
+
|
30 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
|
31 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
32 |
+
|
33 |
+
ds = load_dataset("common_voice", "fr", split="test", data_dir="./cv-corpus-6.1-2020-12-11")
|
34 |
+
|
35 |
+
resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
|
36 |
+
|
37 |
+
def map_to_array(batch):
|
38 |
+
speech, _ = torchaudio.load(batch["path"])
|
39 |
+
batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
|
40 |
+
batch["sampling_rate"] = resampler.new_freq
|
41 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace("’", "'")
|
42 |
+
return batch
|
43 |
+
|
44 |
+
ds = ds.map(map_to_array)
|
45 |
+
|
46 |
+
def map_to_pred(batch):
|
47 |
+
features = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0], padding=True, return_tensors="pt")
|
48 |
+
input_values = features.input_values.to(device)
|
49 |
+
attention_mask = features.attention_mask.to(device)
|
50 |
+
with torch.no_grad():
|
51 |
+
logits = model(input_values, attention_mask=attention_mask).logits
|
52 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
53 |
+
batch["predicted"] = processor.batch_decode(pred_ids)
|
54 |
+
batch["target"] = batch["sentence"]
|
55 |
+
return batch
|
56 |
+
|
57 |
+
result = ds.map(map_to_pred, batched=True, batch_size=16, remove_columns=list(ds.features.keys()))
|
58 |
+
|
59 |
+
wer = load_metric("wer")
|
60 |
+
|
61 |
+
print(wer.compute(predictions=result["predicted"], references=result["target"]))
|
62 |
+
```
|
63 |
+
**Result**: 25.2 %
|