cd /root/autodl-tmp/Whisper-Finetune
发布时间:2025-06-24 18:37:17 作者:北方职教升学中心 阅读量:388
4.合并。
#安装环境。
--output_dir 是合并后模型的保存目录。
git clone https://github.com/yeyupiaoling/Whisper-Finetune.git。tiny-finetune。
生成json文件:jsonlines的数据列表,也就是说,
import osimport waveimport contextlibdef get_wav_duration(file_path): with contextlib.closing(wave.open(file_path, 'r')) as f: frames = f.getnframes() rate = f.getframerate() duration = frames / float(rate) return durationdef print_wav_durations(folder_path): for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith('.wav'): file_path = os.path.join(root, file) duration = get_wav_duration(file_path) print(f"File: {file} - Duration: {duration:.2f} seconds")# 指定包含 .wav 文件夹路径folder_path = '/root/autodl-tmp/data'print_wav_durations(folder_path)。
cd /root/autodl-tmp/Whisper-Finetune。
--lora_model 训练结束后保存 Lora 模型路径是检查点文件夹路径。
python merge_lora.py --lora_model=/root/autodl-tmp/Whisper-Finetune/output/whisper-tiny/checkpoint-3/ --output_dir=models/。
生成json文件:jsonlines的数据列表,也就是说,
1.为环境做准备。必须确保不是以前的检查点 if os.path.exists(state.best_model_checkpoint): if os.path.exists(best_checkpoint_folder): shutil.rmtree(best_checkpoint_folder) shutil.copytree(state.best_model_checkpoint, best_checkpoint_folder) print(f"最好的检查点是:{state.best_model_checkpoint},评估结果为:{state.best_metric}") return control。
5.验证。每一行都是JSON数据。conda create -n whisper python=3.10 -y 。
计算所有wav时长。
source activate whisper。
#安装依赖。
导入所有wav文件。
2.加载数据集。
python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple。
Whisper-Finetune/utils/reader.py修改_load_data_list 函数。
#下载基本模型。
微调。
#修改读取json的地方。
#下载基本模型。
#下载github微调文件。每一行都是JSON数据。
# 从数据列表中获取音频数据、
CUDA_VISIBLE_DEVICES=0 python finetune.py --train_data /root/autodl-tmp/data/data_list_lines.json --test_data /root/autodl-tmp/data/data_list_lines.json --base_model=/root/autodl-tmp/whisper-tiny --output_dir=output/。
cd Whisper-Finetune 。
修改Whisper-Finetune/utils/callback.SavePeftModelCallback函数py。
class SavePeftModelCallback(TrainerCallback): def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs): if args.local_rank == 0 or args.local_rank == -1: # 保存效果最好的模型 best_checkpoint_folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-best") # 确保 state.best_model_checkpoint 不是 NoneType if state.best_model_checkpoint is not None: # 只保存最新的5个检查点,因此,
3.微调模型。
#创建环境。采样率和文本 def _get_list_data(self, idx): if self.data_list_path.endswith(".header"): data_list = self.dataset_reader.get_data(self.data_list[idx]) else: data_list = self.data_list[idx] # 分割音频路径和标签 audio_file = data_list["audio"]['path'] transcript = data_list["sentences"] if self.timestamps else data_list["sentence"] language = data_list["language"] if 'language' in data_list.keys() else None if 'start_time' not in data_list["audio"].keys(): sample, sample_rate = soundfile.read(audio_file, dtype='float32') else: start_time, end_time = data_list["audio"]["start_time"], data_list["audio"]["end_time"] # 分割读取音频 sample, sample_rate = self.slice_from_file(audio_file, start=start_time, end=end_time) sample = sample.T # 转成单通道 if self.mono: sample = librosa.to_mono(sample) # 数据增强 if self.augment_configs: sample, sample_rate = self.augment(sample, sample_rate) # 重采样 if self.sample_rate != sample_rate: sample = self.resample(sample, orig_sr=sample_rate, target_sr=self.sample_rate) return sample, sample_rate, transcript, language。= sample_rate: sample = self.resample(sample, orig_sr=sample_rate, target_sr=self.sample_rate) return sample, sample_rate, transcript, language。
conda install pytorch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 pytorch-cuda=11.8 -c pytorch -c nvidia 。
git clone https://gitee.com/hf-models/whisper-tiny.git。