Silero VAD

repository·master·Indexed 27 days ago

https://github.com/snakers4/silero-vad

An enterprise-grade, pre-trained Voice Activity Detector (VAD) designed for high accuracy and low latency. It supports PyTorch and ONNX runtimes, making it suitable for IoT, edge, and mobile applications. The library provides a Python API for extracting speech timestamps, streaming VAD via VADIterator, and tools for fine-tuning the model on custom datasets using .feather files.

Tokens
4.5K
Snippets
12
Records
29
Agent score
94%

What's inside silero-vad

  1. Load Silero-VAD annotated datasets using pandas

    master

    The Silero-VAD datasets are provided as .feather files containing speech annotations. You can load these files using the pandas library.

    All data is annotated with a temporal discretization of approximately 30 milliseconds (num_samples = 512).

    import pandas as pd
    dataframe = pd.read_feather(PATH_TO_FEATHER_FILE)
  2. Search for optimal threshold values

    master

    You can find the optimal input and output thresholds for your fine-tuned model using the search_thresholds script. This script uses the settings defined in your config.yml and applies the specified model to the validation dataset to find the best thresholds.

    python search_thresholds
  3. Install dependencies for Silero-VAD tuning

    master

    To tune the Silero-VAD model on custom data, ensure the following Python dependencies are installed:

    • torchaudio>=0.12.0
    • omegaconf>=2.3.0
    • sklearn>=1.2.0
    • torch>=1.12.0
    • pandas>=2.2.2
    • tqdm

    If you set use_torchhub: False in your configuration, you must also install the silero-vad library via pip.

    pip install silero-vad
  4. Use Silero VAD Python API

    master

    Use the silero-vad package to load a model, read an audio file, and extract speech timestamps. You can use the return_seconds=True argument to get timestamps in seconds instead of the default sample count.

    from silero_vad import load_silero_vad, read_audio, get_speech_timestamps
    
    model = load_silero_vad()
    wav = read_audio('path_to_audio_file')
    speech_timestamps = get_speech_timestamps(
      wav,
      model,
      return_seconds=True,  # Return speech timestamps in seconds (default is samples)
    )
  5. Install Silero VAD via torch.hub

    master

    Load Silero VAD directly from GitHub using PyTorch's hub mechanism. This is useful if you want to avoid installing the package via pip and prefer managing models through Torch Hub.

    import torch
    torch.set_num_threads(1)
    
    model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad', model='silero_vad')
    (get_speech_timestamps, _, read_audio, _, _) = utils
  6. Configure Audio I/O Backends

    master

    Silero VAD uses torchaudio for audio I/O (torchaudio.info, torchaudio.load, and torchaudio.save). You must ensure a proper audio backend is installed on your system. Choose one of the following options:

    • FFmpeg: conda install -c conda-forge 'ffmpeg<7'
    • sox_io: apt-get install sox (Tested on libsox 14.4.2)
    • soundfile: pip install soundfile
  7. Fine-tune the Silero-VAD model

    master

    To start the fine-tuning process using your config.yml settings, run the tune.py script. The process runs for the specified num_epochs, and the best checkpoint (based on ROC-AUC on the validation set) will be saved to the path specified in model_save_path in .jit format.

    python tune.py
  8. Prepare data for Silero-VAD tuning

    master

    Training and validation datasets must be prepared as .feather files. Each dataframe must contain the following mandatory columns:

    • audio_path: Absolute path to the audio file. Files should be PCM data, preferably in .wav or .opus formats. It is recommended to resample audio to 16000 Hz before training.
    • speech_ts: A list of dictionaries representing speech segments: [{'start': START_SEC, 'end': END_SEC}, ...], where times are in seconds. For high-quality tuning, use a precision of up to 30 milliseconds.

    Note: Audio files longer than max_train_length_sec will be truncated. For better efficiency, it is recommended to pre-cut long audio into segments of length max_train_length_sec.

  9. Configure tuning via config.yml

    master

    The config.yml file controls the fine-tuning process. Use the following keys:

    KeyDescription
    train_dataset_pathAbsolute path to the .feather training dataframe.
    val_dataset_pathAbsolute path to the .feather validation dataframe.
    jit_model_pathAbsolute path to the .jit Silero-VAD model. Leave empty to load from repository (depends on use_torchhub).
    use_torchhubIf True, loads model via torch.hub. If False, loads via silero-vad library.
    tune_8kIf True, fine-tunes the 8000 Hz head; otherwise, fine-tunes the 16000 Hz head.
    model_save_pathPath where the fine-tuned model will be saved.
    noise_lossLoss coefficient applied to non-speech audio windows.
    max_train_length_secMaximum audio length in seconds (longer audio will be truncated).
    aug_probProbability of applying augmentations during training.
    learning_rateFine-tuning learning rate.
    batch_sizeBatch size for training and validation.
    num_workersNumber of threads for data loading.
    num_epochsNumber of training epochs.
    deviceTarget device: cpu or cuda.
  10. Install silero-vad dependencies

    master
    To use Silero VAD, you need torchaudio and torch installed. You can also install the silero-vad package via pip to access high-level utilities like load_silero_vad, get_speech_timestamps, and VADIterator. If you intend to use the ONNX model, you must also install onnxruntime.