### Wespeaker Command Line Usage Examples Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/python_package.md Examples demonstrating how to use the Wespeaker command-line interface for various tasks such as embedding extraction, similarity calculation, and speaker diarization. Includes options for specifying input files, output files, and processing devices. ```shell $ wespeaker --task embedding --audio_file audio.wav --output_file embedding.txt ``` ```shell $ wespeaker --task embedding_kaldi --wav_scp wav.scp --output_file /path/to/embedding ``` ```shell $ wespeaker --task similarity --audio_file audio.wav --audio_file2 audio2.wav ``` ```shell $ wespeaker --task diarization --audio_file audio.wav ``` ```shell $ wespeaker --task diarization --audio_file audio.wav --device cuda:0 # use CUDA on Windows/Linux ``` ```shell $ wespeaker --task diarization --audio_file audio.wav --device mps # use Metal Performance Shaders on MacOS ``` -------------------------------- ### Install Wespeaker Python Package Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/python_package.md Instructions for installing the Wespeaker Python package, including standard installation via pip and development installation from a local clone. ```shell pip install git+https://github.com/wenet-e2e/wespeaker.git ``` ```shell git clone https://github.com/wenet-e2e/wespeaker.git cd wespeaker pip install -e . ``` -------------------------------- ### WeSpeaker Command Line Interface Examples (Bash) Source: https://context7.com/wenet-e2e/wespeaker/llms.txt This section provides examples of using the WeSpeaker command-line interface for various tasks, including embedding extraction, similarity computation, and speaker diarization. It shows how to process single files, multiple files from wav.scp, and utilize different pretrained models. ```bash # Extract embedding from single audio wespeaker --task embedding --audio_file audio.wav --output_file embedding.txt # Extract embeddings from Kaldi wav.scp (outputs .ark and .scp files) wespeaker --task embedding_kaldi --wav_scp wav.scp --output_file embeddings # Compute similarity between two audio files wespeaker --task similarity --audio_file audio1.wav --audio_file2 audio2.wav # Speaker diarization wespeaker --task diarization --audio_file meeting.wav --output_file result.rttm # Diarization on multiple files wespeaker --task diarization_list --wav_scp wav.scp --output_file results.rttm # Use specific pretrained model wespeaker --task embedding --audio_file audio.wav --campplus # CAM++ model wespeaker --task embedding --audio_file audio.wav --eres2net # ERes2Net model wespeaker --task embedding --audio_file audio.wav --vblinkp # VoxBlink2 SimAM ResNet34 wespeaker --task embedding --audio_file audio.wav --w2vbert2_mfa # W2V-BERT2.0 MFA # Use custom pretrained model wespeaker --task embedding --pretrain /path/to/model_dir --audio_file audio.wav # GPU/device selection wespeaker --task diarization --audio_file audio.wav --device cuda:0 wespeaker --task diarization --audio_file audio.wav --device mps # macOS Metal # Disable VAD wespeaker --task embedding --audio_file audio.wav --vad false ``` -------------------------------- ### Development Installation for WeSpeaker Source: https://context7.com/wenet-e2e/wespeaker/llms.txt Sets up WeSpeaker for development, including cloning the repository, creating a dedicated Conda environment, installing PyTorch with CUDA support, and installing project dependencies. ```bash git clone https://github.com/wenet-e2e/wespeaker.git cd wespeaker conda create -n wespeaker python=3.9 conda activate wespeaker conda install pytorch=1.12.1 torchaudio=0.12.1 cudatoolkit=11.3 -c pytorch -c conda-forge pip install -r requirements.txt ``` -------------------------------- ### wav.scp File Format Example Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md An example of the wav.scp file format, which maps unique wav IDs to their corresponding file paths. This format is used to reference audio files for processing. ```text id10001/1zcIwhmdeo4/00001.wav /exported/data/voxceleb1_wav_v2/id10001/1zcIwhmdeo4/00001.wav id10001/1zcIwhmdeo4/00002.wav /exported/data/voxceleb1_wav_v2/id10001/1zcIwhmdeo4/00002.wav ... ``` -------------------------------- ### Example wav.scp Format Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/voxconverse_diar.md This shows the expected format for `wav.scp` files used in speech processing. Each line contains a unique audio file ID followed by its absolute path, separated by a space. This format is commonly used to efficiently access audio data. ```text abjxc /path/to/wespeaker/examples/voxconverse/v2/data/dev/audio/abjxc.wav afjiv /path/to/wespeaker/examples/voxconverse/v2/data/dev/audio/afjiv.wav ... ``` -------------------------------- ### spk2utt File Format Example Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md An example of the spk2utt file format, which maps speaker IDs to a list of utterance IDs belonging to that speaker. This is the inverse mapping of utt2spk. ```text id10001 id10001/1zcIwhmdeo4/00001.wav id10001/1zcIwhmdeo4/00002.wav id10001/1zcIwhmdeo4/00003.wav ... id10002 id10002/0_laIeN-Q44/00001.wav id10002/6WO410QOeuo/00001.wav ... ... ``` -------------------------------- ### Start Neural Network Training (Shell) Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md Shell script to initiate neural network training using PyTorch's distributed training capabilities. It configures multi-GPU training, specifies the training script, experiment directory, and paths to training data, noise data, and reverb data. Supports resuming training from checkpoints and different data types. ```bash if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then echo "Start training ..." num_gpus=$(echo $gpus | awk -F ',' '{print NF}') torchrun --standalone --nnodes=1 --nproc_per_node=$num_gpus \ wespeaker/ssl/bin/train_dino.py --config $config \ --exp_dir ${exp_dir} \ --gpus $gpus \ --num_avg ${num_avg} \ --data_type "${data_type}" \ --train_data ${data}/vox2_dev/${data_type}.list \ --wav_scp ${data}/vox2_dev/wav.scp \ --reverb_data ${data}/rirs/lmdb \ --noise_data ${data}/musan/lmdb \ ${checkpoint:+--checkpoint $checkpoint} fi ``` -------------------------------- ### Command-line Usage Examples for WeSpeaker Source: https://github.com/wenet-e2e/wespeaker/blob/master/README.md Demonstrates various command-line operations for WeSpeaker, including extracting speaker embeddings from a single audio file or a list of files defined in a SCP format. It also shows how to compute similarity between two audio files and perform speaker diarization on an audio file. ```sh wespeaker --task embedding --audio_file audio.wav --output_file embedding.txt wespeaker --task embedding_kaldi --wav_scp wav.scp --output_file /path/to/embedding wespeaker --task similarity --audio_file audio.wav --audio_file2 audio2.wav wespeaker --task diarization --audio_file audio.wav ``` -------------------------------- ### utt2spk File Format Example Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md An example of the utt2spk file format, which maps utterance IDs to their corresponding speaker IDs. This file is essential for speaker-related tasks, although speaker labels are not used during self-supervised training. ```text id10001/1zcIwhmdeo4/00001.wav id10001 id10001/1zcIwhmdeo4/00002.wav id10001 ... ``` -------------------------------- ### trials File Format Example Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md An example of the trials file format, used for evaluating speaker verification systems. Each line contains an enrollment utterance ID, a test utterance ID, and a label ('target' or 'nontarget'). ```text id10001/Y8hIVOBuels/00001.wav id10001/1zcIwhmdeo4/00001.wav target id10001/Y8hIVOBuels/00001.wav id10943/vNCVj7yLWPU/00005.wav nontarget id10001/Y8hIVOBuels/00001.wav id10001/7w0IBEWc9Qw/00004.wav target id10001/Y8hIVOBuels/00001.wav id10999/G5R2-Hl7YX8/00008.wav nontarget ... ``` -------------------------------- ### Setup Conda Environment for WeSpeaker Development Source: https://github.com/wenet-e2e/wespeaker/blob/master/README.md Creates and activates a Conda environment named 'wespeaker' with Python 3.9. It then installs specific versions of PyTorch, Torchaudio, and CUDA, followed by project dependencies listed in 'requirements.txt', and finally sets up pre-commit hooks for code quality. ```sh conda create -n wespeaker python=3.9 conda activate wespeaker conda install pytorch=1.12.1 torchaudio=0.12.1 cudatoolkit=11.3 -c pytorch -c conda-forge pip install -r requirements.txt pre-commit install ``` -------------------------------- ### Training Configuration Example (YAML) Source: https://context7.com/wenet-e2e/wespeaker/llms.txt This is an example of a YAML configuration file for training a speaker embedding model using the ResNet34 architecture. It specifies parameters for the experiment directory, GPUs, data augmentation, model architecture, loss function, optimizer, and learning rate scheduler. ```yaml exp_dir: exp/ResNet34-TSTP-emb256 gpus: "[0,1]" num_avg: 10 enable_amp: False seed: 42 num_epochs: 150 save_epoch_interval: 5 dataloader_args: batch_size: 128 num_workers: 16 pin_memory: False drop_last: True dataset_args: shuffle: True resample_rate: 16000 speed_perturb: True num_frms: 200 aug_prob: 0.6 fbank_args: num_mel_bins: 80 frame_shift: 10 frame_length: 25 dither: 1.0 model: ResNet34 model_args: feat_dim: 80 embed_dim: 256 pooling_func: "TSTP" two_emb_layer: False projection_args: project_type: "arc_margin" scale: 32.0 easy_margin: False loss: CrossEntropyLoss optimizer: SGD optimizer_args: momentum: 0.9 weight_decay: 0.0001 scheduler: ExponentialDecrease scheduler_args: initial_lr: 0.1 final_lr: 0.00005 warm_up_epoch: 6 ``` -------------------------------- ### Install WeSpeaker Python Package Source: https://github.com/wenet-e2e/wespeaker/blob/master/README.md Installs the WeSpeaker Python package using pip from its GitHub repository. This command fetches and installs the latest version of the library, making its functionalities available for use in Python projects. ```sh pip install git+https://github.com/wenet-e2e/wespeaker.git ``` -------------------------------- ### Speaker Diarization Setup in Python Source: https://context7.com/wenet-e2e/wespeaker/llms.txt Initializes the WeSpeaker model for speaker diarization. This involves loading the model and setting the computation device, typically a GPU for performance. ```python import wespeaker model = wespeaker.load_model('english') model.set_device('cuda:0') ``` -------------------------------- ### Project Setup and Options (CMake) Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/mnn/CMakeLists.txt Initializes the CMake build system for the Wespeaker project. It sets the minimum required CMake version, project name and version, and defines build options such as MNN integration and minimum library compilation. It also configures C++ standard and threading flags. ```cmake cmake_minimum_required(VERSION 3.14) project(wespeaker VERSION 0.1) set(MNN ON CACHE BOOL "whether to build with MNN") option(MINI_LIBS "whether to build minimum libraies with MNN" OFF) set(CMAKE_VERBOSE_MAKEFILE OFF) include(FetchContent) set(FETCHCONTENT_QUIET OFF) get_filename_component(fc_base "fc_base" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set(FETCHCONTENT_BASE_DIR ${fc_base}) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fPIC") # Include all dependency if(MNN) include(mnn) endif() include(glog) include(gflags) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # build all libraries add_subdirectory(utils) add_subdirectory(frontend) add_subdirectory(speaker) add_subdirectory(bin) ``` -------------------------------- ### Wespeaker Python Programming Usage Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/python_package.md Illustrates how to use the Wespeaker library within Python scripts. Shows model loading, device setting, and performing tasks like extracting embeddings, computing similarity, and diarization. Also includes examples for speaker registration and recognition. ```python import wespeaker model = wespeaker.load_model('chinese') # set the device on which tensors are or will be allocated. model.set_device('cuda:0') # embedding/embedding_kaldi/similarity/diarization embedding = model.extract_embedding('audio.wav') ut_names, embeddings = model.extract_embedding_list('wav.scp') similarity = model.compute_similarity('audio1.wav', 'audio2.wav') di_result = model.diarize('audio.wav', 'give_this_utt_a_name') # register and recognize model.register('spk1', 'spk1_audio1.wav') model.register('spk2', 'spk2_audio1.wav') model.register('spk3', 'spk3_audio1.wav') result = model.recognize('spk1_audio2.wav') ``` -------------------------------- ### Run Diarization Recipe Stages Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/voxconverse_diar.md This script executes the diarization recipe for VoxConverse v2 in stages. Each stage performs a specific part of the diarization process, allowing for step-by-step understanding and verification. Ensure you are in the correct directory before running. ```bash cd examples/voxconverse/v2/ bash run.sh --stage 1 --stop_stage 1 bash run.sh --stage 2 --stop_stage 2 bash run.sh --stage 3 --stop_stage 3 bash run.sh --stage 4 --stop_stage 4 bash run.sh --stage 5 --stop_stage 5 bash run.sh --stage 6 --stop_stage 6 bash run.sh --stage 7 --stop_stage 7 bash run.sh --stage 8 --stop_stage 8 ``` -------------------------------- ### Prepare Datasets for VoxCeleb (Shell) Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox.md This shell script prepares datasets for the VoxCeleb experiment, including voxceleb1, voxceleb2, MUSAN (noise), and RIRS_NOISES (reverberation). It calls a helper script `local/prepare_data.sh` and suggests manual download of archives. ```default if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then echo "Prepare datasets ..." ./local/prepare_data.sh --stage 2 --stop_stage 4 --data ${data} fi ``` -------------------------------- ### Execute VoxCeleb v2 Recipe Stages (Shell) Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox.md This snippet shows how to execute the `run.sh` script for the VoxCeleb v2 recipe in stages. Each command runs a specific stage of the experiment, allowing for step-by-step understanding and verification. ```default cd examples/voxceleb/v2/ bash run.sh --stage 1 --stop_stage 1 bash run.sh --stage 2 --stop_stage 2 bash run.sh --stage 3 --stop_stage 3 bash run.sh --stage 4 --stop_stage 4 bash run.sh --stage 5 --stop_stage 5 bash run.sh --stage 6 --stop_stage 6 bash run.sh --stage 7 --stop_stage 7 bash run.sh --stage 8 --stop_stage 8 ``` -------------------------------- ### Execute DINO Recipe Stages Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md This script executes the DINO self-supervised speaker verification recipe in stages. It is recommended to run each stage sequentially to understand the process. Ensure you are in the correct directory before execution. ```bash cd examples/voxceleb/v3/dino bash run.sh --stage 1 --stop_stage 1 bash run.sh --stage 2 --stop_stage 2 bash run.sh --stage 3 --stop_stage 3 bash run.sh --stage 4 --stop_stage 4 bash run.sh --stage 5 --stop_stage 5 bash run.sh --stage 6 --stop_stage 6 ``` -------------------------------- ### Extract Embeddings from Trained Model (Python/Bash) Source: https://context7.com/wenet-e2e/wespeaker/llms.txt This section shows how to extract speaker embeddings using a trained model. It provides both a command-line interface example using the `extract.py` script and a Python API example using the `extract` function, specifying model paths, data lists, and output file locations. ```bash # Command line extraction python wespeaker/bin/extract.py --config exp/model/config.yaml \ --model_path exp/model/avg_model.pt \ --data_type "shard" \ --data_list data/test/shard.list \ --batch_size 32 \ --num_workers 8 \ --embed_ark exp/embeddings/xvector.ark ``` ```python # Python API for batch extraction from wespeaker.bin.extract import extract extract( config='exp/model/config.yaml', model_path='exp/model/avg_model.pt', data_type='shard', data_list='data/test/shard.list', embed_ark='exp/embeddings/xvector.ark', batch_size=32, num_workers=8 ) # Outputs: xvector.ark and xvector.scp files ``` -------------------------------- ### Download and Prepare VoxConverse Data Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/voxconverse_diar.md This script downloads and prepares the VoxConverse 2020 dataset, including annotations and audio files for both development and test sets. It also generates `wav.scp` files, which are essential for linking audio file IDs to their respective paths, a common format in speech processing toolkits. ```bash if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then mkdir -p data # Download annotations for dev and test sets (version 0.0.3) wget -c https://github.com/joonson/voxconverse/archive/refs/heads/master.zip -O data/voxconverse_master.zip unzip -o data/voxconverse_master.zip -d data # Download dev audios mkdir -p data/dev wget --no-check-certificate -c https://www.robots.ox.ac.uk/~vgg/data/voxconverse/data/voxconverse_dev_wav.zip -O data/voxconverse_dev_wav.zip unzip -o data/voxconverse_dev_wav.zip -d data/dev # Create wav.scp for dev audios ls `pwd`/data/dev/audio/*.wav | awk -F/ '{print substr($NF, 1, length($NF)-4), $0}' > data/dev/wav.scp # Test audios mkdir -p data/test wget --no-check-certificate -c https://www.robots.ox.ac.uk/~vgg/data/voxconverse/data/voxconverse_test_wav.zip -O data/voxconverse_test_wav.zip unzip -o data/voxconverse_test_wav.zip -d data/test # Create wav.scp for test audios ls `pwd`/data/test/voxconverse_test_wav/*.wav | awk -F/ '{print substr($NF, 1, length($NF)-4), $0}' > data/test/wav.scp fi ``` -------------------------------- ### Neural Network Training with torchrun Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox.md Initiates neural network training using PyTorch Distributed Data Parallel (DDP) mode. It leverages `torchrun` to manage multiple GPU processes. The configuration for training, including model architecture, optimization, and dataset, is specified in a YAML file. Training can be resumed from a checkpoint. ```shell if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then echo "Start training ..." num_gpus=$(echo $gpus | awk -F ',' '{print NF}') torchrun --standalone --nnodes=1 --nproc_per_node=$num_gpus \ wespeaker/bin/train.py --config $config \ --exp_dir ${exp_dir} \ --gpus $gpus \ --num_avg ${num_avg} \ --data_type "${data_type}" \ --train_data ${data}/vox2_dev/${data_type}.list \ --train_label ${data}/vox2_dev/utt2spk \ --reverb_data ${data}/rirs/lmdb \ --noise_data ${data}/musan/lmdb \ ${checkpoint:+--checkpoint $checkpoint} fi ``` -------------------------------- ### Download Diarization Prerequisites Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/voxconverse_diar.md This script downloads essential tools and models for speaker diarization. It includes the SCTK evaluation toolkit for DER calculation, a VAD model from silero-vad to remove silence, and a pre-trained ResNet34 model for speaker embeddings. These are crucial for the subsequent stages of the tutorial. ```bash if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then mkdir -p external_tools # [1] Download evaluation toolkit wget -c https://github.com/usnistgov/SCTK/archive/refs/tags/v2.4.12.zip -O external_tools/SCTK-v2.4.12.zip unzip -o external_tools/SCTK-v2.4.12.zip -d external_tools # [2] Download voice activity detection model pretrained by Silero Team wget -c https://github.com/snakers4/silero-vad/archive/refs/tags/v3.1.zip -O external_tools/silero-vad-v3.1.zip unzip -o external_tools/silero-vad-v3.1.zip -d external_tools # [3] Download ResNet34 speaker model pretrained by WeSpeaker Team mkdir -p pretrained_models wget -c https://wespeaker-1256283475.cos.ap-shanghai.myqcloud.com/models/voxceleb/voxceleb_resnet34_LM.onnx -O pretrained_models/voxceleb_resnet34_LM.onnx fi ``` -------------------------------- ### Build WeSpeaker Client Docker Image Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/runtime.md Builds the Docker image for the WeSpeaker client application. This command prepares the client environment, allowing it to connect to the WeSpeaker server for tasks such as extracting audio embeddings. It uses a dedicated Dockerfile for the client. ```bash # client docker build . -f Dockerfile/dockerfile.client -t wespeaker_client:latest --network host ``` -------------------------------- ### Build Wespeaker Server Docker Image Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/server/x86_gpu/README.md Builds the Docker image for the Wespeaker server using the provided Dockerfile. This image contains the necessary components to run the inference server. ```docker docker build . -f Dockerfile/dockerfile.server -t wespeaker:latest --network host ``` -------------------------------- ### Test Single Audio with Python Client Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/server/diarization_gpu/README.md This script tests a single audio file by sending it to the Wespeaker server. It requires the audio file path, server URL, and an output directory. The output is collected in an RTTM file. ```bash export output_directory="output" mkdir -p $output_directory python client.py --url=localhost:8001 --audio_file=/ws/test_data/abjxc.wav --output_directory=$output_directory cat $output_directory/rttm* > $output_directory/rttm ``` -------------------------------- ### Sample Pair Composition for Self-Supervised Learning (Python) Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md Python code illustrating how sample pairs are constructed for different self-supervised training methods. For SimCLR and MoCo, two segments are randomly taken from each sentence. For DINO, multiple short and long segments are cropped to form a positive pair, enabling the model to learn representations without explicit labels. ```python # For SimCLR and MoCo, we take 2 segments from each sentence randomly. # For DINO, we will crop 2 short and 4 long segments to form a positive pair. ``` -------------------------------- ### Export Trained Model to JIT Format - Python Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox_ssl.md Exports the trained WeSpeaker model to the JIT format using Libtorch for C++ inference. This script takes the configuration file and checkpoint path as input and outputs a zipped model file. ```python if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then echo "Export the best model ..." python wespeaker/bin/export_jit.py \ --config $exp_dir/config.yaml \ --checkpoint $exp_dir/models/avg_model.pt \ --output_file $exp_dir/models/final.zip fi ``` -------------------------------- ### Build WeSpeaker Server Docker Image Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/runtime.md Builds the Docker image for the WeSpeaker server. This command compiles the necessary components and sets up the environment for running the Triton inference server with the WeSpeaker model. It uses a specific Dockerfile and ensures network connectivity. ```bash # server docker build . -f Dockerfile/dockerfile.server -t wespeaker:latest --network host ``` -------------------------------- ### Configure Speaker Library Build with ONNX/MNN Backends Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/core/speaker/CMakeLists.txt This snippet sets up the build for the 'speaker' library. It conditionally appends source files and links libraries based on the ONNX and MNN build flags. If neither ONNX nor MNN is enabled, it throws a fatal error. It links the 'frontend' library and conditionally links 'onnxruntime' or 'MNN'. ```cmake set(speaker_srcs speaker_engine.cc) if(NOT ONNX AND NOT MNN) message(FATAL_ERROR "Please build with ONNX or MNN!") endif() if(ONNX) list(APPEND speaker_srcs onnx_speaker_model.cc) endif() if(MNN) list(APPEND speaker_srcs mnn_speaker_model.cc) endif() add_library(speaker STATIC ${speaker_srcs}) target_link_libraries(speaker PUBLIC frontend) if(ONNX) target_link_libraries(speaker PUBLIC onnxruntime) endif() if(MNN) target_link_libraries(speaker PUBLIC MNN) endif() ``` -------------------------------- ### Test Multiple Audios with Python Client using wav.scp Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/server/diarization_gpu/README.md This script tests multiple audio files specified in a wav.scp file. It requires the path to the wav.scp file, server URL, and an output directory. The results are aggregated into an RTTM file. ```bash export wav_scp_dir=/ws/test_data python client.py --url=localhost:8001 --wavscp=$wav_scp_dir/wav.scp --output_directory="outp" cat $output_directory/rttm* > $output_directory/rttm ``` -------------------------------- ### Perform Large Margin Finetuning Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox.md Initiates large margin finetuning to further improve model performance, as described in the paper (https://arxiv.org/pdf/2010.11255.pdf). This process involves copying a pre-trained model and running a shell script with specific parameters for LM training. ```shell if [ ${stage} -le 8 ] && [ ${stop_stage} -ge 8 ]; then echo "Large margin fine-tuning ..." lm_exp_dir=${exp_dir}-LM mkdir -p ${lm_exp_dir}/models # Use the pre-trained average model to initialize the LM training cp ${exp_dir}/models/avg_model.pt ${lm_exp_dir}/models/model_0.pt bash run.sh --stage 3 --stop_stage 7 \ --data ${data} \ --data_type ${data_type} \ --config ${lm_config} \ --exp_dir ${lm_exp_dir} \ --gpus $gpus \ --num_avg 1 \ --checkpoint ${lm_exp_dir}/models/model_0.pt \ --trials "$trials" \ --score_norm_method ${score_norm_method} \ --top_n ${top_n} fi ``` -------------------------------- ### Export Trained Model with Libtorch Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/vox.md Exports the best trained model using `wenet/bin/export_jit.py` for C++ inference. This script requires a configuration file and a trained checkpoint, outputting a zip file containing the model. ```shell if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then # Export the best model you want python wenet/bin/export_jit.py \ --config $dir/train.yaml \ --checkpoint $dir/avg_${average_num}.pt \ --output_file $dir/final.zip fi ``` -------------------------------- ### Add Frontend Library Sources (CMake) Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/core/frontend/CMakeLists.txt This CMake command adds the specified C++ source files to the 'frontend' static library. It defines the core components of the frontend processing pipeline. ```cmake add_library(frontend STATIC feature_pipeline.cc fft.cc ) ``` -------------------------------- ### Link Frontend Library Dependencies (CMake) Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/core/frontend/CMakeLists.txt This CMake command links the 'frontend' library with the 'utils' library. It ensures that any symbols or functionality required by the frontend from the 'utils' library are available during the build process. ```cmake target_link_libraries(frontend PUBLIC utils) ``` -------------------------------- ### Build Wespeaker Client Docker Image Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/server/x86_gpu/README.md Builds the Docker image for the Wespeaker client. This image is used to run client-side operations, such as generating embeddings and testing the pipeline. ```docker docker build . -f Dockerfile/dockerfile.client -t wespeaker_client:latest --network host ``` -------------------------------- ### Compile asv_main Executable with speaker Library Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/core/bin/CMakeLists.txt This CMake snippet defines the 'asv_main' executable and links it against the 'speaker' library. It assumes 'asv_main.cc' is the source file and 'speaker' is a pre-configured target library. ```cmake add_executable(asv_main asv_main.cc) target_link_libraries(asv_main PUBLIC speaker) ``` -------------------------------- ### Test Whole WeSpeaker Pipeline Performance in Client Docker Source: https://github.com/wenet-e2e/wespeaker/blob/master/docs/runtime.md Evaluates the end-to-end performance of the WeSpeaker pipeline within a client Docker environment. This involves generating test input data from an audio file and then running perf_analyzer to measure throughput and latency under various concurrency levels. ```bash cd client/ # generate test input python3 generate_input.py --audio_file=test.wav --seconds=2.02 perf_analyzer -m speaker -b 1 --concurrency-range 200:1000:200 --input-data=input.json -u localhost:8000 ``` -------------------------------- ### Train Speaker Embedding Models (Bash) Source: https://context7.com/wenet-e2e/wespeaker/llms.txt This section outlines commands for training speaker embedding models using distributed training. It covers single-node multi-GPU training and multi-node training configurations, specifying training data, experiment directory, and GPU allocation. ```bash # Single node multi-GPU training torchrun --nnodes=1 --nproc_per_node=2 \ --rdzv_id=2024 --rdzv_backend="c10d" --rdzv_endpoint="localhost:29400" \ wespeaker/bin/train.py --config conf/resnet.yaml \ --exp_dir exp/ResNet34 \ --gpus "[0,1]" \ --num_avg 10 \ --data_type "shard" \ --train_data data/vox2_dev/shard.list \ --train_label data/vox2_dev/utt2spk \ --reverb_data data/rirs/lmdb \ --noise_data data/musan/lmdb # Multi-node training torchrun --nnodes=2 --nproc_per_node=4 \ --rdzv_id=2024 --rdzv_backend="c10d" --rdzv_endpoint="master_ip:29400" \ wespeaker/bin/train.py --config conf/resnet.yaml \ --exp_dir exp/ResNet34 --gpus "[0,1,2,3]" ... ``` -------------------------------- ### Configure Diarization and Perform Diarization (Python) Source: https://context7.com/wenet-e2e/wespeaker/llms.txt This Python snippet demonstrates how to configure diarization parameters such as minimum segment duration and window size, and then perform diarization on a single audio file. The results are then printed and can be saved in RTTM format. ```python from wespeaker.utils.config import load_config from wespeaker.recognizer.speaker_diarizer import Diarizer # Load configuration (optional, can also set params directly) # config = load_config('path/to/diarizer_config.yaml') # Initialize diarizer model = Diarizer() # Configure diarization parameters model.set_diarization_params( min_duration=0.255, # Minimum segment duration window_secs=1.5, # Sliding window size period_secs=0.75, # Sliding window step frame_shift=10, # Frame shift in ms batch_size=32, # Embedding extraction batch size subseg_cmn=True # Apply CMN per subsegment ) # Perform diarization on single file diar_result = model.diarize('meeting_audio.wav', 'meeting001') # Print results: [(utt_id, start_time, end_time, speaker_id), ...] for utt_id, start, end, speaker in diar_result: print(f"Speaker {speaker}: {start:.3f}s - {end:.3f}s") # Save results in RTTM format model.make_rttm(diar_result, 'output.rttm') # Process multiple files from wav.scp utts, segment2labels = model.diarize_list('wav.scp') ``` -------------------------------- ### CMake: Project Configuration and Options Source: https://github.com/wenet-e2e/wespeaker/blob/master/runtime/onnxruntime/CMakeLists.txt Configures the minimum CMake version, project name and version, and sets build options for ONNX and GPU support. It also sets the C++ standard and includes directories. ```cmake cmake_minimum_required(VERSION 3.14) project(wespeaker VERSION 0.1) option(ONNX "whether to build with ONNX" ON) option(GPU "whether to build with GPU" OFF) set(CMAKE_VERBOSE_MAKEFILE OFF) include(FetchContent) set(FETCHCONTENT_QUIET OFF) get_filename_component(fc_base "fc_base" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") set(FETCHCONTENT_BASE_DIR ${fc_base}) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fPIC") # Include all dependency if(ONNX) include(onnx) endif() include(glog) include(gflags) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # build all libraries add_subdirectory(utils) add_subdirectory(frontend) add_subdirectory(speaker) add_subdirectory(bin) ```