Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: pt
|
3 |
+
datasets:
|
4 |
+
- CORAA
|
5 |
+
metrics:
|
6 |
+
- wer
|
7 |
+
tags:
|
8 |
+
- audio
|
9 |
+
- speech
|
10 |
+
- wav2vec2
|
11 |
+
- pt
|
12 |
+
- portuguese-speech-corpus
|
13 |
+
- automatic-speech-recognition
|
14 |
+
- speech
|
15 |
+
- PyTorch
|
16 |
+
license: apache-2.0
|
17 |
+
model-index:
|
18 |
+
- name: Edresson Casanova XLSR Wav2Vec2 Large 53 Portuguese
|
19 |
+
results:
|
20 |
+
- task:
|
21 |
+
name: Speech Recognition
|
22 |
+
type: automatic-speech-recognition
|
23 |
+
metrics:
|
24 |
+
- name: Test CORAA WER
|
25 |
+
type: wer
|
26 |
+
value: 25.26
|
27 |
+
---
|
28 |
+
|
29 |
+
# Wav2vec 2.0 trained with CORAA Portuguese Dataset
|
30 |
+
|
31 |
+
This a the demonstration of a fine-tuned Wav2vec model for Portuguese using the following [CORAA dataset](https://github.com/nilc-nlp/CORAA)
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
#### Transcription examples
|
36 |
+
|
37 |
+
| Text | Transcription |
|
38 |
+
|------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
|
39 |
+
| É comum os usuários confundirem software livre com software livre | É comum os __usuares__ __confunder em__ __softwerlivr__ com __softwerlivre__ |
|
40 |
+
| Ele fez tanto ghostwriting que ele começa a se sentir como um fantasma também | Ele fez tanto __golstraitn__ que ele __começou__ a se sentir como um fantasma também |
|
41 |
+
| Arnold apresentou um gráfico mostrando quantas cegonhas ele havia contado nos últimos dez anos | Arnold apresentou um gráfico mostrando quantas __segonhas__ ele havia contado nos últimos dez anos |
|
42 |
+
| Mais cedo ou mais tarde eles descobrirão como ler esses hieróglifos | Mais __sedo__ ou mais tarde eles descobriram como __de__ esses __ierogrôficos__ |
|
43 |
+
| Viver juntos compartilhar objetivos e ter um bom relacionamento | __E ver__ juntos __signafica__ viver juntos ou __fartlhar__ objetivos ter um bom __relacionamentoo__ |
|
44 |
+
| Da mesma forma uma patente pode impedir que concorrentes desenvolvam produtos similares | Da mesma forma uma patente pode impedir que concorrentes __desenvolva__ produtos similares |
|
45 |
+
| Duas mulheres e uma menina levantam com troféus | Duas mulheres e uma menina levantam com __trofés__ |
|
46 |
+
| Esse acrobata de circo deve ter um sistema vestibular bem treinado pensou o espectador | Esse acrobata de __cirko__ deve ter um sistema vestibular __bemtreinado__ pensou o espectador |
|
47 |
+
| Durante a exposição o tribunal pode fazer quaisquer perguntas ou esclarecimentos que considere apropriados | Durante a exposição o tribunal pode fazer quaisquer perguntas ou esclarecimentos que considere __apropriado__ |
|
48 |
+
|
49 |
+
## Imports and dependencies
|
50 |
+
|
51 |
+
|
52 |
+
```python
|
53 |
+
%%capture
|
54 |
+
!pip install datasets
|
55 |
+
!pip install jiwer
|
56 |
+
!pip install torchaudio
|
57 |
+
!pip install transformers
|
58 |
+
!pip install soundfile
|
59 |
+
```
|
60 |
+
|
61 |
+
|
62 |
+
```python
|
63 |
+
import torchaudio
|
64 |
+
from datasets import load_dataset, load_metric
|
65 |
+
from transformers import (
|
66 |
+
Wav2Vec2ForCTC,
|
67 |
+
Wav2Vec2Processor,
|
68 |
+
)
|
69 |
+
import torch
|
70 |
+
import re
|
71 |
+
import sys
|
72 |
+
```
|
73 |
+
|
74 |
+
## Preparation
|
75 |
+
|
76 |
+
|
77 |
+
```python
|
78 |
+
chars_to_ignore_regex = '[\,\?\.\!\;\:\"]' # noqa: W605
|
79 |
+
wer = load_metric("wer")
|
80 |
+
device = "cuda"
|
81 |
+
```
|
82 |
+
|
83 |
+
```python
|
84 |
+
model_name = 'Edresson/wav2vec2-large-xlsr-coraa-portuguese'
|
85 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
|
86 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
87 |
+
```
|
88 |
+
|
89 |
+
```python
|
90 |
+
def map_to_pred(batch):
|
91 |
+
features = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0], padding=True, return_tensors="pt")
|
92 |
+
input_values = features.input_values.to(device)
|
93 |
+
attention_mask = features.attention_mask.to(device)
|
94 |
+
with torch.no_grad():
|
95 |
+
logits = model(input_values, attention_mask=attention_mask).logits
|
96 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
97 |
+
batch["predicted"] = processor.batch_decode(pred_ids)
|
98 |
+
batch["predicted"] = [pred.lower() for pred in batch["predicted"]]
|
99 |
+
batch["target"] = batch["sentence"]
|
100 |
+
return batch
|
101 |
+
```
|
102 |
+
|
103 |
+
## Tests
|
104 |
+
|
105 |
+
For the results consult the [CORAA article](https://arxiv.org/abs/2110.15731)
|
106 |
+
|
107 |
+
### Example with Common Voice
|
108 |
+
|
109 |
+
|
110 |
+
```python
|
111 |
+
dataset = load_dataset("common_voice", "pt", split="test", data_dir="./cv-corpus-6.1-2020-12-11")
|
112 |
+
|
113 |
+
resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
|
114 |
+
|
115 |
+
def map_to_array(batch):
|
116 |
+
speech, _ = torchaudio.load(batch["path"])
|
117 |
+
batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
|
118 |
+
batch["sampling_rate"] = resampler.new_freq
|
119 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower().replace("’", "'")
|
120 |
+
return batch
|
121 |
+
```
|
122 |
+
|
123 |
+
```python
|
124 |
+
ds = dataset.map(map_to_array)
|
125 |
+
result = ds.map(map_to_pred, batched=True, batch_size=1, remove_columns=list(ds.features.keys()))
|
126 |
+
print(wer.compute(predictions=result["predicted"], references=result["target"]))
|
127 |
+
```
|