FunASR 微调 Whisper 全流程指南:数据准备、参数配置与源码级原理解析
FunASR 微调 Whisper 全流程指南数据准备、参数配置与源码级原理解析【免费下载链接】FunASROpen-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.项目地址: https://gitcode.com/GitHub_Trending/fun/FunASR本文以 FunASR 开源仓库中 examples/industrial_data_pretraining/whisper/README.md 为骨架系统讲解如何在 FunASR 训练框架下对 OpenAI Whisper 系列模型tiny 到 large-v3-turbo进行领域数据微调与推理部署。你将掌握 JSONL 数据集的构造规范、funasr.bin.train训练命令的完整参数语义、关键超参数学习率、batch size、warmup、冻结编码器的调优策略以及微调后通过 AutoModel 一键加载与多路径推理的实战方法同时深入理解 FunASR 中WhisperWarp模型封装funasr/models/whisper/model.py的训练与推理底层实现。一、为什么在 FunASR 中微调 WhisperOpenAI Whisper 是业界广泛使用的多语言语音识别基础模型但其通用能力在垂直领域如金融、医疗、工业术语、方言口音往往表现不佳。FunASR 将 Whisper 无缝纳入其统一的训练与推理框架同一套AutoModel接口、同一套 JSONL 数据管线、同一套funasr.bin.train训练入口即可完成从数据准备到模型微调再到推理部署的完整闭环。从源码看FunASR 通过 funasr/models/whisper/model.py 中的WhisperWarp类对 Whisper 做了包装并注册了从Whisper-tiny.en到Whisper-large-v3-turbo的全系列模型名见 funasr/register.py 对应的注册表。这意味着训练脚本中只需通过modelWhisper-large-v3字符串即可指定模型训练框架会自动解析并实例化对应结构。二、支持微调的模型列表根据 examples/industrial_data_pretraining/whisper/README.md 及finetune.sh脚本中的说明FunASR 支持以下 Whisper 变体模型名称说明whisper-tiny / whisper-tiny.en最小规模适合快速验证流程whisper-base / whisper-base.en轻量级基线whisper-small / whisper-small.en速度与精度均衡whisper-medium / whisper-medium.en中等规模whisper-large-v1 / whisper-large-v2 / whisper-large-v3大规模高精度whisper-large-v3-turbov3 的加速蒸馏版本推理更快对应到 FunASR 模型名在model中使用时需遵循注册名如Whisper-large-v3全部由 funasr/models/whisper/model.py 中的tables.register(model_classes, ...)装饰器注册。.en后缀的模型为纯英文模型若目标领域主要为中文应优先选择多语言版本无.en后缀。三、数据准备JSONL 格式规范Whisper 微调使用 JSONLJSON Lines格式的数据集每行一个 JSON 对象包含三个字段key样本唯一标识字符串source音频文件路径本地绝对/相对路径或 URLtarget对应的转写文本。{key: utt001, source: /path/to/audio1.wav, target: the transcription text} {key: utt002, source: /path/to/audio2.wav, target: another transcription}仓库中已提供了可直接参考的真实样例数据 data/list/train.jsonl其完整字段形式如下{key: BAC009S0764W0121, source: https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/BAC009S0764W0121.wav, source_len: 90, target: 甚至出现交易几乎停滞的情况, target_len: 13}可以看到除key/source/target外还可附带source_len、target_len等元信息字段非必需训练框架按需读取。验证集val.jsonl与训练集格式完全一致分别通过train_data_set_list与valid_data_set_list指定。四、发起微调训练4.1 一键脚本仓库提供了开箱即用的训练脚本 finetune.sh直接执行bash finetune.sh脚本内容如下完整继承含全部参数#!/bin/bash # Whisper Fine-tuning with FunASR # Data format: JSONL with audio and text fields # {key: utt1, source: /path/to/audio.wav, target: transcription text} export CUDA_VISIBLE_DEVICES0,1 model_nameWhisper-large-v3 train_datadata/train.jsonl val_datadata/val.jsonl output_direxp/whisper_finetune python -m funasr.bin.train \ model${model_name} \ model_conf.hubopenai \ train_data_set_list${train_data} \ valid_data_set_list${val_data} \ dataset_conf.batch_size4 \ dataset_conf.num_workers4 \ train_conf.output_dir${output_dir} \ train_conf.max_epoch10 \ train_conf.lr1e-5 \ train_conf.warmup_steps500 \ optimadam \ optim_conf.lr1e-5 \ schedulerwarmuplr \ scheduler_conf.warmup_steps5004.2 命令行参数逐项解读训练统一通过python -m funasr.bin.train启动使用前缀的 Hydra 覆盖语法传参。各参数含义如下参数取值示例作用modelWhisper-large-v3指定模型注册名决定模型结构model_conf.hubopenai权重来源openai表示直接调用whisper.load_model从 OpenAI 加载官方权重需pip install openai-whisperfunasr/modelscope则从模型仓库加载train_data_set_listdata/train.jsonl训练集 JSONL 路径valid_data_set_listdata/val.jsonl验证集 JSONL 路径dataset_conf.batch_size4每张 GPU 的 batch size需根据显存调整dataset_conf.num_workers4DataLoader 数据加载进程数train_conf.output_direxp/whisper_finetune模型 checkpoint 与日志输出目录train_conf.max_epoch10最大训练轮数train_conf.lr1e-5学习率大模型建议更小train_conf.warmup_steps500学习率 warmup 步数optimadam优化器类型optim_conf.lr1e-5优化器侧学习率与train_conf.lr保持一致schedulerwarmuplr学习率调度器类型scheduler_conf.warmup_steps500调度器 warmup 步数4.3 训练底层原理交叉熵损失与教师强制funasr/models/whisper/model.py 中的forward()实现了微调的核心逻辑其数据流为编码器前向audio_features self.model.encoder(speech)输入为(B, T, D)的 mel 频谱特征教师强制Teacher Forcing训练时文本序列格式为[SOT, lang, task, ..., tokens, EOT]代码通过text[:, :-1]作为解码器输入、text[:, 1:]作为目标逐 token 右移一位避免模型在训练阶段看到未来 token解码器前向logits self.model.decoder(decoder_input, audio_features)交叉熵损失F.cross_entropy(logits, decoder_target, ignore_index-100)pad 位置通过ignore_index-100屏蔽。forward()返回{loss: loss, stats: {...}}字典其中stats记录了当前 loss 与 batch size供训练框架打印与监控。这也解释了 README 中Training uses the forward() method which computes cross-entropy loss on (mel-spectrogram, token_ids) pairs的说明。4.4 模型加载机制hub 参数的分流逻辑WhisperWarp.__init__funasr/models/whisper/model.py根据hub参数走两条加载路径hub openai将Whisper-large-v3等模型名去掉Whisper-前缀后直接调用whisper.load_model(large-v3)加载 OpenAI 官方预训练权重其他 hub默认funasr通过whisper.model.ModelDimensions与whisper.model.Whisper(dimsdims)从模型仓库如 ModelScope的配置与权重重建模型。因此微调时model_conf.hubopenai意味着从 OpenAI 官方权重开始微调若需基于已有 FunASR/ModelScope 格式权重可切换 hub 并配合本地模型目录。五、关键超参数与实战调优 Tips原文档给出的核心参数表examples/industrial_data_pretraining/whisper/README.md参数默认值说明modelWhisper-large-v3模型规模lr1e-5学习率模型越大取值越小max_epoch10训练轮数batch_size4每张 GPU 的 batch sizewarmup_steps500学习率预热步数结合 README 的 Tips 与训练框架机制给出以下实战建议中文场景首选whisper-large-v3它是多语言模型中中文支持最好的版本作为领域微调基座能保留最强的多语言泛化能力。冻结编码器加速训练在训练命令末尾追加train_conf.freeze_parammodel.encoder可冻结 Whisper 编码器仅训练解码器部分显著减少显存占用与训练时间。对于数据量有限或只需适配领域文本分布的场景这是性价比很高的策略。使用更小的学习率1e-5 ~ 5e-6Whisper 预训练权重非常成熟过大的学习率会导致灾难性遗忘catastrophic forgetting。finetune.sh中lr1e-5是通用起点大规模模型建议降至 5e-6。数据量建议README 建议目标领域音频100 小时以上才能获得有意义的提升数据不足时可结合冻结编码器 低学习率避免过拟合。显存不足时的调整dataset_conf.batch_size4为每 GPU 的 batch size出现 OOM 时可降至 1~2并同步降低lr或延长max_epoch以保持等效训练量。六、微调后的推理与部署6.1 加载微调 checkpoint 推理微调完成后checkpoint 保存在output_dir默认exp/whisper_finetune。通过AutoModel直接指定本地模型路径即可加载from funasr import AutoModel # Load fine-tuned model model AutoModel(model/path/to/exp/whisper_finetune) result model.generate(inputtest.wav) print(result[0][text])推理返回结果列表每个元素形如{key: ..., text: ...}与 funasr/models/whisper/model.py 中inference()返回结构一致。6.2 从 OpenAI 权重直接推理未微调场景仓库提供 demo_from_openai.py演示如何从 OpenAI 官方权重加载 Whisper-large-v3-turbo 并配合 FunASR 的 VAD 模型做长音频切分推理from funasr import AutoModel model AutoModel( modelWhisper-large-v3-turbo, vad_modeliic/speech_fsmn_vad_zh-cn-16k-common-pytorch, vad_kwargs{max_single_segment_time: 30000}, hubopenai, ) DecodingOptions { task: transcribe, language: None, beam_size: None, fp16: True, without_timestamps: False, prompt: None, } res model.generate( DecodingOptionsDecodingOptions, batch_size_s0, inputhttps://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav, ) print(res)要点说明hubopenai对应WhisperWarp的 OpenAI 加载路径需pip3 install -U openai-whispervad_model指定 FunASR 的流式 VAD 模型max_single_segment_time30000将超过 30 秒的音频自动切段规避 Whisper 长音频性能衰减DecodingOptions直接透传给 OpenAI Whisper 的whisper.DecodingOptions对应源码 funasr/models/whisper/model.py 中的whisper.DecodingOptions(**kwargs.get(DecodingOptions, {}))支持tasktranscribe/translate、language、beam_size、fp16、without_timestamps、prompt提示词热词等完整解码控制。6.3 命令行推理三种方式仓库提供三种命令行推理脚本均通过python -m funasr.bin.inference启动方式一从模型仓库推理infer.shinputhttps://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav output_dir./outputs/debug modeliic/speech_whisper-large_asr_multilingual devicecuda:0 # cuda:0 for gpu0, cuda:1 for gpu1, cpu python -m funasr.bin.inference \ model${model} \ input${input} \ output_dir${output_dir} \ device${device}方式二从本地模型推理infer_from_local.sh——先从 ModelScope 克隆模型到本地再用--config-path/--config-name指向本地目录local_path_root${workspace}/modelscope_models mkdir -p ${local_path_root} local_path${local_path_root}/Whisper-large-v3 git clone https://www.modelscope.cn/iic/Whisper-large-v3.git ${local_path} init_param${local_path}/large-v3.pt configconfig.yaml python -m funasr.bin.inference \ --config-path ${local_path} \ --config-name ${config} \ init_param${init_param} \ input${input} \ output_dir${output_dir} \ device${device}方式三从 OpenAI hub 推理infer_from_openai.shmodelWhisper-large-v2 # 也支持 Whisper-small / Whisper-medium / Whisper-large-v3 hubopenai devicecuda:0 python -m funasr.bin.inference \ model${model} \ hub${hub} \ input${input} \ output_dir${output_dir} \ device${device}其中方式二与微调后的本地 checkpoint 推理路径一致--config-path指向模型目录、init_param指定权重文件微调后即output_dir下的模型文件适合离线部署环境input支持本地音频路径、URL 等输入形式详见 infer.sh 注释。七、总结从微调到落地的完整链路本文基于 examples/industrial_data_pretraining/whisper/ 目录完整覆盖了 FunASR 微调 Whisper 的五个环节模型选型根据语言中/英与算力选择 tiny 至 large-v3-turbo 系列通过model注册名指定数据构造按{key, source, target}的 JSONL 规范准备训练集与验证集参考 data/list/train.jsonl训练启动bash finetune.sh或自定义python -m funasr.bin.train核心参数包括model_conf.hub、dataset_conf.batch_size、train_conf.lr/max_epoch/warmup_steps调优策略中文用 large-v3、小学习率 1e-5~5e-6、freeze_param冻结编码器加速、100 小时领域数据保障效果推理部署AutoModel 加载本地 checkpoint 一键推理或通过python -m funasr.bin.inference支持模型仓库、本地目录、OpenAI 权重三种来源。源码层面funasr/models/whisper/model.py 的WhisperWarp封装清晰展示了训练教师强制 交叉熵与推理whisper.decodeDecodingOptions透传的完整实现微调者既可以把它当作黑盒使用也可以在需要定制损失函数或解码策略时以此为切入点深入改造。【免费下载链接】FunASROpen-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.项目地址: https://gitcode.com/GitHub_Trending/fun/FunASR创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考