Update README.md
Browse files
README.md
CHANGED
|
@@ -67,6 +67,183 @@ The following hyperparameters were used during training:
|
|
| 67 |
- num_epochs: 5
|
| 68 |
- mixed_precision_training: Native AMP
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
### Training results
|
| 71 |
|
| 72 |
| Training Loss | Epoch | Step | Validation Loss | Wer | Cer | Ser |
|
|
|
|
| 67 |
- num_epochs: 5
|
| 68 |
- mixed_precision_training: Native AMP
|
| 69 |
|
| 70 |
+
### Training code
|
| 71 |
+
|
| 72 |
+
```bash
|
| 73 |
+
pip install datasets librosa scikit-learn torch torchaudio evaluate jiwer nltk
|
| 74 |
+
pip install --upgrade datasets
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
```python
|
| 78 |
+
from huggingface_hub import login
|
| 79 |
+
from datasets import load_dataset, DatasetDict
|
| 80 |
+
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2CTCTokenizer, Wav2Vec2Processor, Wav2Vec2ForCTC, TrainingArguments, Trainer
|
| 81 |
+
from datasets import load_dataset, Audio
|
| 82 |
+
import torch
|
| 83 |
+
import torchaudio
|
| 84 |
+
import re
|
| 85 |
+
import evaluate
|
| 86 |
+
import numpy as np
|
| 87 |
+
|
| 88 |
+
from dataclasses import dataclass, field
|
| 89 |
+
from typing import Any, Dict, List, Optional, Union
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
login("***")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
common_voice = DatasetDict()
|
| 96 |
+
common_voice["train"] = load_dataset("mozilla-foundation/common_voice_17_0", "ru", split="train")
|
| 97 |
+
common_voice["test"] = load_dataset("mozilla-foundation/common_voice_17_0", "ru", split="test")
|
| 98 |
+
|
| 99 |
+
common_voice = common_voice.remove_columns(["accent", "age", "client_id", "down_votes", "gender", "locale", "path", "segment", "up_votes"])
|
| 100 |
+
common_voice = common_voice.cast_column("audio", Audio(sampling_rate=16000))
|
| 101 |
+
|
| 102 |
+
tokenizer = Wav2Vec2CTCTokenizer.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-russian")
|
| 103 |
+
feature_extractor = Wav2Vec2FeatureExtractor(feature_size=1, sampling_rate=16000, padding_value=0.0, return_attention_mask=True)
|
| 104 |
+
processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)
|
| 105 |
+
|
| 106 |
+
def prepare_dataset(batch):
|
| 107 |
+
audio = batch["audio"]
|
| 108 |
+
|
| 109 |
+
# batched output is "un-batched"
|
| 110 |
+
batch["input_values"] = processor(audio["array"], sampling_rate=audio["sampling_rate"]).input_values[0]
|
| 111 |
+
batch["input_length"] = len(batch["input_values"])
|
| 112 |
+
|
| 113 |
+
with processor.as_target_processor():
|
| 114 |
+
batch["labels"] = processor(batch["sentence"]).input_ids
|
| 115 |
+
return batch
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
common_voice = common_voice.map(prepare_dataset, remove_columns=common_voice["train"].column_names, num_proc=2)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def has_valid_labels(example):
|
| 122 |
+
return example["labels"] is not None and len(example["labels"]) > 0
|
| 123 |
+
|
| 124 |
+
common_voice = common_voice.filter(has_valid_labels, num_proc=2)
|
| 125 |
+
|
| 126 |
+
ya nadeusb eta poebota ne zavosnet
|
| 127 |
+
|
| 128 |
+
common_voice
|
| 129 |
+
|
| 130 |
+
common_voice
|
| 131 |
+
|
| 132 |
+
wer_metric = evaluate.load("wer")
|
| 133 |
+
cer_metric = evaluate.load("cer")
|
| 134 |
+
|
| 135 |
+
def compute_metrics(pred):
|
| 136 |
+
pred_logits = pred.predictions
|
| 137 |
+
pred_ids = np.argmax(pred_logits, axis=-1)
|
| 138 |
+
|
| 139 |
+
pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id
|
| 140 |
+
|
| 141 |
+
pred_str = processor.batch_decode(pred_ids, skip_special_tokens=True)
|
| 142 |
+
label_str = processor.batch_decode(pred.label_ids, group_tokens=False, skip_special_tokens=True)
|
| 143 |
+
|
| 144 |
+
pairs = [(ref.strip(), hyp.strip()) for ref, hyp in zip(label_str, pred_str)]
|
| 145 |
+
pairs = [(ref, hyp) for ref, hyp in pairs if len(ref) > 0]
|
| 146 |
+
|
| 147 |
+
if len(pairs) == 0:
|
| 148 |
+
return {"wer": 1.0, "cer": 1.0, "ser": 1.0}
|
| 149 |
+
|
| 150 |
+
label_str, pred_str = zip(*pairs)
|
| 151 |
+
|
| 152 |
+
wer = 100 * wer_metric.compute(predictions=pred_str, references=label_str)
|
| 153 |
+
cer = 100 * cer_metric.compute(predictions=pred_str, references=label_str)
|
| 154 |
+
|
| 155 |
+
incorrect_sentences = sum([ref != pred for ref, pred in zip(label_str, pred_str)])
|
| 156 |
+
ser = 100 * incorrect_sentences / len(label_str)
|
| 157 |
+
|
| 158 |
+
return {
|
| 159 |
+
"wer": wer,
|
| 160 |
+
"cer": cer,
|
| 161 |
+
"ser": ser
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
model = Wav2Vec2ForCTC.from_pretrained(
|
| 166 |
+
"jonatasgrosman/wav2vec2-large-xlsr-53-russian",
|
| 167 |
+
ctc_loss_reduction="mean",
|
| 168 |
+
pad_token_id=processor.tokenizer.pad_token_id,
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
@dataclass
|
| 172 |
+
class DataCollatorCTCWithPadding:
|
| 173 |
+
processor: Wav2Vec2Processor
|
| 174 |
+
padding: Union[bool, str] = True
|
| 175 |
+
max_length: Optional[int] = None
|
| 176 |
+
max_length_labels: Optional[int] = None
|
| 177 |
+
pad_to_multiple_of: Optional[int] = None
|
| 178 |
+
pad_to_multiple_of_labels: Optional[int] = None
|
| 179 |
+
|
| 180 |
+
def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]:
|
| 181 |
+
# split inputs and labels since they have to be of different lengths and need
|
| 182 |
+
# different padding methods
|
| 183 |
+
input_features = [{"input_values": feature["input_values"]} for feature in features]
|
| 184 |
+
label_features = [{"input_ids": feature["labels"]} for feature in features]
|
| 185 |
+
|
| 186 |
+
batch = self.processor.pad(
|
| 187 |
+
input_features,
|
| 188 |
+
padding=self.padding,
|
| 189 |
+
max_length=self.max_length,
|
| 190 |
+
pad_to_multiple_of=self.pad_to_multiple_of,
|
| 191 |
+
return_tensors="pt",
|
| 192 |
+
)
|
| 193 |
+
with self.processor.as_target_processor():
|
| 194 |
+
labels_batch = self.processor.pad(
|
| 195 |
+
label_features,
|
| 196 |
+
padding=self.padding,
|
| 197 |
+
max_length=self.max_length_labels,
|
| 198 |
+
pad_to_multiple_of=self.pad_to_multiple_of_labels,
|
| 199 |
+
return_tensors="pt",
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# replace padding with -100 to ignore loss correctly
|
| 203 |
+
labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100)
|
| 204 |
+
|
| 205 |
+
batch["labels"] = labels
|
| 206 |
+
|
| 207 |
+
return batch
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
data_collator = DataCollatorCTCWithPadding(processor=processor, padding=True)
|
| 211 |
+
|
| 212 |
+
training_args = TrainingArguments(
|
| 213 |
+
output_dir="/content/drive/MyDrive/models/wav2vec2-large-ru-5ep",
|
| 214 |
+
logging_dir="/content/drive/MyDrive/models/wav2vec2-large-ru-5ep",
|
| 215 |
+
group_by_length=True,
|
| 216 |
+
per_device_train_batch_size=8,
|
| 217 |
+
per_device_eval_batch_size=4,
|
| 218 |
+
eval_strategy="steps",
|
| 219 |
+
logging_strategy="steps",
|
| 220 |
+
save_strategy="steps",
|
| 221 |
+
num_train_epochs=5,
|
| 222 |
+
logging_steps=25,
|
| 223 |
+
eval_steps=500,
|
| 224 |
+
save_steps=500,
|
| 225 |
+
fp16=True,
|
| 226 |
+
optim="adamw_torch_fused",
|
| 227 |
+
torch_compile=True,
|
| 228 |
+
gradient_checkpointing=True,
|
| 229 |
+
learning_rate=1e-4,
|
| 230 |
+
weight_decay=0.005,
|
| 231 |
+
report_to=["tensorboard"],
|
| 232 |
+
push_to_hub=False
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
trainer = Trainer(
|
| 236 |
+
model=model,
|
| 237 |
+
data_collator=data_collator,
|
| 238 |
+
args=training_args,
|
| 239 |
+
compute_metrics=compute_metrics,
|
| 240 |
+
train_dataset=common_voice["train"],
|
| 241 |
+
eval_dataset=common_voice["test"],
|
| 242 |
+
tokenizer=processor,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
trainer.train()
|
| 246 |
+
```
|
| 247 |
### Training results
|
| 248 |
|
| 249 |
| Training Loss | Epoch | Step | Validation Loss | Wer | Cer | Ser |
|