モデル概要
モデル特徴
モデル能力
使用事例
🚀 Whisper
Whisperは、自動音声認識(ASR)と音声翻訳のための事前学習済みモデルです。68万時間のラベル付きデータで学習されたWhisperモデルは、微調整することなく多くのデータセットやドメインに対して高い汎化能力を示します。
Whisperは、OpenAIのAlec Radfordらによる論文 Robust Speech Recognition via Large-Scale Weak Supervision で提案されました。元のコードリポジトリは こちら で確認できます。
免責事項: このモデルカードの内容の一部はHugging Faceチームによって作成され、一部は元のモデルカードからコピー&ペーストされています。
✨ 主な機能
- 68万時間のラベル付き音声データを使用して大規模な弱教師付き学習で訓練されたTransformerベースのエンコーダ・デコーダモデル。
- 英語のみのデータまたは多言語データで訓練され、音声認識と音声翻訳の両方のタスクに対応。
- 5種類の異なるモデルサイズのチェックポイントが用意されており、すべての事前学習済みチェックポイントは Hugging Face Hub で利用可能。
📚 ドキュメント
モデルの詳細
WhisperはTransformerベースのエンコーダ・デコーダモデルで、シーケンス-to-シーケンス モデルとも呼ばれます。大規模な弱教師付き学習を用いて注釈付けされた68万時間のラベル付き音声データで訓練されています。
モデルは、英語のみのデータまたは多言語データで訓練されています。英語のみのモデルは音声認識タスクで訓練され、多言語モデルは音声認識と音声翻訳の両方で訓練されています。音声認識の場合、モデルは音声と同じ言語の文字起こしを予測します。音声翻訳の場合、モデルは音声と異なる言語の文字起こしを予測します。
Whisperのチェックポイントは、モデルサイズが異なる5つの構成で提供されています。最小の4つは、英語のみまたは多言語データで訓練されています。最大のチェックポイントは多言語のみです。すべての10個の事前学習済みチェックポイントは Hugging Face Hub で利用可能です。チェックポイントの概要は以下の表にまとめられており、Hub上のモデルへのリンクも記載されています。
サイズ | パラメータ | 英語のみ | 多言語 |
---|---|---|---|
tiny | 39 M | ✓ | ✓ |
base | 74 M | ✓ | ✓ |
small | 244 M | ✓ | ✓ |
medium | 769 M | ✓ | ✓ |
large | 1550 M | x | ✓ |
large-v2 | 1550 M | x | ✓ |
モデルの指標
タスク | データセット | 評価指標 | 値 |
---|---|---|---|
自動音声認識 | LibriSpeech (clean) | Test WER | 3.432213777886737 |
自動音声認識 | LibriSpeech (other) | Test WER | 7.628304527060248 |
自動音声認識 | Common Voice 11.0 | Test WER | 87.3 |
自動音声認識 | Common Voice 13.0 | Wer | 125.69809089960707 |
サポートされている言語
- en, zh, de, es, ru, ko, fr, ja, pt, tr, pl, ca, nl, ar, sv, it, id, hi, fi, vi, he, uk, el, ms, cs, ro, da, hu, ta, no, th, ur, hr, bg, lt, la, mi, ml, cy, sk, te, fa, lv, bn, sr, az, sl, kn, et, mk, br, eu, is, hy, ne, mn, bs, kk, sq, sw, gl, mr, pa, si, km, sn, yo, so, af, oc, ka, be, tg, sd, gu, am, yi, lo, uz, fo, ht, ps, tk, nn, mt, sa, lb, my, bo, tl, mg, as, tt, haw, ln, ha, ba, jw, su
💻 使用例
基本的な使用法
音声サンプルを文字起こしするには、モデルを WhisperProcessor
と一緒に使用する必要があります。
WhisperProcessor
は以下の用途に使用されます。
- 音声入力を前処理する(モデル用にログメルスペクトログラムに変換する)
- モデルの出力を後処理する(トークンからテキストに変換する)
モデルは、適切な「コンテキストトークン」を渡すことで、実行するタスク(文字起こしまたは翻訳)を知らされます。これらのコンテキストトークンは、デコードプロセスの開始時にデコーダに与えられるトークンのシーケンスで、以下の順序になります。
- 文字起こしは常に
<|startoftranscript|>
トークンから始まります。 - 2番目のトークンは言語トークンです(例: 英語の場合は
<|en|>
)。 - 3番目のトークンは「タスクトークン」です。
<|transcribe|>
(音声認識)または<|translate|>
(音声翻訳)の2つの値のいずれかを取ります。 - さらに、モデルがタイムスタンプ予測を含めない場合は
<|notimestamps|>
トークンが追加されます。
したがって、典型的なコンテキストトークンのシーケンスは次のようになります。
<|startoftranscript|> <|en|> <|transcribe|> <|notimestamps|>
これは、モデルに英語で音声認識タスクを実行し、タイムスタンプを予測しないように指示します。
これらのトークンは、強制または非強制のいずれかに設定できます。強制された場合、モデルは各位置で各トークンを予測するようにされます。これにより、Whisperモデルの出力言語とタスクを制御することができます。非強制の場合、Whisperモデルは自動的に出力言語とタスクを予測します。
コンテキストトークンは以下のように設定できます。
model.config.forced_decoder_ids = WhisperProcessor.get_decoder_prompt_ids(language="english", task="transcribe")
これにより、モデルは音声認識タスクで英語を予測するように強制されます。
高度な使用法
文字起こし
英語から英語
この例では、コンテキストトークンは「非強制」であり、モデルが自動的に出力言語(英語)とタスク(文字起こし)を予測します。
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import load_dataset
>>> # モデルとプロセッサをロード
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> model.config.forced_decoder_ids = None
>>> # ダミーデータセットをロードし、音声ファイルを読み込む
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> sample = ds[0]["audio"]
>>> input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
>>> # トークンIDを生成
>>> predicted_ids = model.generate(input_features)
>>> # トークンIDをテキストにデコード
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
['<|startoftranscript|><|en|><|transcribe|><|notimestamps|> Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.<|endoftext|>']
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.']
skip_special_tokens=True
を設定することで、文字起こしの先頭からコンテキストトークンを削除できます。
フランス語からフランス語
次の例は、デコーダIDを適切に設定することによるフランス語からフランス語の文字起こしを示しています。
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # モデルとプロセッサをロード
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="transcribe")
>>> # ストリーミングデータセットをロードし、最初の音声サンプルを読み込む
>>> ds = load_dataset("common_voice", "fr", split="test", streaming=True)
>>> ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
>>> input_speech = next(iter(ds))["audio"]
>>> input_features = processor(input_speech["array"], sampling_rate=input_speech["sampling_rate"], return_tensors="pt").input_features
>>> # トークンIDを生成
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # トークンIDをテキストにデコード
>>> transcription = processor.batch_decode(predicted_ids)
['<|startoftranscript|><|fr|><|transcribe|><|notimestamps|> Un vrai travail intéressant va enfin être mené sur ce sujet.<|endoftext|>']
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' Un vrai travail intéressant va enfin être mené sur ce sujet.']
翻訳
タスクを "translate" に設定すると、Whisperモデルは音声翻訳を実行するように強制されます。
フランス語から英語
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # モデルとプロセッサをロード
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="translate")
>>> # ストリーミングデータセットをロードし、最初の音声サンプルを読み込む
>>> ds = load_dataset("common_voice", "fr", split="test", streaming=True)
>>> ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
>>> input_speech = next(iter(ds))["audio"]
>>> input_features = processor(input_speech["array"], sampling_rate=input_speech["sampling_rate"], return_tensors="pt").input_features
>>> # トークンIDを生成
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # トークンIDをテキストにデコード
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' A very interesting work, we will finally be given on this subject.']
評価
このコードスニペットは、LibriSpeech test-clean でWhisper Smallを評価する方法を示しています。
>>> from datasets import load_dataset
>>> from transformers import WhisperForConditionalGeneration, WhisperProcessor
>>> import torch
>>> from evaluate import load
>>> librispeech_test_clean = load_dataset("librispeech_asr", "clean", split="test")
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").to("cuda")
>>> def map_to_pred(batch):
>>> audio = batch["audio"]
>>> input_features = processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt").input_features
>>> batch["reference"] = processor.tokenizer._normalize(batch['text'])
>>>
>>> with torch.no_grad():
>>> predicted_ids = model.generate(input_features.to("cuda"))[0]
>>> transcription = processor.decode(predicted_ids)
>>> batch["prediction"] = processor.tokenizer._normalize(transcription)
>>> return batch
>>> result = librispeech_test_clean.map(map_to_pred)
>>> wer = load("wer")
>>> print(100 * wer.compute(references=result["reference"], predictions=result["prediction"]))
3.432213777886737
長時間の文字起こし
Whisperモデルは、本質的に最大30秒の音声サンプルで動作するように設計されています。ただし、チャンキングアルゴリズムを使用することで、任意の長さの音声サンプルを文字起こしするために使用できます。これは、Transformersの pipeline
メソッドを通じて可能です。チャンキングは、パイプラインをインスタンス化する際に chunk_length_s=30
を設定することで有効になります。チャンキングが有効になると、パイプラインはバッチ推論で実行できます。また、return_timestamps=True
を渡すことで、シーケンスレベルのタイムスタンプを予測するように拡張することもできます。
>>> import torch
>>> from transformers import pipeline
>>> from datasets import load_dataset
>>> device = "cuda:0" if torch.cuda.is_available() else "cpu"
>>> pipe = pipeline(
>>> "automatic-speech-recognition",
>>> model="openai/whisper-small",
>>> chunk_length_s=30,
>>> device=device,
>>> )
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> sample = ds[0]["audio"]
>>> prediction = pipe(sample.copy(), batch_size=8)["text"]
" Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel."
>>> # 予測のタイムスタンプも返すことができます
>>> prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]
[{'text': ' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.',
'timestamp': (0.0, 5.44)}]
チャンキングアルゴリズムの詳細については、ブログ記事 ASR Chunking を参照してください。
微調整
事前学習済みのWhisperモデルは、異なるデータセットやドメインに対して高い汎化能力を示します。ただし、特定の言語やタスクに対しては、微調整 を通じて予測能力をさらに向上させることができます。ブログ記事 Fine-Tune Whisper with 🤗 Transformers は、わずか5時間のラベル付きデータでWhisperモデルを微調整するためのステップバイステップガイドを提供しています。
評価された使用法
これらのモデルの主な対象ユーザーは、現在のモデルのロバスト性、汎化能力、機能、バイアス、制約を研究するAI研究者です。ただし、Whisperは開発者にとっても、特に英語の音声認識において、ASRソリューションとして非常に有用です。モデルがリリースされると、「意図された」使用法のみにアクセスを制限したり、何が研究であり何が研究でないかについて合理的なガイドラインを策定したりすることは不可能であることを認識しています。
モデルは主に、ASRと音声に関して訓練および評価されています。
📄 ライセンス
このモデルは apache-2.0 ライセンスの下で提供されています。



