aniltrkkn commited on
Commit
98e2fbd
1 Parent(s): 4ca8a0c

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +138 -0
README.md ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ language: tr
2
+ datasets:
3
+ - common_voice
4
+ metrics:
5
+ - wer
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-53-Turkish
14
+ results:
15
+ - task:
16
+ name: Speech Recognition
17
+ type: automatic-speech-recognition
18
+ dataset:
19
+ name: Common Voice tr
20
+ type: common_voice
21
+ args: tr
22
+ metrics:
23
+ - name: Test WER
24
+ type: wer
25
+ value: 17.46
26
+ ---
27
+
28
+ # Wav2Vec2-Large-XLSR-53-Turkish
29
+
30
+ Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Turkish 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
+ from unicode_tr import unicode_tr
43
+
44
+ test_dataset = load_dataset("common_voice", "tr", split="test[:2%]")
45
+
46
+ processor = Wav2Vec2Processor.from_pretrained("aniltrkkn/wav2vec2-large-xlsr-53-turkish")
47
+ model = Wav2Vec2ForCTC.from_pretrained("aniltrkkn/wav2vec2-large-xlsr-53-turkish")
48
+
49
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
50
+
51
+ # Preprocessing the datasets.
52
+ # We need to read the aduio files as arrays
53
+ def speech_file_to_array_fn(batch):
54
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
55
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
56
+ return batch
57
+
58
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
59
+ inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
60
+
61
+ with torch.no_grad():
62
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
63
+
64
+ predicted_ids = torch.argmax(logits, dim=-1)
65
+
66
+ print("Prediction:", processor.batch_decode(predicted_ids))
67
+ print("Reference:", test_dataset["sentence"][:2])
68
+ ```
69
+
70
+
71
+ ## Evaluation
72
+
73
+ The model can be evaluated as follows on the Turkish test data of Common Voice.
74
+
75
+
76
+ ```python
77
+ import torch
78
+ import torchaudio
79
+ from datasets import load_dataset, load_metric
80
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
81
+ import re
82
+
83
+ test_dataset = load_dataset("common_voice", "tr", split="test")
84
+ wer = load_metric("wer")
85
+
86
+ processor = Wav2Vec2Processor.from_pretrained("aniltrkkn/wav2vec2-large-xlsr-53-turkish")
87
+ model = Wav2Vec2ForCTC.from_pretrained("aniltrkkn/wav2vec2-large-xlsr-53-turkish")
88
+ model.to("cuda")
89
+
90
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
91
+ resampler = torchaudio.transforms.Resample(48_000, 16_000)
92
+
93
+ # Preprocessing the datasets.
94
+ # We need to read the aduio files as arrays
95
+ def speech_file_to_array_fn(batch):
96
+ batch["sentence"] = str(unicode_tr(re.sub(chars_to_ignore_regex, "", batch["sentence"])).lower())
97
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
98
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
99
+ return batch
100
+
101
+ test_dataset = test_dataset.map(speech_file_to_array_fn)
102
+
103
+ # Preprocessing the datasets.
104
+ # We need to read the aduio files as arrays
105
+ def evaluate(batch):
106
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
107
+
108
+ with torch.no_grad():
109
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
110
+
111
+ pred_ids = torch.argmax(logits, dim=-1)
112
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
113
+ return batch
114
+
115
+ result = test_dataset.map(evaluate, batched=True, batch_size=8)
116
+
117
+ print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
118
+ ```
119
+
120
+ **Test Result**: 17.46 %
121
+
122
+ ## Training
123
+ unicode_tr package is used for converting sentences to lower case since regular lower() does not work well with Turkish.
124
+
125
+ Since training data is very limited for Turkish, all data is employed with a K-Fold (k=5) training approach. Best model out of the 5 trainings is uploaded. Training arguments:
126
+ --num_train_epochs="30" \
127
+ --per_device_train_batch_size="32" \
128
+ --evaluation_strategy="steps" \
129
+ --activation_dropout="0.055" \
130
+ --attention_dropout="0.094" \
131
+ --feat_proj_dropout="0.04" \
132
+ --hidden_dropout="0.047" \
133
+ --layerdrop="0.041" \
134
+ --learning_rate="2.34e-4" \
135
+ --mask_time_prob="0.082" \
136
+ --warmup_steps="250" \
137
+
138
+ All trainings took ~20 hours with a GeForce RTX 3090 Graphics Card.