モデル概要
モデル特徴
モデル能力
使用事例
🚀 Wav2Vec2-Large-XLSR-53-rw
このモデルは、Common Voice データセットを使用してキニヤルワンダ語で facebook/wav2vec2-large-xlsr-53 をファインチューニングしたものです。約25%のトレーニングデータ(反対票がなく、9.5秒以内の発話に限定)を使用し、検証セットから2048の発話で検証を行いました。lucio/wav2vec2-large-xlsr-kinyarwanda モデルとは異なり、句読点を予測しない代わりに、このモデルは代名詞と母音で始まる単語の縮約を示すアポストロフィを予測しようとしますが、過剰一般化する可能性があります。 このモデルを使用する際には、音声入力が16kHzでサンプリングされていることを確認してください。
📦 インストール
このセクションでは、モデルを使用するための必要な依存関係をインストールする方法を説明します。以下のコマンドを使用して、必要なライブラリをインストールしてください。
pip install torch torchaudio datasets transformers jiwer unidecode
💻 使用例
基本的な使用法
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
# WARNING! This will download and extract to use about 80GB on disk.
test_dataset = load_dataset("common_voice", "rw", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("lucio/wav2vec2-large-xlsr-kinyarwanda")
model = Wav2Vec2ForCTC.from_pretrained("lucio/wav2vec2-large-xlsr-kinyarwanda")
resampler = torchaudio.transforms.Resample(48_000, 16_000)
# Preprocessing the datasets.
# We need to read the audio files as arrays
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
batch["speech"] = resampler(speech_array).squeeze().numpy()
return batch
test_dataset = test_dataset.map(speech_file_to_array_fn)
inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
with torch.no_grad():
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
predicted_ids = torch.argmax(logits, dim=-1)
print("Prediction:", processor.batch_decode(predicted_ids))
print("Reference:", test_dataset["sentence"][:2])
このコードでは、モデルを直接(言語モデルなしで)使用して音声認識を行います。
高度な使用法
import jiwer
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import re
import unidecode
test_dataset = load_dataset("common_voice", "rw", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("lucio/wav2vec2-large-xlsr-kinyarwanda-apostrophied")
model = Wav2Vec2ForCTC.from_pretrained("lucio/wav2vec2-large-xlsr-kinyarwanda-apostrophied")
model.to("cuda")
chars_to_ignore_regex = r'[!"#$%&()*+,./:;<=>?@\[\]\\_{}|~£¤¨©ª«¬®¯°·¸»¼½¾ðʺ˜˝ˮ‐–—―‚“”„‟•…″‽₋€™−√�]'
def remove_special_characters(batch):
batch["text"] = re.sub(r'[ʻʽʼ‘’´`]', r"'", batch["sentence"]) # normalize apostrophes
batch["text"] = re.sub(chars_to_ignore_regex, "", batch["text"]).lower().strip() # remove all other punctuation
batch["text"] = re.sub(r"([b-df-hj-np-tv-z])' ([aeiou])", r"\1'\2", batch["text"]) # remove spaces where apostrophe marks a deleted vowel
batch["text"] = re.sub(r"(-| '|' | +)", " ", batch["text"]) # treat dash and other apostrophes as word boundary
batch["text"] = unidecode.unidecode(batch["text"]) # strip accents from loanwords
return batch
## Audio pre-processing
resampler = torchaudio.transforms.Resample(48_000, 16_000)
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
batch["speech"] = resampler(speech_array).squeeze().numpy()
batch["sampling_rate"] = 16_000
return batch
def cv_prepare(batch):
batch = remove_special_characters(batch)
batch = speech_file_to_array_fn(batch)
return batch
test_dataset = test_dataset.map(cv_prepare)
# Preprocessing the datasets.
# We need to read the audio files as arrays
def evaluate(batch):
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
with torch.no_grad():
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
pred_ids = torch.argmax(logits, dim=-1)
batch["pred_strings"] = processor.batch_decode(pred_ids)
return batch
result = test_dataset.map(evaluate, batched=True, batch_size=8)
def chunked_wer(targets, predictions, chunk_size=None):
if chunk_size is None: return jiwer.wer(targets, predictions)
start = 0
end = chunk_size
H, S, D, I = 0, 0, 0, 0
while start < len(targets):
chunk_metrics = jiwer.compute_measures(targets[start:end], predictions[start:end])
H = H + chunk_metrics["hits"]
S = S + chunk_metrics["substitutions"]
D = D + chunk_metrics["deletions"]
I = I + chunk_metrics["insertions"]
start += chunk_size
end += chunk_size
return float(S + D + I) / float(H + S + D)
print("WER: {:2f}".format(100 * chunked_wer(result["sentence"], result["pred_strings"], chunk_size=4000)))
このコードでは、モデルを評価し、Word Error Rate (WER) を計算します。
📚 ドキュメント
モデル情報
属性 | 詳情 |
---|---|
モデルタイプ | XLSR Wav2Vec2 Large Kinyarwanda with apostrophes |
トレーニングデータ | Common Voice rw データセットの約25%(反対票がなく、9.5秒以内の発話に限定) |
評価結果
テスト結果: 39.92 %
トレーニングの詳細
Common Voiceトレーニングデータセットの例を使用してトレーニングを行いました。down_vote
がある発話や9.5秒を超える発話は除外しました。使用したデータは合計約125kの例で、利用可能なデータの25%です。OVHcloudが提供する1つのV100 GPUで合計約60時間のトレーニングを行いました。具体的には、32kの例のブロックで20エポック、その後3つの32kの例のブロックでそれぞれ10エポックのトレーニングを行いました。検証には、検証データセットの2048の例を使用しました。
トレーニングに使用した スクリプト は、transformersリポジトリに提供されている例のスクリプト を改変したものです。
📄 ライセンス
このモデルは、Apache-2.0ライセンスの下で提供されています。



