qqpann commited on
Commit
5451826
1 Parent(s): f0fe9bb

Update: readme

Browse files
Files changed (1) hide show
  1. README.md +99 -0
README.md CHANGED
@@ -34,3 +34,102 @@ model-index:
34
  type: cer
35
  value: 0.06610296027
36
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  type: cer
35
  value: 0.06610296027
36
  ---
37
+
38
+ # Wav2Vec2-Large-XLSR-53-Japanese
39
+
40
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Japanese using the [Common Voice](https://huggingface.co/datasets/common_voice), ... and ... dataset{s}.
41
+ When using this model, make sure that your speech input is sampled at 16kHz.
42
+
43
+ ## Usage
44
+
45
+ The model can be used directly (without a language model) as follows:
46
+
47
+ ```python
48
+ import torch
49
+ import torchaudio
50
+ from datasets import load_dataset
51
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
52
+
53
+ test_dataset = load_dataset("common_voice", "ja", split="test[:2%]")
54
+
55
+ processor = Wav2Vec2Processor.from_pretrained("qqhann/w2v_hf_jsut_xlsr53")
56
+ model = Wav2Vec2ForCTC.from_pretrained("qqhann/w2v_hf_jsut_xlsr53")
57
+
58
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
59
+
60
+ # Preprocessing the datasets.
61
+ # We need to read the aduio files as arrays
62
+ def speech_file_to_array_fn(batch):
63
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
64
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
65
+ return batch
66
+
67
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
68
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
69
+
70
+ with torch.no_grad():
71
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
72
+
73
+ predicted_ids = torch.argmax(logits, dim=-1)
74
+
75
+ print("Prediction:", processor.batch_decode(predicted_ids))
76
+ print("Reference:", test_dataset["sentence"][:2])
77
+ ```
78
+
79
+ ## Evaluation
80
+
81
+ The model can be evaluated as follows on the Japanese test data of Common Voice.
82
+
83
+ ```python
84
+ import torch
85
+ import torchaudio
86
+ from datasets import load_dataset, load_metric
87
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
88
+ import re
89
+
90
+ test_dataset = load_dataset("common_voice", "ja", split="test")
91
+ wer = load_metric("wer")
92
+
93
+ processor = Wav2Vec2Processor.from_pretrained("qqhann/w2v_hf_jsut_xlsr53")
94
+ model = Wav2Vec2ForCTC.from_pretrained("qqhann/w2v_hf_jsut_xlsr53")
95
+ model.to("cuda")
96
+
97
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]' # TODO: adapt this list to include all special characters you removed from the data
98
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
99
+
100
+ # Preprocessing the datasets.
101
+ # We need to read the aduio files as arrays
102
+ def speech_file_to_array_fn(batch):
103
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
104
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
105
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
106
+ return batch
107
+
108
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
109
+
110
+ # Preprocessing the datasets.
111
+ # We need to read the aduio files as arrays
112
+ def evaluate(batch):
113
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
114
+
115
+ with torch.no_grad():
116
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
117
+
118
+ pred_ids = torch.argmax(logits, dim=-1)
119
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
120
+ return batch
121
+
122
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
123
+
124
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
125
+ ```
126
+
127
+ **Test Result**: 20.48 %
128
+
129
+ ## Training
130
+
131
+ <!-- The Common Voice `train`, `validation`, and ... datasets were used for training as well as ... and ... # TODO: adapt to state all the datasets that were used for training. -->
132
+
133
+ The privately collected JSUT Japanese dataset was used for training.
134
+
135
+ <!-- The script used for training can be found [here](...) # TODO: fill in a link to your training script here. If you trained your model in a colab, simply fill in the link here. If you trained the model locally, it would be great if you could upload the training script on github and paste the link here. -->