)
fairseq 中 wav2vec 2.0 的自监督语音预训练与微调实战指南附 vq-wav2vec 与 TPU 训练【免费下载链接】unilmLarge-scale Self-supervised Pre-training Across Tasks, Languages, and Modalities项目地址: https://gitcode.com/GitHub_Trending/un/unilm导读本文以 kosmos-2/fairseq/examples/wav2vec/README.md 为核心系统讲解在 fairseq 框架中从零训练 wav2vec 2.0 预训练模型、基于 CTC 目标微调、解码评估以及提取嵌入特征的完整流程。同时结合仓库内 wav2vec_manifest.py、libri_labels.py、config/pretraining 与 config/finetuning 等源码与配置文件展开参数级讲解。读完本文你将掌握数据清单manifest制作、Base/Large 模型预训练、CTC 微调、语言模型解码、嵌入提取以及 Google Cloud TPU 训练等完整实战技能。wav2vec 2.0从连续对比学习到离散化的自监督语音表征wav2vec 2.0 的核心思想是在无标注数据上学习语音表示论文出自wav2vec 2.0: A Framework for Self-Supervised Learning of Speech RepresentationsBaevski et al., 2020。随后这一框架被快速扩展为一系列代表性工作跨语言预训练Unsupervised Cross-lingual Representation Learning for Speech RecognitionConneau et al., 2020产出 XLSR-53 多语言模型自训练增强Self-training and Pre-training are Complementary for Speech RecognitionXu et al., 2020将 wav2vec 2.0 与自训练结合多领域数据混合Robust wav2vec 2.0: Analyzing Domain Shift in Self-Supervised Pre-TrainingHsu et al., 2021零样本跨语言音素识别Simple and Effective Zero-shot Cross-lingual Phoneme RecognitionXu et al., 2021在 XLSR-53 基础上微调以转写未见语言。从仓库源码看wav2vec 2.0 的核心实现位于 fairseq/models/wav2vec/wav2vec2.py其主类Wav2Vec2Model约 L277 处定义由卷积特征抽取器feature extractor、量化模块quantizer和 Transformer 上下文网络组成CTC 微调头wav2vec_ctc则在 fairseq/models/wav2vec/wav2vec2_asr.py 中实现。第一代 wav2vec 与离散化的 vq-wav2vec 则位于 fairseq/models/wav2vec/wav2vec.py。预训练模型总览单语 wav2vec 2.0 系列Librispeech / Libri-Light官方发布的多档模型如下表。表中“Finetuning split”指微调所用的标注数据量“*”表示 2020-10-24 更新的版本“**”表示 2021-11-13 更新的版本且其预训练数据在 Libri-Light 基础上混入了 CommonVoice、Switchboard、Fisher 三个语料。模型微调切分预训练数据集模型文件Wav2Vec 2.0 Base不微调Librispeechwav2vec_small.ptWav2Vec 2.0 Base10 分钟Librispeechwav2vec_small_10m.ptWav2Vec 2.0 Base100 小时Librispeechwav2vec_small_100h.ptWav2Vec 2.0 Base960 小时Librispeechwav2vec_small_960h.ptWav2Vec 2.0 Large不微调Librispeechlibri960_big.ptWav2Vec 2.0 Large10 分钟Librispeechwav2vec_big_10m.ptWav2Vec 2.0 Large100 小时Librispeechwav2vec_big_100h.ptWav2Vec 2.0 Large960 小时Librispeechwav2vec_big_960h.ptWav2Vec 2.0 Large (LV-60)*不微调Libri-Lightwav2vec_vox_new.ptWav2Vec 2.0 Large (LV-60)*10 分钟Libri-Light Librispeechwav2vec_vox_10m_new.ptWav2Vec 2.0 Large (LV-60)*100 小时Libri-Light Librispeechwav2vec_vox_100h_new.ptWav2Vec 2.0 Large (LV-60)*960 小时Libri-Light Librispeechwav2vec2_vox_960h_new.ptWav2Vec 2.0 Large (LV-60) 自训练*10 分钟Libri-Light Librispeechwav2vec_vox_10m_pl.ptWav2Vec 2.0 Large (LV-60) 自训练*100 小时Libri-Light Librispeechwav2vec_vox_100h_pl.ptWav2Vec 2.0 Large (LV-60) 自训练*960 小时Libri-Light Librispeechwav2vec_vox_960h_pl.ptWav2Vec 2.0 Large (LV-60 CV SWBD FSH)**不微调Libri-Light CommonVoice Switchboard Fisherw2v_large_lv_fsh_swbd_cv.ptWav2Vec 2.0 Large (LV-60 CV SWBD FSH)**960 小时 Librispeech同上w2v_large_lv_fsh_swbd_cv_ftls960_updated.ptWav2Vec 2.0 Large (LV-60 CV SWBD FSH)**300 小时 Switchboard同上w2v_large_lv_fsh_swbd_cv_ftsb300_updated.pt说明以上模型文件均托管在 fairseq 官方发布站点dl.fbaipublicfiles.com/fairseq/wav2vec/路径下下载后可配合本文后续的微调与解码命令使用。多语料混合版本** 标记中的数据集来源为 Libri-Light、CommonVoice、SwitchboardLDC97S62与 FisherLDC2004T19。多语言 XLSR-53多语言预训练模型 XLSR-53 的架构为 Large使用约 5.6 万小时、覆盖 53 种语言的音频数据来自 MLS、CommonVoice、BABEL 三个语料库模型文件为xlsr_53_56k.pt。其多语言预训练数据构成如下MLSMultilingual LibriSpeech8 种语言、约 5.07 万小时覆盖荷兰语、英语、法语、德语、意大利语、波兰语、葡萄牙语、西班牙语CommonVoice36 种语言、约 3600 小时覆盖阿拉伯语、巴斯克语、布列塔尼语、中文CN/HK/TW、楚瓦什语、迪维希语、荷兰语、英语、世界语、爱沙尼亚语、法语、德语、Hakh-Chin、印尼语、国际语、爱尔兰语、意大利语、日语、卡拜尔语、基尼亚卢旺达语、吉尔吉斯语、拉脱维亚语、蒙古语、波斯语、葡萄牙语、俄语、萨哈语、斯洛文尼亚语、西班牙语、瑞典语、泰米尔语、鞑靼语、土耳其语、威尔士语Babel17 种语言、约 1700 小时覆盖阿萨姆语、孟加拉语、粤语、宿务语、格鲁吉亚语、海地语、哈萨克语、库尔德语、老挝语、普什图语、斯瓦希里语、他加禄语、泰米尔语、Tok、土耳其语、越南语、祖鲁语。零样本跨语言音素识别模型官方还发布了在 CommonVoice6.1 版与 Babel 语言上微调的零样本音素识别模型分为 Espeak 与 Phonetisaurus 两套音素化phonemizer体系。两套模型的音素都是 IPA 符号但两个音素化工具产出的标注仍存在细微差异因此数据由哪个 phonemizer 生成就应使用对应的模型。预训练模型微调数据集语言数音素化工具模型词典LV-60CommonVoice26Espeakespeak_en_26lang_m10.ptespeak_dict.txtXLSR-53CommonVoice26Espeakespeak_26lang_m10.ptespeak_dict.txtXLSR-53CommonVoice21Phonetisaurusphonetisaurus_21lang_m10.ptphonetisaurus_dict.txtXLSR-53CommonVoice, BABEL21, 19Phonetisaurusphonetisaurus_40lang_m10.ptphonetisaurus_40lang.dict.txt用 CLI 训练新模型数据清单制作无论预训练还是微调第一步都是把 wav 文件目录变成 fairseq 能读的 manifest。官方建议把每个音频切分成1030 秒的独立片段。先安装soundfilepip install soundfile然后运行清单生成脚本$ python examples/wav2vec/wav2vec_manifest.py /path/to/waves --dest /manifest/path --ext $ext --valid-percent $valid参数说明$ext音频扩展名取flac、wav或任意soundfile可读取的格式$valid用作验证集的比例建议设为合理数值如0.01。若想使用预定义验证集如 Librispeech 的 dev-other则设为0再用手工处理的 manifest 覆盖生成的valid.tsv。从 wav2vec_manifest.py 源码可以看到其完整参数面root音频目录、--valid-percent默认0.01、--dest输出目录默认.、--ext默认flac、--seed默认42以及--path-must-contain路径必须包含的子串过滤条件。脚本会递归 glob 匹配**/*.$ext用soundfile.info().frames统计每段音频的帧数按valid_percent随机把文件写入train.tsv与valid.tsv每行格式为相对路径\t帧数首行是音频根目录绝对路径。该脚本位于 examples/wav2vec/wav2vec_manifest.py当前仓库中的实际路径为kosmos-2/fairseq/examples/wav2vec/wav2vec_manifest.py。预训练 wav2vec 2.0Base 与 LargeBase 模型对应 Librispeech 配置以下命令复现论文中在 Librispeech 上训练的 Base 模型。注意输入必须是单声道、16 kHz 采样率$ fairseq-hydra-train \ task.data/path/to/data \ --config-dir /path/to/fairseq-py/examples/wav2vec/config/pretraining \ --config-name wav2vec2_base_librispeech仓库中的配置文件为 kosmos-2/fairseq/examples/wav2vec/config/pretraining/wav2vec2_base_librispeech.yaml关键参数包括task._name: audio_pretrainingmax_sample_size: 250000min_sample_size: 32000normalize: falsedataset.max_tokens: 1400000num_workers: 6distributed_training.distributed_world_size: 64ddp_backend: legacy_ddpcriterion._name: wav2vecinfonce: trueInfoNCE 对比损失log_keys: [prob_perplexity,code_perplexity,temp]loss_weights: [0.1, 10]对比损失与多样性损失权重optimization.max_update: 400000lr: [0.0005]optimizer: adamadam_betas: (0.9,0.98)、adam_eps: 1e-06、weight_decay: 0.01lr_scheduler: polynomial_decaywarmup_updates: 32000model._name: wav2vec2quantize_targets: truefinal_dim: 256encoder_layerdrop: 0.05dropout_input: 0.1dropout_features: 0.1feature_grad_mult: 0.1encoder_embed_dim: 768。多卡模拟技巧若实际只有 k 张 GPU可在--config-dir之前追加distributed_training.distributed_world_sizek与optimization.update_freq[x]其中x 64/k从而模拟 64 卡的效果即用梯度累积弥补世界规模不足。Large 模型对应 Libri-Light 配置$ fairseq-hydra-train \ task.data/path/to/data \ --config-dir /path/to/fairseq-py/examples/wav2vec/config/pretraining \ --config-name wav2vec2_large_librivox配置文件 kosmos-2/fairseq/examples/wav2vec/config/pretraining/wav2vec2_large_librivox.yaml 与 Base 相比的关键差异task.normalize: truemax_sample_size: 320000dataset.batch_size: 4max_tokens: 1200000distributed_training.distributed_world_size: 128criterion.loss_weights: [0.1, 0]关闭多样性损失分支的显式权重配合latent_temp调度optimization.max_update: 1000000lr: [0.005]modelextractor_mode: layer_norm、layer_norm_first: true、final_dim: 768、latent_temp: [2.0,0.1,0.999995]Gumbel softmax 温度从 2.0 衰减至 0.1衰减率 0.999995、encoder_layers: 24、encoder_embed_dim: 1024、encoder_ffn_embed_dim: 4096、encoder_attention_heads: 16、feature_grad_mult: 1.0且各类 dropout 置 0。模拟 128 卡的方式同理distributed_training.distributed_world_sizek与optimization.update_freq[x]其中x 128/k。从源码结构看Wav2Vec2Modelwav2vec2.py的forward依次完成卷积特征抽取含layer_norm归一化→ 时间步掩码masking与通道掩码channel masking→ 量化Gumbel 或 k-means→ Transformer 上下文网络编码 → 输出上下文表征与对比学习所需的量化目标配合criterion.wav2vecfairseq/criterions/wav2vec_criterion.py计算 InfoNCE 损失。用 CTC 微调预训练模型微调需要平行的音频与标注文件以及 fairseq 格式的词表。字母级letter词表官方提供dict.ltr.txt可直接下载Librispeech 的标注可用 libri_labels.py 从wav2vec_manifest.py产出的 tsv 生成splittrain $ python libri_labels.py /path/to/tsv --output-dir /output/dir --output-name $split从源码看该脚本会读取 tsv 首行的根目录按{目录-目录}.trans.txt的命名规则定位 Librispeech 转写文件并同时产出.wrd词级与.ltr字母级用|分隔字母、句尾追加|两种标注文件。以 Librispeech 100h、字母目标的微调为例$ fairseq-hydra-train \ distributed_training.distributed_port$PORT \ task.data/path/to/data \ model.w2v_path/path/to/model.pt \ --config-dir /path/to/fairseq-py/examples/wav2vec/config/finetuning \ --config-name base_100h--config-name可切换到其他切分配置。仓库中 config/finetuning 目录提供了base_10m/1h/10h/100h/960h.yaml与vox_10m/1h/10h/100h/960h.yaml全套配置前者配 Base/Librispeech 模型后者配 LV-60 系列模型。以 base_100h.yaml 为例的关键参数task._name: audio_finetuninglabels: ltrnormalize: falsedataset.max_tokens: 3200000valid_subset: dev_othercriterion._name: ctczero_infinity: trueCTC 损失中忽略无限值防止 log 0 造成梯度爆炸optimization.max_update: 80000lr: [0.00003]sentence_avg: trueupdate_freq: [4]lr_scheduler: tri_stagephase_ratio: [0.1, 0.4, 0.5]final_lr_scale: 0.05model._name: wav2vec_ctcw2v_path: ???必须通过命令行指定预训练权重路径apply_mask: true、mask_prob: 0.65、mask_channel_prob: 0.5、mask_channel_length: 64、layerdrop: 0.1、activation_dropout: 0.1、feature_grad_mult: 0.0冻结卷积特征抽取器、freeze_finetune_updates: 0。对比 vox_960h.yamlLV-60 自训练模型、960h 全量微调mask_prob: 0.5、mask_channel_prob: 0.25、freeze_finetune_updates: 10000前 1 万步冻结主干仅训练头部、max_update: 320000、distributed_world_size: 24。微调 24 卡同样可用distributed_world_sizekupdate_freq[x]x 24/k模拟。语言模型联合解码训练中如需用语言模型解码需安装 flashlight 的 Python 绑定旧称 wav2letter并在命令行追加criterion.wer_args[/path/to/kenlm, /path/to/lexicon, 2, -1]。评估 CTC 模型评估带语言模型的 CTC 模型同样需要 flashlight Python 绑定。论文所用的 fairseq Transformer 语言模型可从 wav2letter 模型库2019 SOTA 配方获取下载后注意把语言模型词表转为大写。预训练模型的字母词典使用dict.ltr.txt。评估命令$subsetdev_other python examples/speech_recognition/infer.py /checkpoint/abaevski/data/speech/libri/10h/wav2vec/raw --task audio_finetuning \ --nbest 1 --path /path/to/model --gen-subset $subset --results-path /path/to/save/results/for/sclite --w2l-decoder kenlm \ --lm-model /path/to/kenlm.bin --lm-weight 2 --word-score -1 --sil-weight 0 --criterion ctc --labels ltr --max-tokens 4000000 \ --post-process letter若想得到不含语言模型的原始数字使用--w2l-decoder viterbi并省略 lexicon若使用 Transformer 语言模型将解码器切换为--w2l-decoder fairseqlm。用 Transformers 使用 wav2vec 2.0Wav2Vec2 自 Transformers 4.4 起已内置预训练权重可直接从 hub 加载。官方给出如下推理与微调示例# !pip install transformers # !pip install datasets import soundfile as sf import torch from datasets import load_dataset from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor # load pretrained model processor Wav2Vec2Processor.from_pretrained(facebook/wav2vec2-base-960h) model Wav2Vec2ForCTC.from_pretrained(facebook/wav2vec2-base-960h) librispeech_samples_ds load_dataset(patrickvonplaten/librispeech_asr_dummy, clean, splitvalidation) # load audio audio_input, sample_rate sf.read(librispeech_samples_ds[0][file]) # pad input values and return pt tensor input_values processor(audio_input, sampling_ratesample_rate, return_tensorspt).input_values # INFERENCE # retrieve logits take argmax logits model(input_values).logits predicted_ids torch.argmax(logits, dim-1) # transcribe transcription processor.decode(predicted_ids[0]) # FINE-TUNE target_transcription A MAN SAID TO THE UNIVERSE I EXIST # encode labels with processor.as_target_processor(): labels processor(target_transcription, return_tensorspt).input_ids # compute loss by passing labels loss model(input_values, labelslabels).loss loss.backward()第一代 wav2vec模型与 CLI 训练预训练模型与用法第一代 wav2vecwav2vec: Unsupervised Pre-training for Speech RecognitionSchneider et al., 2019在 Librispeech 上发布了大号模型wav2vec_large.pt。加载示例import torch import fairseq cp_path /path/to/wav2vec.pt model, cfg, task fairseq.checkpoint_utils.load_model_ensemble_and_task([cp_path]) model model[0] model.eval() wav_input_16khz torch.randn(1,10000) z model.feature_extractor(wav_input_16khz) c model.feature_aggregator(z)其中feature_extractor输出局部特征 zfeature_aggregator输出聚合后的上下文表征 c。从源码结构看这两个模块分别对应 wav2vec.py 中由--conv-feature-layers与--conv-aggregator-layers定义的卷积网络。用 CLI 训练 wav2vec数据清单制作默认扩展名 wav$ python examples/wav2vec/wav2vec_manifest.py /path/to/waves --dest /manifest/path --ext wav训练命令wav2vec 的经典超参组合$ python train.py /manifest/path --save-dir /model/path --num-workers 6 --fp16 --max-update 400000 --save-interval 1 --no-epoch-checkpoints \ --arch wav2vec --task audio_pretraining --min-lr 1e-06 --stop-min-lr 1e-09 --optimizer adam --lr 0.005 --lr-scheduler cosine \ --conv-feature-layers [(512, 10, 5), (512, 8, 4), (512, 4, 2), (512, 4, 2), (512, 4, 2), (512, 1, 1), (512, 1, 1)] \ --conv-aggregator-layers [(512, 2, 1), (512, 3, 1), (512, 4, 1), (512, 5, 1), (512, 6, 1), (512, 7, 1), (512, 8, 1), (512, 9, 1), (512, 10, 1), (512, 11, 1), (512, 12, 1), (512, 13, 1)] \ --skip-connections-agg --residual-scale 0.5 --log-compression --warmup-updates 500 --warmup-init-lr 1e-07 --criterion wav2vec --num-negatives 10 \ --max-sample-size 150000 --max-tokens 1500000 --skip-invalid-size-inputs-valid-test关键参数解读--conv-feature-layers/--conv-aggregator-layers每个元组为(通道数, 卷积核, 步长)分别刻画特征抽取网络7 层下采样逐步 5→1与聚合网络12 层步长 1扩大感受野--skip-connections-agg --residual-scale 0.5聚合网络带残差连接残差按 0.5 缩放--log-compression对输入做 log 压缩--num-negatives 10对比学习负样本数--criterion wav2vec对应 fairseq/criterions/wav2vec_criterion.py学习率采用 cosine 调度从 0.005 衰减到 1e-09预热 500 步。在 Google Cloud TPU 上预训练 wav2vec2Wav2Vec2 已支持 TPU 预训练仅限预训练不支持微调。TPU 训练有两种入口Hydra 配置方式与命令行参数方式后者存在已知问题见 fairseq issue #3741。v3-8 单机 Hydra 方式$ OMP_NUM_THREADS1 fairseq-hydra-train \ task.data/manifest/path \ --config-dir /PATH/TO/FAIRSEQ/examples/wav2vec/config/pretraining \ --config-name wav2vec2_large_librivox_tpu.yamlv3-8 单机命令行方式$ OMP_NUM_THREADS1 python train.py /manifest/path --save-dir /model/path --num-workers 6 --fp16 --max-update 400000 --save-interval 1 --no-epoch-checkpoints \ --arch wav2vec2 --task audio_pretraining --min-lr 1e-06 --stop-min-lr 1e-09 --optimizer adam --lr 0.005 --lr-scheduler cosine \ --conv-feature-layers [(512, 10, 5), (512, 8, 4), (512, 4, 2), (512, 4, 2), (512, 4, 2), (512, 1, 1), (512, 1, 1)] \ --conv-aggregator-layers [(512, 2, 1), (512, 3, 1), (512, 4, 1), (512, 5, 1), (512, 6, 1), (512, 7, 1), (512, 8, 1), (512, 9, 1), (512, 10, 1), (512, 11, 1), (512, 12, 1), (512, 13, 1)] \ --skip-connections-agg --residual-scale 0.5 --log-compression --warmup-updates 500 --warmup-init-lr 1e-07 --criterion wav2vec --num-negatives 10 \ --max-sample-size 150000 --max-tokens 1500000 --skip-invalid-size-inputs-valid-test \ --tpu --distributed-world-size 8 --num-batch-buckets 3 --enable-padding \ --encoder-layerdrop 0 --mask-channel-prob 0.1Pod slicev3-NN8Hydra 方式$ OMP_NUM_THREADS1 fairseq-hydra-train \ task.data/manifest/path \ --config-dir /PATH/TO/FAIRSEQ/examples/wav2vec/config/pretraining \ --config-name wav2vec2_large_librivox_tpu-pod.yaml # 按需修改 distributed-world-sizePod slice 命令行方式借助 torch_xla 的 xla_dist 启动$ python -m torch_xla.distributed.xla_dist \ --tpu ${TPUNAME} --conda-envtorch-xla-${TORCH_XLA_VERSION} --env OMP_NUM_THREADS1 \ -- \ python train.py /manifest/path --save-dir /model/path --num-workers 6 --fp16 --max-update 400000 --save-interval 1 --no-epoch-checkpoints \ --arch wav2vec2 --task audio_pretraining --min-lr 1e-06 --stop-min-lr 1e-09 --optimizer adam --lr 0.005 --lr-scheduler cosine \ --conv-feature-layers [(512, 10, 5), (512, 8, 4), (512, 4, 2), (512, 4, 2), (512, 4, 2), (512, 1, 1), (512, 1, 1)] \ --conv-aggregator-layers [(512, 2, 1), (512, 3, 1), (512, 4, 1), (512, 5, 1), (512, 6, 1), (512, 7, 1), (512, 8, 1), (512, 9, 1), (512, 10, 1), (512, 11, 1), (512, 12, 1), (512, 13, 1)] \ --skip-connections-agg --residual-scale 0.5 --log-compression --warmup-updates 500 --warmup-init-lr 1e-07 --criterion wav2vec --num-negatives 10 \ --max-sample-size 150000 --max-tokens 1500000 --skip-invalid-size-inputs-valid-test \ --tpu --distributed-world-size ${WORLD_SIZE} --num-batch-buckets 3 --enable-padding \ --encoder-layerdrop 0 --mask-channel-prob 0.1TPU 相关配置可参考仓库中的 wav2vec2_large_librivox_tpu.yaml 与 wav2vec2_large_librivox_tpu-pod.yaml其与 GPU 版本的主要差异在于common.tpu: true、fp16: falseTPU 使用 bfloat16 而非 fp16、task.precompute_mask_indices: true、task.enable_padding: true、task.num_batch_buckets: 3并在inferred_w2v_config中显式给出mask_prob: 0.65、mask_selection: static、mask_channel_prob: 0.1。提取下游任务数据的嵌入使用 wav2vec_featurize.py 可从预训练 checkpoint 为下游任务音频批量提取特征$ PYTHONPATH/path/to/fairseq python examples/wav2vec/wav2vec_featurize.py --input /path/to/task/waves --output /path/to/output \ --model /model/path/checkpoint_best.pt --split train valid testvq-wav2vec离散语音表征vq-wav2vecvq-wav2vec: Self-Supervised Learning of Discrete Speech RepresentationsBaevski et al., 2019在 wav2vec 基础上引入向量量化把连续表征离散化为 code也被用于Effectiveness of self-supervised pre-training for speech recognitionBaevski et al., 2019。预训练模型与用法描述数据集模型vq-wav2vec GumbelLibrispeechvq-wav2vec.ptvq-wav2vec K-meansLibrispeechvq-wav2vec_kmeans.ptRoberta on K-means codesLibrispeechbert_kmeans.tar加载示例import torch import fairseq cp torch.load(/path/to/vq-wav2vec.pt) model, cfg, task fairseq.checkpoint_utils.load_model_ensemble_and_task([cp]) model model[0] model.eval() wav_input_16khz torch.randn(1,10000) z model.feature_extractor(wav_input_16khz) _, idxs model.vector_quantizer.forward_idx(z) print(idxs.shape) # output: torch.Size([1, 60, 2]), 60 timesteps with 2 indexes corresponding to 2 groups in the model输出idxs的形状为[1, 60, 2]60 个时间步、2 个索引对应模型中 2 个量化分组groups。训练 Gumbel vq-wav2vec清单制作与前述相同训练命令$ python train.py /manifest/path --save-dir /model/path --num-workers 6 --fp16 --max-update 400000 \ --save-interval 1 --no-epoch-checkpoints --arch wav2vec --task audio_pretraining --min-lr 1e-06 --stop-min-lr 1e-09 \ --optimizer adam --lr 1e-05 --lr-scheduler cosine \ --conv-feature-layers [(512, 10, 5), (512, 8, 4), (512, 4, 2), (512, 4, 2), (512, 4, 2), (512, 1, 1), (512, 1, 1), (512, 1, 1)] \ --conv-aggregator-layers [(512, 2, 1), (512, 3, 1), (512, 4, 1), (512, 5, 1), (512, 6, 1), (512, 7, 1), (512, 8, 1), (512, 9, 1), (512, 10, 1), (512, 11, 1), (512, 12, 1), (512, 13, 1)] \ --activation gelu --offset auto --skip-connections-agg --residual-scale 0.5 \ --log-keys [prob_perplexity,code_perplexity,temp] --vq-type gumbel --vq-groups 2 --vq-depth 2 \ --combine-groups --vq-vars 320 --vq-temp (2,0.5,0.999995) --prediction-steps 12 --warmup-updates 1000 \ --warmup-init-lr 1e-07 --criterion wav2vec --num-negatives 10 --max-sample-size 150000 \ --max-tokens 300000 --cross-sample-negatives 0 --update-freq 1 --seed 2 --skip-invalid-size-inputs-valid-test关键量化参数--vq-type gumbel量化方式、--vq-groups 22 个分组、--vq-depth 2每组 2 层、--vq-vars 320每组码本大小 320、--vq-temp (2,0.5,0.999995)温度初值/终值/衰减率、--combine-groups合并分组计算对比目标、--log-keys记录困惑度与温度。k-means 模式把--vq-type改为kmeans并追加--loss-weights [1]。官方预训练模型基于 16 卡训练。音频 token 化如供 BERT 训练使用 vq-wav2vec_featurize.py 把音频转成离散 code$ PYTHONPATH/path/to/fairseq python examples/wav2vec/vq-wav2vec_featurize.py --data-dir /manifest/path --output-dir /path/to/output \ --checkpoint /model/path/checkpoint_best.pt --split train valid test --extension tsv小结与进一步阅读本文完整覆盖了 wav2vec 2.0 / wav2vec / vq-wav2vec 在 fairseq 中的一条龙使用路径从 manifest 制作wav2vec_manifest.py、预训练配置config/pretraining、CTC 微调libri_labels.py 与 config/finetuning到解码评估、Transformers 推理、特征提取与 TPU 训练。若想进一步深入可在当前仓库中继续阅读模型实现fairseq/models/wav2vec/wav2vec2.pyWav2Vec2Model、fairseq/models/wav2vec/wav2vec.py、fairseq/models/wav2vec/wav2vec2_asr.pywav2vec_ctc训练目标fairseq/criterions/wav2vec_criterion.py多语言微调examples/wav2vec/xlsr无监督语音识别wav2vec-U与 kaldi 自训练管线examples/wav2vec/unsupervised。需要注意的是预训练权重文件托管在 fairseq 官方发布站点仓库只读使用时请按官方发布地址自行下载并依据本文参数校验输入音频规格单声道、16 kHz与分布式设置。【免费下载链接】unilmLarge-scale Self-supervised Pre-training Across Tasks, Languages, and Modalities项目地址: https://gitcode.com/GitHub_Trending/un/unilm创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考