TeleSpeech-ASR

repository·master·Indexed 21 days ago

https://github.com/tele-ai/telespeech-asr

A large-scale speech recognition model supporting approximately 30 Chinese dialects, including Cantonese, Shanghainese, Sichuanese, and Wenzhou. It provides unsupervised pre-trained models (base and large) and fine-tuned models (KeSpeech). The project includes frameworks for fairseq-based fine-tuning and wenet-based representation extraction, supporting both continuous and discrete unit training for downstream ASR tasks using Conformer and Transformer architectures.

Tokens
5.8K
Snippets
12
Records
18
Agent score
74%

What's inside TeleSpeech-ASR

  1. Overview of the Representation Training Framework

    master

    The framework is built on top of wenet and provides two primary methods for using pre-trained models in downstream ASR tasks:

    1. Continuous Representation Training: Extracts intermediate layer representations from a pre-trained model to serve as feature inputs for ASR models.
    2. Discrete Unit Training: Generates discrete units (tokens) from a pre-trained model to be used as inputs for discrete ASR models.

    Supported ASR Models & Decoding:

    • Architectures: Conformer, Transformer. You can decouple representation training from the ASR model, allowing you to add other downstream models like Paraformer or Branchformer.
    • Decoding Methods: ctc_greedy_search, attention, and attention_rescore.
  2. Overview of TeleSpeech-ASR Models

    master

    TeleSpeech-ASR is a large-scale multi-dialect speech recognition model pre-trained on 300,000 hours of unlabeled multi-dialect speech. It supports approximately 30 dialects, including Cantonese, Shanghainese, Sichuanese, and Wenzhou dialect.

    Available Models:

    Model VersionParametersDownload LinkDictionaryNotes
    pretrain_base0.09 BLinkUnsupervised pre-trained model. Cannot be used for ASR directly.
    pretrain_large0.3 BLinkUnsupervised pre-trained model. Cannot be used for ASR directly.
    finetune_large_kespeech0.3 BLinkdict.char7531.txtFine-tuned on KeSpeech (8 dialects). Ready for direct ASR inference.

    Usage Note: Pre-trained models require supervised fine-tuning on labeled data before they can perform ASR tasks. You can use either the fairseq-based fine-tuning framework or the wenet-based representation extraction framework.

  3. Prepare Audio Features and Data Lists

    master

    The model requires 40-dimensional MFCC features extracted from 16K sampling rate audio, not raw audio.

    1. Feature Extraction

    Use Kaldi to extract 40-dim MFCC features. You can run the prepare_kaldi_feats.sh script by placing it in a Kaldi egs directory alongside a configuration file like mfcc_hires.conf.

    Note: If you already have extracted features, you can replace kaldiio.load_mat with the method from wenet/kaldi_io.py to avoid requiring a Kaldi installation during runtime.

    2. Create Data Lists

    Prepare a data.list file (or .tsv for fine-tuning) where each line is tab-separated (\t) following this format:

    utt:<utt_id>\tfeat:<path_to_ark>:<offset>\tfeat_shape:<frames>,<dim>\ttext:<transcription>\ttoken:<tokens>\ttokenid:[TOKENID]\ttoken_shape:<len>,<vocab_size>

    Example train/data.list:

    utt:X0000000000_100638174_S00037	feat:/data/raw_nnaudio.test.1.ark:2983479385	feat_shape:363,40	text:不惜在这种试验中毁灭包括自己在内的一切	token:不 惜 在 这 种 试 验 中 毁 灭 包 括 自 己 在 内 的 一 切	tokenid:[TOKENID]	token_shape:19,5537
  4. Perform Inference and Decoding

    master

    For Fine-tuned Models (e.g., finetune_large_kespeech)

    1. Prepare your .tsv data files (following the data.list format).
    2. Modify run_scripts/decode.sh to set the correct model path and test data paths.
      • Use dataset.gen_subset to specify the names of the .tsv files to be tested (multiple files can be configured).
    3. Execute the decoding script:
    $ bash run_scripts/decode.sh

    For Pre-trained Models (Post-Fine-tuning)

    If you have just finished fine-tuning a pre-trained model, follow the same steps as above using the newly fine-tuned model weights.

  5. Fine-tune Pre-trained Models (Fairseq)

    master

    To fine-tune a pretrain_base or pretrain_large model using the fairseq framework:

    1. Environment Setup: Update data2vec_dialect/path.sh to point to your fairseq installation path.
    2. Data Preparation: Extract MFCC features and create .tsv files (e.g., train.tsv, dev.tsv) by symlinking your data.list files.
    3. Configuration:
      • Update run_scripts/run_d2v_finetune.sh with your specific paths.
      • Set task.data to the directory containing your .tsv files (e.g., task.data=/data/wenetspeech/train).
    4. Execution: Run the fine-tuning script from the data2vec_dialect directory:
    $ bash run_scripts/run_d2v_finetune.sh
  6. Train Downstream ASR Tasks (Wenet Representation)

    master

    This method uses the pre-trained model as a feature extractor for downstream ASR training via the wenet_representation framework.

    1. Setup: Enter wenet_representation and update path.sh with the correct paths for fairseq, data2vec_dialect, and wenet_representation.

    2. Continuous Representation Training & Decoding:

      • Configure dataset details in run_d2v.sh.
      • Run: bash run_d2v.sh
    3. Discrete Representation Training & Decoding:

      • Prepare discrete representation training files (data.list.discrete) based on your data.list.
      • Update wenet/discrete_token/kmeans_d2v.yaml with the correct model_dir and user_dir.
      • Run feature dumping: bash wenet/discrete_token/dump_feat.sh
      • Configure dataset details in run_discrete.sh.
      • Run: bash run_discrete.sh
  7. Install TeleSpeech-ASR Dependencies

    master

    To use TeleSpeech-ASR, ensure you have the following environment requirements:

    • PyTorch version >= 1.13.0
    • Python version >= 3.8
    • Kaldi installed (required for data preparation and training).

    Step 1: Install fairseq

    $ git clone https://github.com/pytorch/fairseq
    $ cd fairseq
    $ pip install --editable ./

    Step 2: Install project requirements

    $ pip install -r requirements.txt

    Minimal Installation (Fine-tuning/Decoding only): If you only need to perform fine-tuning or decoding via fairseq, you can install only the essential dependencies:

    $ pip install kaldiio timm editdistance soundfile
    $ git clone https://github.com/pytorch/fairseq
    $ cd fairseq
    $ pip install --editable ./
    $ pip install -r requirements.txt
  8. Configure Discrete Unit Training

    master

    Discrete unit training involves two main steps: extracting features via K-Means and then training the discrete ASR model.

    1. Feature Extraction (K-Means)

    Use the script discrete_token/dump_feat.sh with the following parameters:

    • train_km_set: The dataset used to train the K-Means model.
    • percent: The fraction of data to use for training (e.g., 0.1 for 10%). Use -1 to use the entire dataset.
    • input_type: Supports original audio wav.scp files or Wenet data.list files.
    • feat_save_type: The format for saving representations (npy or kaldi).

    Configure K-Means via discrete_token/kmeans_d2v.yaml:

    • reader_conf: Settings should be largely consistent with frontend_conf in the continuous training config.
      • max_chunk: If set, audio is truncated by max_chunk during representation extraction. Useful for saving GPU memory.
      • weights: Layer weights used when multilayer_feature is true. Weights can be obtained from continuous representation training.
    • kmeans_conf: Parameters for MiniBatchKMeans.
      • n_clusters: The number of clusters (vocabulary size).

    2. Discrete ASR Training

    Use conf/train_d2v2_discrete_conformer.yaml for training:

    • frontend_conf:
      • input_size: Must match the n_clusters value used during K-Means.
      • padding_idx: The padding index used during training.
    • Other settings are identical to the continuous representation training configuration (conf/train_d2v2_ark_conformer.yaml).
  9. Configure Continuous Representation Training

    master

    Continuous representation training is configured via YAML files, such as conf/train_d2v2_ark_conformer.yaml. Key configuration sections include:

    frontend_conf (Pre-trained Model Settings)

    • finetune_model: Set to true if the model in moder_dir is a fine-tuned model; set to false to use an unsupervised pre-trained model.
    • multilayer_feature: Boolean. If true, uses weighted calculation across multiple layers.
    • layer: Determines which layers to use. Combined with multilayer_feature, it supports four modes:
      1. layer: [-1] and multilayer_feature: true: Uses the weighted sum of all layers.
      2. layer: [-1] and multilayer_feature: false: Uses only the last layer's representation.
      3. layer: [specific_values] and multilayer_feature: true: Calculates the weighted sum of only the specified layers.
      4. layer: [specific_values] and multilayer_feature: false: Calculates the average of the specified layers.
    • num_layer: Total number of layers in the model. Note: For unsupervised pre-trained models, this is layers + 1; for fine-tuned models, it is layers.

    preencoder_conf (Dimensionality Reduction)

    • Comment this section out to disable dimensionality reduction.

    spec_aug_after_conf (Representation Dropout)

    • scale: Determines if the mask length should be scaled according to the dimension. Comment this section out to disable.

    encoder_conf (Encoder Settings)

    • input_layer: Use conv2d2 to enable 2x downsampling.

    dataset_conf (Dataset Settings)

    • cmvn: Normalization settings for data sent to the pre-trained model. This must match the settings used during pre-training.
    • max_length: Set based on positional encoding (default 5000). For data2vec models with 4x downsampling, this can be up to 20000. If input_layer is conv2d2, use max_length * 2.
    • Note: speed_perturb and spec_aug are not used for MFCC feature inputs.
    # Example configuration structure (conceptual)
    frontend_conf:
      finetune_model: true
      multilayer_feature: true
      layer: [-1]
      num_layer: 12
    preencoder_conf: {}
    spec_aug_after_conf:
      scale: true
    encoder_conf:
      input_layer: "conv2d2"
    dataset_conf:
      cmvn: true
      max_length: 10000
  10. Configure Distributed Data Parallel (DDP) training

    master

    To run training across multiple GPUs, use the --ddp.* flags. The script uses torch.distributed to coordinate processes.

    Required flags for DDP:

    • --ddp.world_size: Total number of GPUs/processes.
    • --ddp.rank: The global rank of the current process.
    • --ddp.dist_backend: The backend to use (nccl is recommended for NVIDIA GPUs).
    • --ddp.init_method: The method used to initialize the process group.

    If using DDP with FP16, you can also enable --fp16_grad_sync to use FP16 gradient compression for communication.

    # Example for 4-GPU training on a single node
    python wenet/bin/train.py \
        --config config.yaml \
        --train_data train.data \
        --cv_data cv.data \
        --model_dir ./output \
        --symbol_table symbols.txt \
        --gpu 0 \
        --ddp.rank 0 \
        --ddp.world_size 4 \
        --ddp.dist_backend nccl
  11. Train or fine-tune models with train.py

    master

    The train.py script is the primary CLI entrypoint for training or fine-tuning ASR models. It supports single-GPU, multi-GPU (DDP), and CPU training. It can initialize models from a configuration file, load existing checkpoints, or initialize specific encoder modules from pre-trained models.

    Key capabilities include:

    • Distributed Training: Supports nccl or gloo backends via DDP.
    • Mixed Precision: Supports automatic mixed precision (--use_amp) and FP16 gradient synchronization for DDP (--fp16_grad_sync).
    • Fine-tuning: Allows freezing specific parameters via the freeze_param key in the YAML config or initializing the encoder with --enc_init.
    • Logging: Integrates with TensorBoard for tracking loss, learning rate, and CER.
    python wenet/bin/train.py \
        --config your_config.yaml \
        --train_data train_data_file \
        --cv_data cv_data_file \
        --model_dir output_model_dir \
        --symbol_table symbol_table_file
  12. Prepare Dictionaries for Fine-tuning and Representation Training

    master

    Depending on your task, you need to prepare a dictionary file in a specific format.

    For Fairseq Fine-tuning

    Prepare a dict.${label}.txt file (e.g., dict.ltr.txt for letter-based modeling). Each line contains a token followed by its frequency:

    是 2
    好 3

    For Wenet Representation Training

    Prepare a lang_char.txt file. This format is similar to the fairseq dictionary but must include three additional special tokens: <blank>, , and <sos/eos>:

    <blank> 0
    拚 1
    是 2
    好 3
    ...
    <sos/eos> 5536