Update README.md
Browse files
README.md
CHANGED
@@ -1,9 +1,78 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import torch
|
8 |
import torchaudio
|
9 |
from datasets import load_dataset, load_metric
|
@@ -12,7 +81,7 @@ import re
|
|
12 |
import argparse
|
13 |
|
14 |
lang_id = "zh-HK"
|
15 |
-
model_id = "
|
16 |
|
17 |
parser = argparse.ArgumentParser(description='hanles checkpoint loading')
|
18 |
parser.add_argument('--checkpoint', type=str, default=None)
|
@@ -59,27 +128,15 @@ result = test_dataset.map(evaluate, batched=True, batch_size=16)
|
|
59 |
print("CER: {:2f}".format(100 * cer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
60 |
```
|
61 |
|
62 |
-
Character Error Rate implementation
|
63 |
|
64 |
-
|
|
|
|
|
65 |
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
66 |
class CER(datasets.Metric):
|
67 |
def _info(self):
|
68 |
-
|
69 |
-
description=_DESCRIPTION,
|
70 |
-
citation=_CITATION,
|
71 |
-
inputs_description=_KWARGS_DESCRIPTION,
|
72 |
-
features=datasets.Features(
|
73 |
-
{
|
74 |
-
"predictions": datasets.Value("string", id="sequence"),
|
75 |
-
"references": datasets.Value("string", id="sequence"),
|
76 |
-
}
|
77 |
-
),
|
78 |
-
codebase_urls=["https://github.com/jitsi/jiwer/"],
|
79 |
-
reference_urls=[
|
80 |
-
"https://en.wikipedia.org/wiki/Word_error_rate",
|
81 |
-
],
|
82 |
-
)
|
83 |
|
84 |
def _compute(self, predictions, references):
|
85 |
preds = [char for seq in predictions for char in list(seq)]
|
@@ -87,4 +144,12 @@ class CER(datasets.Metric):
|
|
87 |
return wer(refs, preds)
|
88 |
```
|
89 |
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
language: zh-HK
|
2 |
+
datasets:
|
3 |
+
- common_voice
|
4 |
+
metrics:
|
5 |
+
- cer
|
6 |
+
tags:
|
7 |
+
- audio
|
8 |
+
- automatic-speech-recognition
|
9 |
+
- speech
|
10 |
+
- xlsr-fine-tuning-week
|
11 |
+
license: apache-2.0
|
12 |
+
model-index:
|
13 |
+
- name: wav2vec2-large-xlsr-cantonese
|
14 |
+
results:
|
15 |
+
- task:
|
16 |
+
name: Speech Recognition
|
17 |
+
type: automatic-speech-recognition
|
18 |
+
dataset:
|
19 |
+
name: Common Voice zh-HK
|
20 |
+
type: common_voice
|
21 |
+
args: zh-HK
|
22 |
+
metrics:
|
23 |
+
- name: Test CER
|
24 |
+
type: cer
|
25 |
+
value: 17.81
|
26 |
+
---
|
27 |
+
|
28 |
+
# Wav2Vec2-Large-XLSR-53-Cantonese
|
29 |
+
|
30 |
+
Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Cantonese using the [Common Voice](https://huggingface.co/datasets/common_voice).
|
31 |
+
When using this model, make sure that your speech input is sampled at 16kHz.
|
32 |
+
|
33 |
+
## Usage
|
34 |
+
|
35 |
+
The model can be used directly (without a language model) as follows:
|
36 |
+
|
37 |
+
```python
|
38 |
+
import torch
|
39 |
+
import torchaudio
|
40 |
+
from datasets import load_dataset
|
41 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
42 |
+
|
43 |
+
test_dataset = load_dataset("common_voice", "zh-HK", split="test[:2%]")
|
44 |
+
|
45 |
+
processor = Wav2Vec2Processor.from_pretrained("ctl/wav2vec2-large-xlsr-cantonese")
|
46 |
+
model = Wav2Vec2ForCTC.from_pretrained("ctl/wav2vec2-large-xlsr-cantonese")
|
47 |
+
|
48 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
49 |
+
|
50 |
+
# Preprocessing the datasets.
|
51 |
+
# We need to read the aduio files as arrays
|
52 |
+
def speech_file_to_array_fn(batch):
|
53 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
54 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
55 |
+
return batch
|
56 |
+
|
57 |
+
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
58 |
+
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
59 |
|
60 |
+
with torch.no_grad():
|
61 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
62 |
|
63 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
64 |
+
|
65 |
+
print("Prediction:", processor.batch_decode(predicted_ids))
|
66 |
+
print("Reference:", test_dataset["sentence"][:2])
|
67 |
+
```
|
68 |
+
|
69 |
+
|
70 |
+
## Evaluation
|
71 |
+
|
72 |
+
The model can be evaluated as follows on the {language} test data of Common Voice. # TODO: replace #TODO: replace language with your {language}, *e.g.* French
|
73 |
+
|
74 |
+
|
75 |
+
```python
|
76 |
import torch
|
77 |
import torchaudio
|
78 |
from datasets import load_dataset, load_metric
|
|
|
81 |
import argparse
|
82 |
|
83 |
lang_id = "zh-HK"
|
84 |
+
model_id = "ctl/wav2vec2-large-xlsr-cantonese"
|
85 |
|
86 |
parser = argparse.ArgumentParser(description='hanles checkpoint loading')
|
87 |
parser.add_argument('--checkpoint', type=str, default=None)
|
|
|
128 |
print("CER: {:2f}".format(100 * cer.compute(predictions=result["pred_strings"], references=result["sentence"])))
|
129 |
```
|
130 |
|
131 |
+
### Character Error Rate implementation
|
132 |
|
133 |
+
Adapting code from [wer](https://github.com/huggingface/datasets/blob/master/metrics/wer/wer.py)
|
134 |
+
|
135 |
+
```python
|
136 |
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
137 |
class CER(datasets.Metric):
|
138 |
def _info(self):
|
139 |
+
...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
def _compute(self, predictions, references):
|
142 |
preds = [char for seq in predictions for char in list(seq)]
|
|
|
144 |
return wer(refs, preds)
|
145 |
```
|
146 |
|
147 |
+
|
148 |
+
**Test Result**: 17.81 %
|
149 |
+
|
150 |
+
|
151 |
+
## Training
|
152 |
+
|
153 |
+
The Common Voice `train`, `validation` were used for training.
|
154 |
+
|
155 |
+
The script used for training will be posted [here](https://github.com/chutaklee/CantoASR)
|