NeMo Audio 配置文件完全指南:数据集、Lhotse 加载器、模型架构与微调实战
【免费下载链接】SpeechA scalable generative AI framework built for researchers and developers working on Large Language Models, Multimodal, and Speech AI (Automatic Speech Recognition and Text-to-Speech)项目地址: https://gitcode.com/GitHub_Trending/nem/Speech
本指南以 NeMo Speech 仓库中 docs/source/audio/configs.rst 为核心,系统讲解音频(Audio)集合模型配置文件的组织方式与全部核心参数:从经典的 NeMo manifest 数据集配置,到 Lhotse CutSet/Shar 高性能数据加载,再到支持温度重加权的多数据集混合采样、模型架构声明与三种微调启动方式。读完本文,你将能够独立读懂并编写一套可运行的 NeMo Audio 训练配置,并能用命令行覆盖任意参数完成从零训练与基于预训练模型的微调。
一、配置文件整体结构
NeMo Audio 集合的配置文件采用 Hydra + OmegaConf 体系组织。每个配置文件通常由name、model、trainer、exp_manager四大部分组成,其中model段是核心,一般需要包含:
- 数据集信息:通过
model.train_ds、model.validation_ds、model.test_ds小节声明训练、验证、测试数据; - 增强参数:在线增强(如 RIR 卷积、加性噪声)的开关与数据路径;
- 模型架构规格:通过
_target_指向具体的类实现(如nemo.collections.audio.modules.transforms.AudioToSpectrogram),声明编码器、解码器、估计器/掩码网络、损失函数与评估指标。
关于 Experiment Manager 与 PyTorch Lightning trainer 等所有 NeMo 模型通用的参数,参见 核心配置文档。
所有 NeMo Audio 模型的示例配置文件均可在 examples/audio/conf 目录中找到,仓库当前包含 15 个覆盖不同任务的配置:masking.yaml、masking_with_online_augmentation.yaml、predictive.yaml、predictive_conformer.yaml、flow_matching_generative.yaml、flow_matching_generative_ssl_pretraining.yaml、score_based_generative.yaml、schroedinger_bridge.yaml、beamforming.yaml、maxine_bnr.yaml等。
训练入口脚本为 examples/audio/audio_to_audio_train.py,它通过@hydra_runner(config_path="./conf", config_name="masking")加载配置,再根据model.type字段分发到对应的模型类(mask_based→EncMaskDecAudioToAudioModel,predictive→PredictiveAudioToAudioModel,score_based→ScoreBasedGenerativeAudioToAudioModel,schroedinger_bridge→SchroedingerBridgeAudioToAudioModel,flow_matching→FlowMatchingAudioToAudioModel,bnr→BNR2),随后调用model.maybe_init_from_pretrained_checkpoint(cfg)加载预训练权重并执行trainer.fit(model)。
二、NeMo Dataset 配置(manifest 方式)
训练、验证、测试参数分别通过model.train_ds、model.validation_ds、model.test_ds小节指定。根据任务不同,可能包含加载音频的采样率或时长等参数;部分字段可以留空,在运行时通过命令行指定。数据集处理类的完整参数列表参见 API 文档的数据集章节。
一个典型的 train / validation / test 数据集配置示例如下(对应 examples/audio/conf/masking.yaml):
model: sample_rate: 16000 skip_nan_grad: false train_ds: manifest_filepath: ??? input_key: audio_filepath # key of the input signal path in the manifest target_key: target_filepath # key of the target signal path in the manifest target_channel_selector: 0 # target signal is the first channel from files in target_key audio_duration: 4.0 # in seconds, audio segment duration for training random_offset: true # if the file is longer than audio_duration, use random offset to select a subsegment min_duration: ${model.train_ds.audio_duration} batch_size: 64 # batch size may be increased based on the available memory shuffle: true num_workers: 8 pin_memory: true validation_ds: manifest_filepath: ??? input_key: audio_filepath # key of the input signal path in the manifest target_key: target_filepath # key of the target signal path in the manifest target_channel_selector: 0 # target signal is the first channel from files in target_key batch_size: 64 # batch size may be increased based on the available memory shuffle: false num_workers: 4 pin_memory: true test_ds: manifest_filepath: ??? input_key: audio_filepath # key of the input signal path in the manifest target_key: target_filepath # key of the target signal path in the manifest target_channel_selector: 0 # target signal is the first channel from files in target_key batch_size: 1 # batch size may be increased based on the available memory shuffle: false num_workers: 4 pin_memory: true关键参数说明:
| 参数 | 作用 |
|---|---|
manifest_filepath | 指向 JSON Lines 格式的 manifest 文件,???表示必填,必须通过命令行或配置提供 |
input_key | manifest 中输入信号路径对应的键,如audio_filepath(含噪/含混响信号) |
target_key | manifest 中目标信号路径对应的键,如target_filepath(干净信号) |
target_channel_selector | 当目标文件为多通道时选择第几通道作为目标(0 表示第一通道) |
audio_duration | 训练音频段时长(秒)。注意它与 STFT 参数联动:例如hop_length=256时,audio_duration=4.0对应的 STFT 时间帧数为1 + 4.0 // 0.256 = 16(若想得到 256 帧,则需audio_duration = 1 + 256 * 0.256 ≈ 65.5,实际配置中masking_with_online_augmentation.yaml使用truncate_duration: 4.0+hop_length: 256并注释"Number of STFT time frames = 1 + truncate_duration // encoder.hop_length = 256",这里的//是整除关系,配置时应以实际帧数为准) |
random_offset | 文件长于audio_duration时,用随机偏移选取子片段(训练) |
min_duration | 过滤掉短于该时长的样本,常用${model.train_ds.audio_duration}引用保持一致性 |
batch_size | 批大小,可按显存上调 |
shuffle | 是否打乱数据(训练 true、验证/测试 false) |
num_workers | DataLoader 工作进程数 |
pin_memory | 是否固定内存加速 GPU 传输 |
三、Lhotse Dataset 配置
3.1 Lhotse CutSet
启用 Lhotse 数据加载器后,训练数据以 CutSet 清单形式组织。目标信号需要存放在自定义的target_recording字段中:
train_ds: use_lhotse: true # enable Lhotse data loader cuts_path: ??? # path to Lhotse cuts manifest with input signals and the corresponding target signals (target signals should be in the custom "target_recording" field) truncate_duration: 4.00 # truncate audio to 4 seconds truncate_offset_type: random # if the file is longer than truncate_duration, use random offset to select a subsegment batch_size: 64 # batch size may be increased based on the available memory shuffle: true num_workers: 8 pin_memory: true与 manifest 方式相比,Lhotse 使用truncate_duration+truncate_offset_type组合替代audio_duration+random_offset:truncate_duration将音频截断到固定时长,truncate_offset_type: random在文件过长时随机选取子片段。
3.2 Lhotse CutSet + 在线增强
在线增强通过 RIR(房间冲激响应)卷积与加性噪声实现,需要在训练集配置中额外开启rir_enabled并提供 RIR 与噪声数据的 Lhotse 清单路径。完整示例见 examples/audio/conf/masking_with_online_augmentation.yaml:
train_ds: use_lhotse: true # enable Lhotse data loader cuts_path: ??? # path to Lhotse cuts manifest with speech signals for augmentation (including custom "target_recording" field with the same signals) truncate_duration: 4.00 # truncate audio to 4 seconds truncate_offset_type: random # if the file is longer than truncate_duration, use random offset to select a subsegment batch_size: 64 # batch size may be increased based on the available memory shuffle: true num_workers: 8 pin_memory: true rir_enabled: true # enable room impulse response augmentation rir_path: ??? # path to Lhotse recordings manifest with room impulse response signals noise_path: ??? # path to Lhotse cuts manifest with noise signals注意:在线增强模式下,cuts_path提供的是用于增强的干净语音(其target_recording字段与自身相同,作为干净目标),而validation_ds/test_ds的cuts_path则应直接提供含噪语音与干净目标的配对数据。增强相关的在线增强教程可参考 Speech_Enhancement_with_Online_Augmentation.ipynb。
3.3 Lhotse Shar 格式
Shar 是 Lhotse 的高性能打包存储格式,适合海量数据与分布式训练。其配置只需要shar_path指向 shar 目录,无需cuts_path:
train_ds: shar_path: ??? use_lhotse: true truncate_duration: 4.00 # truncate audio to 4 seconds truncate_offset_type: random batch_size: 8 # batch size may be increased based on the available memory shuffle: true num_workers: 8 pin_memory: true一个完整的 Shar 配置示例见 examples/audio/conf/flow_matching_generative_ssl_pretraining.yaml,该文件同时演示了 Shar 训练集与 manifest 验证集的混合用法。由于 Shar 数据量通常极大,其trainer段需要显式设置max_steps(如 10000)与limit_train_batches(如 1000)来控制每个伪 epoch 的训练量,并设置use_distributed_sampler: false(Lhotse 数据加载所必需)。
3.4 多数据集温度重加权(Dataset Reweighting with Temperature)
当通过嵌套的input_cfg组合多个数据集时,可以用reweight_temperature控制采样分布。该特性允许你在增删数据集时无需手工重算权重即可平衡各数据集的采样比例。
温度缩放公式为:
$$\hat{w}i = \frac{w_i^{\tau}}{\sum{j} w_j^{\tau}}$$
其中 $w_i$ 是数据集 $i$ 的原始权重,$\tau$ 是温度,$\hat{w}_i$ 是归一化后的采样概率。
温度的作用机制:
temperature = 1.0:保持原始权重比例(中性,不重加权);temperature = 0.0:所有数据集等概率采样,与原始权重无关;0 < temperature < 1.0:相对大数据集更多采样小数据集;temperature > 1.0:放大数据集权重之间的差异。
两种配置格式:
- 标量值(应用于所有嵌套层级,会打印警告日志):
train_ds: use_lhotse: true reweight_temperature: 0.5 # Applied to all levels, warning logged input_cfg: - type: group input_cfg: - type: lhotse_shar shar_path: /path/to/dataset1 weight: 900 - type: lhotse_shar shar_path: /path/to/dataset2 weight: 100 - type: lhotse_shar shar_path: /path/to/dataset3 weight: 200 - type: nemo_tarred manifest_filepath: /path/to/dataset4/manifest.json tarred_audio_filepath: /path/to/dataset4/audio.tar weight: 300- 列表格式(与最大嵌套深度一一对应,每层一个温度):
train_ds: use_lhotse: true reweight_temperature: [1.0, 0.0] # Level 1: preserve ratios, Level 2: equalize input_cfg: - type: group weight: 0.7 input_cfg: - type: lhotse_shar shar_path: /path/to/dataset1 weight: 600 - type: lhotse_shar shar_path: /path/to/dataset2 weight: 400 - type: group weight: 0.3 input_cfg: - type: lhotse_shar shar_path: /path/to/dataset3 weight: 100注意:若
reweight_temperature以列表形式提供,其长度必须与input_cfg的最大嵌套深度完全一致,过多或过少都会抛出ValueError。若希望所有层级使用相同温度,请使用标量值。
最大嵌套深度计算:
最大嵌套深度即配置中input_cfg键的最大嵌套层数,同一层的兄弟 group 共享同一个温度值。例如:
# This has maximum nesting depth = 2 input_cfg: # Level 1 - type: group input_cfg: # Level 2 - type: lhotse_shar - type: group # Same level as above (sibling) input_cfg: # Level 2 (same as above) - type: lhotse_shar当input_cfg通过 CLI 覆盖为 YAML 文件路径时(如model.train_ds.input_cfg=train_all.yaml),深度计算会加载被引用文件并遍历其内容统计嵌套的input_cfg键,支持多级文件引用:
# train_all.yaml (referenced via input_cfg=train_all.yaml) - type: group weight: 100 input_cfg: ${oc.env:MANIFEST_ROOT}/train_en.yaml # resolved at runtime - type: group weight: 200 input_cfg: ${oc.env:MANIFEST_ROOT}/train_de.yaml注意:包含 OmegaConf 插值的路径(如
${oc.env:MANIFEST_ROOT})在深度统计阶段无法解析——它们会在运行时由OmegaConf.create()才被解析,此类路径被当作单个额外嵌套层级处理。
实战示例:平衡多任务数据组
train_ds: use_lhotse: true reweight_temperature: [1.0, 0.0] # Level 1: Preserve task ratios, Level 2: Equalize within tasks input_cfg: - type: group weight: 0.7 tags: task: asr input_cfg: - type: nemo_tarred manifest_filepath: /path/to/asr1/manifest.json tarred_audio_filepath: /path/to/asr1/audio.tar weight: 600 # Large dataset - type: nemo_tarred manifest_filepath: /path/to/asr2/manifest.json tarred_audio_filepath: /path/to/asr2/audio.tar weight: 100 # Small dataset (will be upsampled with temp=0.0) - type: group weight: 0.3 tags: task: ast input_cfg: - type: nemo_tarred manifest_filepath: /path/to/ast1/manifest.json tarred_audio_filepath: /path/to/ast1/audio.tar weight: 50 - type: nemo_tarred manifest_filepath: /path/to/ast2/manifest.json tarred_audio_filepath: /path/to/ast2/audio.tar weight: 200该示例的效果:
- Level 1 温度为
1.0:ASR 组与 AST 组之间保持 70/30 的原始比例; - Level 2 温度为
0.0:每组内部所有数据集等概率采样,无论其原始权重如何(小数据集被上采样)。
从源码实现看,reweight_temperature参数定义于 nemo/collections/common/data/lhotse/dataloader.py(reweight_temperature: Any = None,支持 float / int / list 混合格式),温度重加权逻辑位于 nemo/collections/common/data/lhotse/cutset.py,对应的深度计算与行为验证测试见 tests/collections/common/test_lhotse_temperature_reweighting.py 与 tests/collections/common/test_lhotse_dataloading.py。input_cfg还支持type: nemo_tarred(NeMo tarred 音频数据集)与type: lhotse_shar两种底层数据集类型,tags字段可用于给数据集打标签。
四、模型架构配置
每个配置文件都应声明实验所用的模型架构。下面是一个简单预测式(predictive)模型的完整示例(对应 examples/audio/conf/predictive.yaml):
model: type: predictive sample_rate: 16000 skip_nan_grad: false num_outputs: 1 normalize_input: true # normalize the input signal to 0dBFS train_ds: manifest_filepath: ??? input_key: noisy_filepath target_key: clean_filepath audio_duration: 2.00 # trim audio to 2 seconds random_offset: true normalization_signal: input_signal batch_size: 8 # batch size may be increased based on the available memory shuffle: true num_workers: 8 pin_memory: true validation_ds: manifest_filepath: ??? input_key: noisy_filepath target_key: clean_filepath batch_size: 8 shuffle: false num_workers: 4 pin_memory: true encoder: _target_: nemo.collections.audio.modules.transforms.AudioToSpectrogram fft_length: 510 # Number of subbands in the STFT = fft_length // 2 + 1 = 256 hop_length: 128 magnitude_power: 0.5 scale: 0.33 decoder: _target_: nemo.collections.audio.modules.transforms.SpectrogramToAudio fft_length: ${model.encoder.fft_length} hop_length: ${model.encoder.hop_length} magnitude_power: ${model.encoder.magnitude_power} scale: ${model.encoder.scale} estimator: _target_: nemo.collections.audio.parts.submodules.ncsnpp.SpectrogramNoiseConditionalScoreNetworkPlusPlus in_channels: 1 # single-channel noisy input out_channels: 1 # single-channel estimate num_res_blocks: 3 # increased number of res blocks pad_time_to: 64 # pad to 64 frames for the time dimension pad_dimension_to: 0 # no padding in the frequency dimension loss: _target_: nemo.collections.audio.losses.MSELoss # computed in the time domain metrics: val: sisdr: # output SI-SDR _target_: torchmetrics.audio.ScaleInvariantSignalDistortionRatio optim: name: adam lr: 1e-4 # optimizer arguments betas: [0.9, 0.999] weight_decay: 0.0架构配置的要点:
type字段:predictive对应PredictiveAudioToAudioModel,训练脚本据此选择模型类;不写该字段时默认使用mask_based(EncMaskDecAudioToAudioModel)并打印警告。encoder/decoder:通过_target_指向nemo.collections.audio.modules.transforms中的 STFT 变换类。注意fft_length=510时子带数为510 // 2 + 1 = 256;decoder通过${model.encoder.fft_length}等 OmegaConf 插值自动与encoder保持一致,避免参数漂移。estimator:预测式模型在此使用 NCSN++(Noise Conditional Score Network Plus Plus)网络,in_channels/out_channels对应输入输出通道数,num_res_blocks控制残差块数量,pad_time_to/pad_dimension_to控制时间/频率维度的 padding。loss:nemo.collections.audio.losses.MSELoss,在时域计算(即预测目标为时域波形)。metrics:torchmetrics.audio.ScaleInvariantSignalDistortionRatio计算 SI-SDR 作为验证指标。optim:支持adam/adamw等优化器及lr、betas、weight_decay参数;生成式配置(如 flow matching)还可追加sched调度器小节(CosineAnnealing+warmup_steps、min_lr等)。
不同模型家族的架构段差异较大,可通过对比仓库示例快速掌握:
- 掩码式(mask-based):
mask_estimator(RNN)+mask_processor(参考通道掩码),损失为SDRLoss(支持scale_invariant),见 masking.yaml; - 生成式(flow matching):
estimator(Transformer UNet)+flow(最优传输流)+sampler(Euler 条件流匹配采样器,num_steps: 20)+ssl_pretrain_masking(掩码补丁自监督预训练,mask_fraction: 0.7),指标常用 SI-SDR / ESTOI / PESQ 组合,见 flow_matching_generative_ssl_pretraining.yaml。
五、微调配置(Finetuning)
所有训练脚本均支持通过部分/完全加载预训练 checkpoint 权重到当前实例化的模型来实现便捷微调。前提是当前实例化模型的参数结构与预训练 checkpoint 匹配,否则权重无法正确加载。
预训练权重可通过两种方式提供:
- 提供 NeMo 模型文件路径(
init_from_nemo_model); - 提供预训练 NeMo 模型名称(
init_from_pretrained_model,将从云端自动下载)。
5.1 从零训练
python examples/audio/audio_to_audio_train.py \ --config-path=<path to dir of configs> --config-name=<name of config without .yaml>) \ model.train_ds.manifest_filepath="<path to manifest file>" \ model.validation_ds.manifest_filepath="<path to manifest file>" \ trainer.devices=1 \ trainer.accelerator='gpu' \ trainer.max_epochs=505.2 基于 NeMo 模型文件微调
在命令行中追加+init_from_nemo_model参数(Hydra 的+前缀表示新增配置键)指向本地.nemo文件:
python examples/audio/audio_to_audio_train.py \ --config-path=<path to dir of configs> --config-name=<name of config without .yaml>) \ model.train_ds.manifest_filepath="<path to manifest file>" \ model.validation_ds.manifest_filepath="<path to manifest file>" \ trainer.devices=1 \ trainer.accelerator='gpu' \ trainer.max_epochs=50 \ +init_from_nemo_model="<path to .nemo model file>"5.3 基于预训练模型名称微调
python examples/audio/audio_to_audio_train.py \ --config-path=<path to dir of configs> --config-name=<name of config without .yaml>) \ model.train_ds.manifest_filepath="<path to manifest file>" \ model.validation_ds.manifest_filepath="<path to manifest file>" \ trainer.devices=1 \ trainer.accelerator='gpu' \ trainer.max_epochs=50 \ +init_from_pretrained_model="<name of pretrained checkpoint>"从训练脚本源码看,model.maybe_init_from_pretrained_checkpoint(cfg)会在模型实例化后统一处理这两种预训练权重注入方式,因此三条训练命令共用同一套参数覆盖机制。微调类配置可直接参考 examples/audio/conf/flow_matching_generative_finetuning.yaml。
六、trainer 与 exp_manager 常用配置速查
虽然trainer/exp_manager属于所有 NeMo 模型通用的配置(详见 核心配置文档),但 Audio 示例配置中有一批与音频训练强相关的惯用设置,值得在编写配置文件时留意:
trainer.devices: -1:使用全部可用 GPU;strategy: ddp开启分布式数据并行;trainer.precision: 32:O1/O2 自动混合精度场景下应设为 16;trainer.enable_checkpointing: false与trainer.logger: false:checkpoint 与日志交给exp_manager统一管理;exp_manager.checkpoint_callback_params:用monitor/mode控制保存依据(如掩码式用val_lossmin,预测式用val_sisdrmax),always_save_nemo: true表示直接保存.nemo文件而非 PTL checkpoint;exp_manager.ema:启用指数移动平均(如decay: 0.999);exp_manager.create_early_stopping_callback:基于验证指标早停(注意strict: false避免恢复训练时的监控指标报错);exp_manager.resume_from_checkpoint/resume_if_exists:断点续训。
综上,NeMo Audio 配置体系以 Hydra 覆盖机制为骨架,以train_ds/validation_ds/test_ds为数据入口,以encoder/decoder/estimator(或mask_estimator)为架构声明,配合 Lhotse 高性能加载与温度重加权实现大规模多数据集训练。实际动手时,建议先复制 examples/audio/conf 中与任务最接近的 YAML,再用命令行覆盖 manifest 路径与训练参数即可快速跑通。
【免费下载链接】SpeechA scalable generative AI framework built for researchers and developers working on Large Language Models, Multimodal, and Speech AI (Automatic Speech Recognition and Text-to-Speech)项目地址: https://gitcode.com/GitHub_Trending/nem/Speech
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考