File size: 10,966 Bytes
c79c794 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
import os
from datasets import load_dataset, Audio, concatenate_datasets
# Set processors (optional)
num_proc = os.cpu_count()//2
num_dataloaders = os.cpu_count()//2
print(f"Cpu count: {os.cpu_count()}\nNum proc: {num_proc}\nNum dataloaders: {num_dataloaders}")
# Load datasets
train = load_dataset()
dev = load_dataset()
test = load_dataset()
import unicodedata
import re
def preprocess_text(batch):
# Convert to lowercase
batch['sentence'] = batch['sentence'].lower()
# Normalize text
batch['sentence'] = unicodedata.normalize('NFKC', batch['sentence'])
batch['sentence'] = re.sub(r'[\’\ʻ\ʼ\ʽ\‘]', "'", batch['sentence'])
# Remove punctuation and special characters
batch['sentence'] = re.sub(r'[^\w\s\']', '', batch['sentence'])
batch['sentence'] = re.sub(r'_', ' ', batch['sentence'])
# Remove excessive whitespace
batch['sentence'] = ' '.join(batch['sentence'].split())
return batch
import librosa
import numpy as np
def get_lens(batch):
try:
audio_len = librosa.get_duration(y=batch['audio']['array'], sr=batch['audio']['sampling_rate'])
except:
del batch['audio']
batch['audio'] = None
audio_len = 0.0
transcript_len = len(batch['sentence'])
batch['audio_len'] = audio_len
batch['transcript_len'] = transcript_len
batch['len_ratio'] = float(audio_len)/float(transcript_len)
batch['num_feature_vecs'] = int(np.round(audio_len * 1000 / 20))
return batch
transcript_len = len(batch['sentence'])
batch['audio_len'] = audio_len
batch['transcript_len'] = transcript_len
batch['len_ratio'] = float(audio_len)/float(transcript_len)
batch['num_feature_vecs'] = int(np.round(audio_len * 1000 / 20)) # seconds -> milliseconds, divide by 20 millisecond feature_win_step, round up to nearest int
return batch
def data_checks(batch):
audio_check = (batch['audio_len']>1.0 and batch['audio_len']<30.0)
transcript_check = (batch['transcript_len']>10)
input_output_ratio = float(batch['num_feature_vecs']) / float(batch['transcript_len'])
input_output_ratio_check = input_output_ratio>1.0 # CTC algorithm assumes the input is not shorter than the ouput
return (audio_check and transcript_check and input_output_ratio_check)
train = train.map(preprocess_text, num_proc=num_proc)
dev = dev.map(preprocess_text, num_proc=num_proc)
try:
train = train.map(get_lens, num_proc=num_proc)
except:
train = train.map(get_lens, num_proc=4)
try:
dev = dev.map(get_lens, num_proc=num_proc)
except:
dev = dev.map(get_lens, num_proc=4)
train = train.filter(data_checks, num_proc=num_proc)
dev = dev.filter(data_checks, num_proc=num_proc)
train_mean = np.mean(train['len_ratio'])
train_std = np.std(train['len_ratio'])
dev_mean = np.mean(dev['len_ratio'])
dev_std = np.std(dev['len_ratio'])
num_std_devs = 2
train = train.filter(lambda batch: (abs(batch['len_ratio'] - train_mean) - (num_std_devs * train_std)) <= 0, num_proc=num_proc)
dev = dev.filter(lambda batch: (abs(batch['len_ratio'] - dev_mean) - (num_std_devs * dev_std)) <= 0, num_proc=num_proc)
print(f"Train hours: {sum(train['audio_len'])/3600}\nDev hours: {sum(dev['audio_len'])/3600}")
train = train.remove_columns(['audio_len', 'transcript_len', 'len_ratio', 'num_feature_vecs'])
dev = dev.remove_columns(['audio_len', 'transcript_len', 'len_ratio', 'num_feature_vecs'])
alphabet = None # define the language's alphabet here e.g. " 'abcdefghijklmnorstuwyzƙƴɓɗ" for Hausa
alphabet = sorted(list(set(alphabet)))
vocab_dict = {v: k for k, v in enumerate(alphabet)}
vocab_dict["|"] = vocab_dict[" "]
del vocab_dict[" "]
vocab_dict["[UNK]"] = len(vocab_dict)
vocab_dict["[PAD]"] = len(vocab_dict)
import json
with open('vocab.json', 'w') as vocab_file:
json.dump(vocab_dict, vocab_file)
from transformers import Wav2Vec2CTCTokenizer
tokenizer = Wav2Vec2CTCTokenizer.from_pretrained("./", unk_token="[UNK]", pad_token="[PAD]", word_delimiter_token="|")
from transformers import Wav2Vec2FeatureExtractor
feature_extractor = Wav2Vec2FeatureExtractor(feature_size=1, sampling_rate=16000, padding_value=0.0, do_normalize=True, return_attention_mask=True)
from transformers import Wav2Vec2Processor
processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)
def prepare_dataset(batch):
audio = batch["audio"]
batch["input_values"] = processor(audio=audio["array"], sampling_rate=audio["sampling_rate"]).input_values[0]
batch["input_length"] = len(batch["input_values"])
batch["labels"] = processor(text=batch["sentence"]).input_ids
return batch
try:
train = train.map(prepare_dataset, remove_columns=train.column_names, num_proc=num_proc)
except:
train = train.map(prepare_dataset, remove_columns=train.column_names, num_proc=4)
try:
dev = dev.map(prepare_dataset, remove_columns=dev.column_names, num_proc=num_proc)
except:
dev = dev.map(prepare_dataset, remove_columns=dev.column_names, num_proc=4)
import torch
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union
@dataclass
class DataCollatorCTCWithPadding:
"""
Data collator that will dynamically pad the inputs received.
Args:
processor (:class:`~transformers.Wav2Vec2Processor`)
The processor used for proccessing the data.
padding (:obj:`bool`, :obj:`str` or :class:`~transformers.tokenization_utils_base.PaddingStrategy`, `optional`, defaults to :obj:`True`):
Select a strategy to pad the returned sequences (according to the model's padding side and padding index)
among:
* :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
* :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
maximum acceptable input length for the model if that argument is not provided.
* :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
different lengths).
"""
processor: Wav2Vec2Processor
padding: Union[bool, str] = True
def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]:
# split inputs and labels since they have to be of different lengths and need
# different padding methods
input_features = [{"input_values": feature["input_values"]} for feature in features]
label_features = [{"input_ids": feature["labels"]} for feature in features]
batch = self.processor.pad(
input_features=input_features,
padding=self.padding,
return_tensors="pt",
)
labels_batch = self.processor.pad(
labels=label_features,
padding=self.padding,
return_tensors="pt",
)
# replace padding with -100 to ignore loss correctly
labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100)
batch["labels"] = labels
return batch
data_collator = DataCollatorCTCWithPadding(processor=processor, padding=True)
"""# Model Training"""
import evaluate
wer_metric = evaluate.load("wer")
cer_metric = evaluate.load("cer")
import numpy as np
def compute_metrics(pred):
pred_logits = pred.predictions
pred_ids = np.argmax(pred_logits, axis=-1)
pred.label_ids[pred.label_ids == -100] = processor.tokenizer.pad_token_id
pred_str = processor.batch_decode(pred_ids)
label_str = processor.batch_decode(pred.label_ids, group_tokens=False)
wer = wer_metric.compute(predictions=pred_str, references=label_str)
cer = cer_metric.compute(predictions=pred_str, references=label_str)
return {"wer": wer, "cer": cer}
from transformers import Wav2Vec2ForCTC, TrainingArguments, Trainer, EarlyStoppingCallback
model_checkpoint = "facebook/wav2vec2-xls-r-300m"
model = Wav2Vec2ForCTC.from_pretrained(
model_checkpoint,
attention_dropout=0.0,
hidden_dropout=0.0,
feat_proj_dropout=0.0,
mask_time_prob=0.05,
layerdrop=0.0,
ctc_loss_reduction="mean",
pad_token_id=processor.tokenizer.pad_token_id,
vocab_size=len(processor.tokenizer),
)
model.freeze_feature_encoder()
import wandb
dataset = None
language = None
sample_hours = None
version = None
batch_size = None
grad_acc = 1
eval_batch_size = batch_size//2
epochs = None
output_dir = f"{model_checkpoint.split('/')[-1]}-{dataset}-{language}-{sample_hours}hrs-{version}"
wandb.init(
project="ASR Africa",
entity="asr-africa-research-team",
name=output_dir,
)
training_args = TrainingArguments(
output_dir=output_dir,
group_by_length=True,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=eval_batch_size,
gradient_accumulation_steps=grad_acc,
eval_strategy="epoch",
logging_strategy="epoch",
save_strategy="epoch",
num_train_epochs=epochs,
gradient_checkpointing=True,
fp16=True,
learning_rate=None,
lr_scheduler_type='linear',
warmup_ratio=None,
save_total_limit=2,
load_best_model_at_end=True,
metric_for_best_model="wer",
greater_is_better=False,
optim='adamw_torch',
push_to_hub=True,
hub_model_id=f"asr-africa/{output_dir}",
hub_private_repo=True,
dataloader_num_workers=num_dataloaders,
)
trainer = Trainer(
model=model,
data_collator=data_collator,
args=training_args,
compute_metrics=compute_metrics,
train_dataset=train,
eval_dataset=dev,
tokenizer=processor.feature_extractor,
callbacks=[
EarlyStoppingCallback(
early_stopping_patience=10,
early_stopping_threshold=1e-3
)
],
)
trainer.train()
kwargs = {
"dataset_tags": "",
"dataset": "",
"language": "",
"model_name": "",
"finetuned_from": model_checkpoint,
"tasks": "automatic-speech-recognition",
}
trainer.push_to_hub(**kwargs)
other_test_dataset_1 = load_dataset()
other_test_dataset_2 = load_dataset()
test = concatenate_datasets([test, other_test_dataset_1, other_test_dataset_2]).shuffle(42)
test = test.map(preprocess_text, num_proc=num_proc)
try:
test = test.map(get_lens, num_proc=num_proc)
except:
test = test.map(get_lens, num_proc=4)
test = test.filter(data_checks, num_proc=num_proc)
test_mean = np.mean(test['len_ratio'])
test_std = np.std(test['len_ratio'])
num_std_devs = 2
test = test.filter(lambda batch: (abs(batch['len_ratio'] - test_mean) - (num_std_devs * test_std)) <= 0, num_proc=num_proc)
print(f"Test hours: {sum(test['audio_len'])/3600}")
test = test.remove_columns(['audio_len', 'transcript_len', 'len_ratio', 'num_feature_vecs'])
try:
test = test.map(prepare_dataset, remove_columns=test.column_names, num_proc=num_proc)
except:
test = test.map(prepare_dataset, remove_columns=test.column_names, num_proc=4)
results = trainer.evaluate(eval_dataset=test, metric_key_prefix="test")
print(results)
wandb.log(results)
train.cleanup_cache_files()
dev.cleanup_cache_files()
test.cleanup_cache_files()
torch.cuda.empty_cache() |