# whisper

* Radford A, Kim J W, Xu T, et al. Robust speech recognition via large-scale weak supervision\[C]//International Conference on Machine Learning. PMLR, 2023: 28492-28518.
* <https://github.com/openai/whisper>
* 作者来自：OpenAI
* <mark style="color:red;">wav2vec 2.0 等无监督预训练的语音编码器，需要有监督训练解码器。Whisper 采用多语种、多任务训练，以及 68wh 大规模低质量弱监督训练集，可以在广泛的语音环境中鲁棒地工作，不需要特定数据集的微调、zero-shot 性能也较好。</mark>
* <mark style="color:red;">创新点</mark>
  * <mark style="color:red;">通过在 decoder 端添加任务提示，实现 multitask 训练。且验证了多任务、多语种训练有知识迁移正收益。</mark>
  * <mark style="color:red;">训练数据量大。</mark>
  * <mark style="color:red;">训练时以 50% 的概率将当前音频之前的转写文本添加到解码器的上下文中。实际应用中，推理时可输入上一窗口的识别结果用于长音频识别，或特定领域、特定任务的提示。</mark>
  * <mark style="color:red;">直接预测原始的转写文本，而不需要单独的 ITN 模块，简化了流程。</mark>
* <mark style="color:red;">发现</mark>
  * <mark style="color:red;">训练数据每增加16倍，WER 减半。</mark>
  * 采用对数概率作为得分。从温度0开始，即始终选择最高概率的token。当生成 token 的平均对数概率低于 -1，或生成文本的 gzip 压缩率大于2.4时，将温度提高 0.2 直到 1.0，最终为 5 beams 的 beam search。当温度低于 0.5 时，将前一个窗口的转写文本作为上文输入。<mark style="color:red;">可以减少贪婪解码中更频繁出现的重复循环。</mark>

## 模型

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEBbd20803d649ee1ac60ace53dfad2bb8e?method=download&#x26;shareKey=e472e659f0649f8ee4ad233b75a56240" alt="" width="991"><figcaption></figcaption></figure>

| Size                    | Layers | Width | Heads | Parameters | Required VRAM | Relative speed |
| ----------------------- | ------ | ----- | ----- | ---------- | ------------- | -------------- |
| tiny                    | 4      | 384   | 6     | 39 M       | \~1 GB        | \~32x          |
| base                    | 6      | 512   | 8     | 74 M       | \~1 GB        | \~16x          |
| small                   | 12     | 768   | 12    | 244 M      | \~2 GB        | \~6x           |
| medium                  | 24     | 1024  | 16    | 769 M      | \~5 GB        | \~2x           |
| large/large-v2/large-v3 | 32     | 1280  | 20    | 1550 M     | \~10 GB       | 1x             |

* 音频：30s。没有语音的片段 $$10 \times$$ 下采样也加入训练。16kHz，80维FBank，25ms 窗长，10ms hop。
* 特征归一化：\[-1, 1]，零均值。
* $$2 \times Conv1D$$：kernel 3，第2层 stride 2。
* encoder output + layer norm
* 文本：BPE tokenizer。
* <mark style="color:red;">以 50% 的概率将当前音频之前的转写文本添加到解码器的上下文中。</mark>
* 时间戳预测：训练集中的时刻被量化为20ms分辨率。
* 存在音频截断时
  * 预测时间戳：最后只预测起始时刻token。
  * 不预测时间戳：截断音频，使其不包含不完整的token。
* <mark style="color:red;">多任务训练：口语语种识别、多语种语音识别、语音翻译、VAD。</mark>
* 超参数

  <figure><img src="https://note.youdao.com/yws/api/personal/file/WEBac5a0219964b788e71002aeeb36a0ed2?method=download&#x26;shareKey=57995ce43c68a48fde48a34814cbb555" alt="" width="360"><figcaption></figcaption></figure>
* Large V2

  <figure><img src="https://note.youdao.com/yws/api/personal/file/WEB35781e40821ba56bc40ec46a18412a5a?method=download&#x26;shareKey=f15bc329d64997e2dbaeb9c11fcd173b" alt="" width="335"><figcaption></figcaption></figure>
* Large V3
  * 音频特征：128 维 FBank。
  * 训练集：100wh 弱标注数据 + 400wh 采用 Large V2 生成的伪标签数据。
  * 2 epoch。
  * <https://github.com/openai/whisper/discussions/1762>

## 数据集

* 43.8wh 英语 ASR 数据（约占65%）
* 11.7wh 其它 96 个语种的识别数据。
* 12.6wh $$x \rightarrow \text {en}$$ 翻译数据。
* 各语种数据量详见附录E。

## 数据处理

* <mark style="color:red;">直接预测原始的转写文本，而不需要单独的 ITN 模块，简化了流程。</mark>
* 转写文本筛选
  * 删除机器生成的转写文本（如不包含逗号、感叹号、问号等复杂的标点符号，全大写或全小写）。在人工和机器混合生成的数据集上训练，会显著地损害翻译系统的性能。
  * 语种检测。剔除音频、文本语种不匹配的，但保留文本为英语的作为翻译样本。
  * 文本模糊去重。
* 训练得到初始模型后，对训练集中的数据源根据 WER、数据大小 排序，人工检查，剔除低质量数据源。
* 避免污染：对训练集和重叠风险很高的测试集 TED-LIUM 3 进行文本去重。
* 存在的问题：许多转写文本包含当前说话人的名字。Whisper 在不存在上述情况的子集上进行了微调。

## zero-shot 评价

### TN

* 计算 WER 前进行 TN，以减少非语义差异的惩罚。

#### 英语

1. 转小写
2. 删除\[]中的内容
3. 删除()中的内容
4. 删除 hmm, mm, mhm, mmm, uh, um
5. 删除 ' 前的 whitespace
6. 将标准的或非正式的缩略形式转换为原始形式
7. 删除数字间的逗号
8. 删除其后不为数字的句号
9. 删除除句号、百分比、货币符号之外的 Unicode 类别以 M、S、P开头的字符
   * M：Miscellaneous Symbols，包含箭头、几何形状、星号、圆圈等，如➡、★、○ 等
   * S：Symbols，包含货币符号、标点符号、数学符号、技术符号等，如$、%、℃ 等
   * P：Punctuation，标点符号
10. 将数字、货币转换为阿拉伯数字，如 Ten thousand dollars $$ightarrow$$ $10000
11. 将英式拼写转换为美式拼写
12. 删除前/后不为数字的百分比、句号、货币符号
13. 将连续的 whitespace 替换为 1个空格

#### 非英语

1. 删除\[]中的内容
2. 删除()中的内容
3. 将 NFKC-normalized 字符串中 Unicode 类别以 M、S、P 开头的字符替换为1个空格
4. 转小写
5. 将连续的 whitespace 替换为 1个空格
6. 对于不以空格分隔单词的语种，每个 Unicode 字符间插入1个空格

### 英语语音识别

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEBd35e737cea04cd547ac7bf9c27a2f70a?method=download&#x26;shareKey=8ff960ceb46e3ee800afe7f0f0cfecae" alt="" width="346"><figcaption></figcaption></figure>

* <mark style="color:red;">虽然在 LibriSpeech test-clean 上的 WER 高于 SOTA，但相较于在 LibriSpeech 上监督训练的模型，在其它测试集上的泛化性能更好。</mark>

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB836ace73368a699aefd86ad561fadfd7?method=download&#x26;shareKey=5dc9e9a441ae3044b1319d39750e2934" alt="" width="912"><figcaption></figcaption></figure>

### 多语种识别

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB849dfce200ed0c6e3d11466f25ca73d8?method=download&#x26;shareKey=987641e44832d417d9fefe85fe71cef4" alt="" width="408"><figcaption></figcaption></figure>

* VoxPopuli 数据集上效果较差，可能由于它具有更多的监督数据，有利于微调。

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB70ac88b46df672e6102bdd2b686554b3?method=download&#x26;shareKey=d23033c50f7911dad31a5f71d216021a" alt="" width="418"><figcaption></figcaption></figure>

* <mark style="color:red;">训练数据每增加16倍，WER 减半。</mark>训练数据大多为印欧语系，中文（ZH）、韩语（KO）等语种与上述规律偏离较远，可能的原因：语系迁移、tokenizer、数据质量。

### 语音翻译

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB772cfc893a6086de48d06fa99f5d6062?method=download&#x26;shareKey=5716b9ec8d97207bc03856217ba4bd8a" alt="" width="409"><figcaption></figcaption></figure>

* $$x \rightarrow \text {en}$$ <mark style="color:red;">翻译，在低资源语种上翻译效果显著提升，但在高资源语种上略差于 SOTA。</mark>
* 由于训练集创建时语种识别错误，导致部分英语音频转写任务被标记为翻译任务。

### 对噪声的鲁棒性

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEBf45f39c8413c859841e3dae7ef688dae?method=download&#x26;shareKey=e6777c38d08297ef607a589a1bf6ec85" alt="" width="414"><figcaption></figcaption></figure>

* <mark style="color:red;">在 LibriSpeech test-clean 上，高信噪比时，性能差于用 LibriSpeech 预训练或微调的模型，但在低信噪比时（如10dB pub 噪声），Whisper 性能较好。</mark>

### 长音频转写

* <mark style="color:red;">连续转写 30s 的片段，根据模型预测的时间戳来移动窗口。</mark>
* 采用对数概率作为得分。从温度0开始，即始终选择最高概率的token。当生成 token 的平均对数概率低于 -1，或生成文本的 gzip 压缩率大于2.4时，将温度提高 0.2 直到 1.0，最终为 5 beams 的 beam search。当温度低于 0.5 时，将前一个窗口的转写文本作为上文输入。可以减少贪婪解码中更频繁出现的重复循环。
* 结合 no-speech 概率阈值 0.6、平均对数概率阈值 -1，可以更可靠地检测没有语音的片段。
* 约束第一个时间戳在 0～1s，避免模型忽略前几个词。

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB76755ca27d1951139ef4c367ac9656bb?method=download&#x26;shareKey=c5e47638f97e0a25399aca971e2852a9" alt="" width="392"><figcaption></figcaption></figure>

* 在所有数据集上，均优于 SOTA 开源模型 NVIDIA STT Conformer-CTC Large model (Nemo)；在大部分数据集上，优于商业 ASR 系统（训练集可能包含这些公开测试集）。

### 与人类比较

* 由于歧义、不清晰的语音、label 错误等，每个数据集都有不同程度的错误。仅采用 WER 很难衡量还有多少改进空间。
* 从 Kincaid46 数据集中挑选了25条音频，录音环境涵盖有/无脚本的广播、电话、VoIP 通话、会议。获得5份专业转写员的转写，其中4份完全由人工转写，1份提供了机器辅助转写。

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEBaafd7b2add354903a7ef71e0cc969dbb?method=download&#x26;shareKey=173a38db52d391d654fec5057da63e62" alt="" width="380"><figcaption></figcaption></figure>

* 计算机辅助转写的 WER 最低。Whisper 的英语 ASR 性能接近人类水平，只比纯人工转写的 WER 差不到1个百分点。

### 语种识别

### VAD

## 消融实验

### 模型、数据规模

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB76db9ac4e5dc83608d5834e22c85f9c3?method=download&#x26;shareKey=321535b080bd0292a0690a1dcff41728" alt="" width="953"><figcaption></figcaption></figure>

* 随着模型大小的增加，英语语音识别的性能接近饱和，可能由于其已接近人类水平。多语种语音识别、语音翻译、语种识别的性能不断提高。

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEB8c5929fe495c4089b905cd581b9a0cd6?method=download&#x26;shareKey=dcaa4abc5367d7f32a7bd20e0e4085a3" alt="" width="387"><figcaption></figcaption></figure>

\* Whisper medium

* 通过更长的训练时间、更大的模型，可以进一步提高性能。
* <mark style="color:green;">其它语种、任务是否需要和英语 ASR 相当的数据量来达到最佳性能？</mark>
* 数据分布变化后，是否损伤英语 ASR 的性能？
* 接近人类水平是否是模型的上限？
* 在当前数据集上，扩大模型容量的收益已接近饱和。继续增加英语 ASR 的数据量是否有意义？数据量增加后进一步扩大模型容量是否有收益？

### 多任务、多语种迁移

<figure><img src="https://note.youdao.com/yws/api/personal/file/WEBc6e4ba8f86c64ac0c4f6822672788ab2?method=download&#x26;shareKey=23f20f11fb6137e67baa5297d501e93e" alt="" width="389"><figcaption></figcaption></figure>

* <mark style="color:red;">相较于仅在英语 ASR 任务上训练的模型，相同的英语 ASR 训练计算量，采用多任务、多语种训练，小模型性能较差，但扩展到更大的模型时可以超越单一任务模型的性能，从其它任务中获得知识迁移正收益。</mark>

## 展望

* <mark style="color:red;">存在的问题：陷入重复循环、不转写音频片段的前几个或后几个单词、输出与音频完全无关的文本等，特别是在长音频上。可能的改进方案：在高质量监督数据集上微调Whisper；强化学习。</mark>
* 增加低资源语种的训练数据。
* 微调。
* 通过消融实验研究 encoder、decoder 分别的收益：比如训练无解码器的CTC模型、对比现有语音识别 encoder (如 wav2vec 2.0) + LM 的性能。
* 添加无监督预训练或 self-teaching。

## 缺点

* 不支持流式

## TODO：代码

* <mark style="color:red;">提示输出简体中文，而非繁体字</mark>`whisper --language Chinese --model large audio.wav --initial_prompt "以下是普通话的句子。" # simplified`
* huggingface
  * <mark style="color:green;">时间戳预测</mark><mark style="color:green;">`return_timestamps=True # 句子级时间戳return_timestamps=word # 词级时间戳`</mark>
  * 速度、内存优化
    * 长音频转写：chunked 算法\
      Gandhi S, von Platen P, Rush A M. Distil-Whisper: Robust Knowledge Distillation via Large-Scale Pseudo Labelling\[J]. arXiv preprint arXiv:2311.00430, 2023.
    * flash\_attention\_2
    * Torch Scale-Product-Attention (SDPA)
  * 微调：<https://huggingface.co/blog/zh/fine-tune-whisper>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nanyang.gitbook.io/meng-yuan/yin-pin/yu-yin-shi-bie/mo-xing/whisper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
