Whisper: Robust Speech Recognition via Large-Scale Weak Supervision

repository·main·Indexed 13 days ago

https://github.com/openai/whisper

A general-purpose, multitask speech recognition model capable of multilingual speech recognition, speech translation, and language identification. It provides a Python API and CLI for transcribing audio, with various model sizes including tiny, base, small, medium, large, and turbo to balance speed and accuracy.

Tokens
2.7K
Snippets
11
Records
14
Agent score
50%

What's inside Whisper

  1. Install Whisper

    main

    You can install the latest release of Whisper via pip. The codebase is compatible with Python 3.8-3.11 and recent PyTorch versions.

    To install the latest release:

    pip install -U openai-whisper

    To install directly from the GitHub repository:

    pip install git+https://github.com/openai/whisper.git

    To update to the latest commit from the repository:

    pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git

    Note: If you encounter installation errors related to tiktoken, you may need to install the rust development environment and ensure it is in your PATH. If you see No module named 'setuptools_rust', run:

    pip install setuptools-rust
  2. Replicate TED-LIUM 3 long-form English-only dataset

    main

    To create the long-form transcription dataset used in the Whisper evaluation from the TED-LIUM3 dataset, slice the audio between the beginning of the first labeled segment and the end of the last labeled segment of each talk. Use the concatenated text as the label.

    Timestamps for slicing the 11 TED talks in the test split:

    FilenameBegin time (s)End time (s)
    DanBarber_201016.091116.24
    JaneMcGonigal_201015.4761187.61
    BillGates_201015.8611656.94
    TomWujec_2010U16.26402.17
    GaryFlake_201016.06367.14
    EricMead_2009P18.434536.44
    MichaelSpecter_201016.11979.312
    DanielKahneman_201015.81199.44
    AimeeMullins_2009P17.821296.59
    JamesCameron_201016.751010.65
    RobertGupta_2010U16.8387.03
  3. Convert CallHome & Switchboard wav.scp files to WAV

    main

    If you are using the CallHome or Switchboard corpora (LDC2002S09 and LDC2002T43) and have a wav.scp file, you can convert the entries into actual WAV files using the following bash command. This assumes the command in the wav.scp file can be executed to produce the audio stream.

    mkdir -p wav
    while read name cmd; do
        echo $name
        echo ${cmd/\|/} wav/$name.wav | bash
    done < wav.scp
  4. Install ffmpeg for Whisper

    main

    Whisper requires the ffmpeg command-line tool to be installed on your system. Use your platform's package manager to install it:

    • Ubuntu/Debian: sudo apt update && sudo apt install ffmpeg
    • Arch Linux: sudo pacman -S ffmpeg
    • MacOS (Homebrew): brew install ffmpeg
    • Windows (Chocolatey): choco install ffmpeg
    • Windows (Scoop): scoop install ffmpeg
    # Example for Ubuntu/Debian
    sudo apt update && sudo apt install ffmpeg
  5. Install Whisper and evaluation dependencies

    main

    To use Whisper models and evaluate transcription results (e.g., calculating Word Error Rate), install the Whisper package directly from the GitHub repository and the jiwer library.

    ! pip install git+https://github.com/openai/whisper.git
    ! pip install jiwer
  6. Use Whisper via Python API

    main

    Whisper provides a high-level API for transcription and lower-level access for language detection and decoding.

    High-level transcription:

    import whisper
    
    model = whisper.load_model("turbo")
    result = model.transcribe("audio.mp3")
    print(result["text"])

    Low-level access (Language detection and decoding): This approach allows you to manually handle audio loading, padding, and spectrogram generation.

    import whisper
    
    model = whisper.load_model("turbo")
    
    # load audio and pad/trim it to fit 30 seconds
    audio = whisper.load_audio("audio.mp3")
    audio = whisper.pad_or_trim(audio)
    
    # make log-Mel spectrogram and move to the same device as the model
    mel = whisper.log_mel_spectrogram(audio, n_mels=model.dims.n_mels).to(model.device)
    
    # detect the spoken language
    _, probs = model.detect_language(mel)
    print(f"Detected language: {max(probs, key=probs.get)}")
    
    # decode the audio
    options = whisper.DecodingOptions()
    result = whisper.decode(model, mel, options)
    
    # print the recognized text
    print(result.text)
  7. Reference datasets used for Whisper evaluation

    main

    This record provides a summary of the datasets used to evaluate Whisper, categorized by type. This is useful for researchers looking to replicate the paper's experiments.

    Short-form English-only

    • LibriSpeech: test-clean and test-other splits.
    • TED-LIUM 3: Test split with segmented manual transcripts.
    • Common Voice 5.1: English subset.
    • Artie: Subset of Common Voice.
    • CallHome & Switchboard: LDC2002S09 and LDC2002T43 (preprocessed via Kaldi scripts).
    • WSJ: LDC93S6B and LDC94S13B.
    • CORAAL: 231 interviews (v. 2021.07) with FairSpeech segmentations.
    • CHiME-6: Derived from CHiME-5 using s5_track1 recipe.
    • AMI-IHM, AMI-SDM1: Preprocessed AMI Corpus via s5b recipe.

    Long-form English-only

    • TED-LIUM 3: Sliced audio from full talks.
    • Meanwhile: 64 segments from The Late Show with Stephen Colbert (labels in meanwhile.json).
    • Rev16: 16 selected episodes from Rev.AI's Podcast Transcription Benchmark.
    • Kincaid46: 46 audio files from Descript's transcription accuracy article.
    • Earnings-21, Earnings-22: From the speech-datasets repository.
    • CORAAL: Full-length interview files and transcripts.

    Multilingual

    • Multilingual LibriSpeech (MLS): Test splits from various languages.
    • Fleurs: Collected via HuggingFace datasets implementation.
    • VoxPopuli: 14 languages collected via get_asr_data.py.
    • Common Voice 9: Official corpus.
    • CoVOST 2: X into English data.
  8. Available Whisper model sizes and types

    main

    Whisper provides several model sizes ranging from tiny to large, with different capabilities for English-only tasks versus multilingual tasks. There is also a turbo model optimized for inference speed.

    When choosing a model, consider the parameter count and whether you need multilingual support or English-only optimization. Note that large and turbo models do not have dedicated English-only versions in this list, but are multilingual.

    |  Size  | Parameters | English-only model | Multilingual model |  
    |:------:|:----------:|:------------------:|:------------------:|
    |  tiny  |    39 M    |         ✓          |         ✓          |
    |  base  |    74 M    |         ✓          |         ✓          |
    |  small  |   244 M    |         ✓          |         ✓          |
    |  medium |   769 M    |         ✓          |         ✓          |
    |  large  |   1550 M   |                    |         ✓          |
    |  turbo  |   798 M    |                    |         ✓          |
  9. Available Whisper models and memory requirements

    main

    Whisper offers several model sizes with different trade-offs between speed and accuracy. There are multilingual models and English-only (.en) versions for certain sizes.

    SizeParametersEnglish-onlyMultilingualRequired VRAMRelative Speed
    tiny39 Mtiny.entiny~1 GB~10x
    base74 Mbase.enbase~1 GB~7x
    small244 Msmall.ensmall~2 GB~4x
    medium769 Mmedium.enmedium~5 GB~2x
    large1550 MN/Alarge~10 GB1x
    turbo809 MN/Aturbo~6 GB~8x

    Notes:

    • .en models generally perform better for English-only tasks.
    • The turbo model is an optimized version of large-v3 that is faster with minimal accuracy loss, but it is not trained for translation tasks.
  10. Use Whisper via Command-Line Interface

    main

    You can transcribe audio files directly from the terminal.

    Basic transcription (using turbo model):

    whisper audio.flac audio.mp3 audio.wav --model turbo

    Transcribing specific languages:

    whisper japanese.wav --language Japanese

    Translating non-English speech into English: To translate, you must use a multilingual model (e.g., medium or large) instead of turbo. The turbo model will return the original language even if --task translate is specified.

    whisper japanese.wav --model medium --language Japanese --task translate

    Run whisper --help to see all available options.

  11. Normalize English text for evaluation

    main

    To ensure accurate Word Error Rate (WER) calculations, use EnglishTextNormalizer from whisper.normalizers to standardize both the hypothesis (transcription) and the reference (ground truth) text.

    from whisper.normalizers import EnglishTextNormalizer
    
    normalizer = EnglishTextNormalizer()
    clean_text = normalizer("Your transcription text here")
  12. Perform inference using `model.decode()`

    main

    To transcribe audio, pass log-mel spectrograms and a DecodingOptions object to the model.decode() method. The method returns a list of results containing the transcribed text.

    # Assuming 'mels' is a batch of log-mel spectrograms and 'options' is a DecodingOptions object
    results = model.decode(mels, options)
    
    # Extracting text from results
    for result in results:
        print(result.text)