OpenAI-whisper语音识别模型

[复制链接]
查看637 | 回复0 | 2023-8-11 20:41:50 来自手机 | 显示全部楼层 |阅读模式
1、whisper简介



  • Whisper是一个通用的语音识别模型。它是在不同音频的大型数据集上训练的,也是一个多任务模型,可以执行多语言语音识别、语音翻译和语言识别。
  • whisper有五种模型尺寸,提供速度和准确性的平衡,其中English-only模型提供了四种选择。下面是可用模型的名称、大致内存需求和相对速度。



  • github链接:https://github.com/openai/whisper
2、方法

一种 Transformer 序列到序列模型被训练用于各种语音处理任务,包括多语种语音识别、语音翻译、口语语言识别以及语音活动检测。这些任务共同以一个需要被解码器预测的符号序列的形式进行表示,从而使得单个模型可以替代传统语音处理管道中的多个阶段。多任务训练格式使用一系列特殊的符号作为任务指示符或分类目标。

3、环境配置

  1. conda create -n whisper python=3.9
  2. conda activate whisper
  3. pip install -U openai-whisper
  4. sudo apt update && sudo apt install ffmpeg
  5. pip install setuptools-rust
复制代码
4、python测试脚本



  • 以轻量级tiny模型为例,测试脚本如下:
  1. import whisper
  2. model = whisper.load_model("tiny")
  3. result = model.transcribe("sample_1.wav")
  4. print(result["text"])
复制代码
测试结果如下:



  • 如果要测试large模型,需要16GB以上的显卡才行。
注:以上测试脚本暂不支持多gpu,这是因为有可能在一个GPU上加载编码器,在另一个GPU上加载解码器。
如果想通过多gpu测试,可尝试以下方法:


  • 首先更新包,以便它有最新的提交
  1. pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git
复制代码


  • 然后,参考以下脚本:
  1. import whisper
  2. model = whisper.load_model("large", device="cpu")
  3. model.encoder.to("cuda:0")
  4. model.decoder.to("cuda:1")
  5. model.decoder.register_forward_pre_hook(lambda _, inputs: tuple([inputs[0].to("cuda:1"), inputs[1].to("cuda:1")] + list(inputs[2:])))
  6. model.decoder.register_forward_hook(lambda _, inputs, outputs: outputs.to("cuda:0"))
  7. model.transcribe("jfk.flac")
复制代码
多gpu脚本参考连接:https://github.com/openai/whisper/discussions/360


  • 测试large模型(显存>=16GB),输入音频,输出文本(中文简体),需要设置initial_prompt,不然输出的可能是中文繁体
  1. import whisper
  2. import os
  3. model = whisper.load_model("large")
  4. prompt='以下是普通话的句子'
  5. result = model.transcribe(file_path, language='zh',verbose=True, initial_prompt=prompt)
  6. print(result["text"])
复制代码
来源:https://blog.csdn.net/wjinjie/article/details/130762112
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则