モデル概要
モデル特徴
モデル能力
使用事例
🚀 Whisper
Whisperは、自動音声認識(ASR)と音声翻訳のための事前学習済みモデルです。680万時間のラベル付きデータで学習されたWhisperモデルは、微調整することなく多くのデータセットやドメインに対して強力な汎化能力を示します。
Whisperは、OpenAIのAlec Radfordらによる論文 Robust Speech Recognition via Large-Scale Weak Supervision で提案されました。元のコードリポジトリは こちら で確認できます。
免責事項:このモデルカードの内容の一部はHugging Faceチームによって作成され、一部は元のモデルカードからコピー&ペーストされています。
✨ 主な機能
- 自動音声認識(ASR)と音声翻訳に対応
- 680万時間のラベル付きデータで学習され、汎化能力が高い
- 複数の言語に対応
📚 ドキュメント
モデルの詳細
WhisperはTransformerベースのエンコーダ・デコーダモデルで、シーケンス-to-シーケンス モデルとも呼ばれます。大規模な弱教師付き学習を用いて注釈付けされた680万時間のラベル付き音声データで学習されました。
モデルは英語のみのデータまたは多言語データで学習されています。英語のみのモデルは音声認識タスクで学習され、多言語モデルは音声認識と音声翻訳の両方で学習されています。音声認識の場合、モデルは音声と同じ言語での文字起こしを予測します。音声翻訳の場合、モデルは音声とは異なる言語への文字起こしを予測します。
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 | ✓ |
使用方法
音声サンプルを文字起こしするには、モデルを 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
>>> # load model and processor
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-medium")
>>> model.config.forced_decoder_ids = None
>>> # load dummy dataset and read audio files
>>> 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
>>> # generate token ids
>>> predicted_ids = model.generate(input_features)
>>> # decode token ids to text
>>> 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
>>> # load model and processor
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-medium")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="transcribe")
>>> # load streaming dataset and read first audio sample
>>> 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
>>> # generate token ids
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # decode token ids to text
>>> 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
>>> # load model and processor
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-medium")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="translate")
>>> # load streaming dataset and read first audio sample
>>> 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
>>> # generate token ids
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # decode token ids to text
>>> 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 Mediumを評価する方法を示しています。
>>> 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-medium")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-medium").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"]))
2.900409225488902
長文の文字起こし
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-medium",
>>> 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."
>>> # we can also return timestamps for the predictions
>>> 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と英語への音声翻訳タスクで学習および評価されています。約10の言語で強力なASR結果を示しています。音声活動検出、話者分類、または話者分離などの特定のタスクで微調整された場合、特に追加の機能を示す可能性がありますが、これらの領域では十分に評価されていません。ユーザーはロバストな評価を行うことを強くお勧めします。
📄 ライセンス
このモデルはApache 2.0ライセンスの下で提供されています。



