Update README.md
Browse filesAdd an example of prediction.
README.md
CHANGED
@@ -26,8 +26,39 @@ model-index:
|
|
26 |
# Wav2Vec2-Large-XLSR-53-Breton
|
27 |
The model can be used directly (without a language model) as follows:
|
28 |
```python
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
```
|
|
|
|
|
|
|
31 |
|
32 |
The model can be evaluated as follows on the {language} test data of Common Voice.
|
33 |
```python
|
@@ -45,7 +76,7 @@ model = Wav2Vec2ForCTC.from_pretrained('Marxav/wav2vec2-large-xlsr-53-breton')
|
|
45 |
model.to("cuda")
|
46 |
|
47 |
|
48 |
-
chars_to_ignore_regex = '[
|
49 |
|
50 |
def remove_special_characters(batch):
|
51 |
sentence = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
|
|
|
26 |
# Wav2Vec2-Large-XLSR-53-Breton
|
27 |
The model can be used directly (without a language model) as follows:
|
28 |
```python
|
29 |
+
import torch
|
30 |
+
import torchaudio
|
31 |
+
from datasets import load_dataset
|
32 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
33 |
+
lang = "br"
|
34 |
+
test_dataset = load_dataset("common_voice", lang, split="test[:2%]")
|
35 |
+
|
36 |
+
processor = Wav2Vec2Processor.from_pretrained("Marxav/wav2vec2-large-xlsr-53-breton")
|
37 |
+
model = Wav2Vec2ForCTC.from_pretrained("Marxav/wav2vec2-large-xlsr-53-breton")
|
38 |
+
|
39 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
40 |
+
|
41 |
+
# Preprocessing the datasets.
|
42 |
+
# We need to read the aduio files as arrays
|
43 |
+
def speech_file_to_array_fn(batch):
|
44 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
45 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
46 |
+
return batch
|
47 |
+
|
48 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
49 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
50 |
+
|
51 |
+
with torch.no_grad():
|
52 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
53 |
+
|
54 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
55 |
+
|
56 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
57 |
+
print("Reference:", test_dataset["sentence"][:2])
|
58 |
```
|
59 |
+
The above code leads to the following prediction for the first two samples:
|
60 |
+
* Prediction: ["nel ler ket dont abenn eus netra la vez ser mirc'hid evel sij", 'an eil hag egile']
|
61 |
+
* Reference: ['"N\'haller ket dont a-benn eus netra pa vezer nec\'het evel-se."', 'An eil hag egile.']
|
62 |
|
63 |
The model can be evaluated as follows on the {language} test data of Common Voice.
|
64 |
```python
|
|
|
76 |
model.to("cuda")
|
77 |
|
78 |
|
79 |
+
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“\\%\\‘\\”\\�\\'\\(\\)\\/\\«\\»\\½\\…]'
|
80 |
|
81 |
def remove_special_characters(batch):
|
82 |
sentence = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
|