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