speechbrain.decoders.ctc 模块

用于CTC的解码器和输出归一化。

Authors
  • Mirco Ravanelli 2020

  • 阿库·柔赫 2020

  • 叶松林 2020

  • 阿德尔·穆门 2023, 2024

摘要

类:

CTCBaseSearcher

CTCBaseSearcher 类,供其他 CTC 波束搜索器继承。

CTCBeam

该类处理解码过程中的CTC波束信息。

CTCBeamSearcher

CTC Beam Search 是一种用于 CTC 的 Beam Search,它不跟踪空白和非空白概率。

CTCHypothesis

该类是生成假设的数据处理器。

CTCPrefixBeamSearcher

CTC 前缀束搜索基于 Awni Y. 的论文 First-Pass Large Vocabulary Continuous Speech Recognition using Bi-Directional Recurrent DNNs

CTCPrefixScore

该类实现了参考文献中算法2的CTC前缀评分:https://www.merl.com/publications/docs/TR2017-190.pdf

LMCTCBeam

该类在解码过程中处理LM分数。

TorchAudioCTCPrefixBeamSearcher

TorchAudio CTC 前缀束搜索解码器。

函数:

ctc_greedy_decode

贪婪解码一批概率并应用CTC规则。

filter_ctc_output

应用CTC输出合并和过滤规则。

参考

class speechbrain.decoders.ctc.CTCPrefixScore(x, enc_lens, blank_index, eos_index, ctc_window_size=0)[source]

基础类:object

该类实现了参考文献中算法2的CTC前缀评分:https://www.merl.com/publications/docs/TR2017-190.pdf。 官方实现:https://github.com/espnet/espnet/blob/master/espnet/nets/ctc_prefix_score.py

Parameters:
  • x (torch.Tensor) – 编码器状态。

  • enc_lens (torch.Tensor) – 每个enc_states序列的实际长度。

  • blank_index (int) – 空白标记的索引。

  • eos_index (int) – 序列结束(eos)标记的索引。

  • ctc_window_size (int) – 使用基于注意力峰值的时间窗口计算ctc分数。 如果为0,则不应用窗口。

forward_step(inp_tokens, states, candidates=None, attn=None)[source]

此方法是前缀ctc评分器的一步前向操作。

Parameters:
  • inp_tokens (torch.Tensor) – 前缀标签序列 g 的最后字符,其中 h = g + c。

  • states (tuple) – 之前的ctc状态。

  • candidates (torch.Tensor) – (batch_size * beam_size, ctc_beam_size), 用于重新评分的topk候选。 如果给定,则执行部分ctc评分。

  • attn (torch.Tensor) – (batch_size * beam_size, max_enc_len), 注意力权重。

Returns:

  • new_psi (torch.Tensor)

  • (r, psi, scoring_table) (tuple)

permute_mem(memory, index)[source]

此方法对CTC模型内存进行排列,以使内存索引与当前输出同步。

Parameters:
  • memory (无限制) – 需要置换的内存变量。

  • index (torch.Tensor) – 前一个路径的索引。

Return type:

被置换的内存变量。

speechbrain.decoders.ctc.filter_ctc_output(string_pred, blank_id=-1)[source]

应用CTC输出合并和过滤规则。

移除空白符号并输出重复项。

Parameters:
  • string_pred (list) – 包含由CTC系统预测的输出字符串/整数的列表。

  • blank_id (int, string) – 空白的ID。

Returns:

CTC预测的输出,不包括空白符号和重复部分。

Return type:

list

Example

>>> string_pred = ['a','a','blank','b','b','blank','c']
>>> string_out = filter_ctc_output(string_pred, blank_id='blank')
>>> print(string_out)
['a', 'b', 'c']
speechbrain.decoders.ctc.ctc_greedy_decode(probabilities, seq_lens, blank_id=-1)[source]

贪婪解码一批概率并应用CTC规则。

Parameters:
  • 概率 (torch.tensor) – 来自网络的输出概率(或对数概率),形状为 [batch, lengths, probabilities]

  • seq_lens (torch.tensor) – 相对真实序列长度(用于处理填充输入), 最长序列的长度为1.0,其他序列的长度在零到一之间 形状为 [batch, lengths]。

  • blank_id (int, string) – 空白符号/索引。默认值:-1。如果给出一个负数,则假定它表示从最大可能索引开始倒数,因此-1表示最大可能索引。

Returns:

输出为Python的列表的列表,具有“不规则”维度;填充已被移除。

Return type:

list

Example

>>> import torch
>>> probs = torch.tensor([[[0.3, 0.7], [0.0, 0.0]],
...                       [[0.2, 0.8], [0.9, 0.1]]])
>>> lens = torch.tensor([0.51, 1.0])
>>> blank_id = 0
>>> ctc_greedy_decode(probs, lens, blank_id)
[[1], [1]]
class speechbrain.decoders.ctc.CTCBeam(text: str, full_text: str, next_word: str, partial_word: str, last_token: str | None, last_token_index: int | None, text_frames: List[Tuple[int, int]], partial_frames: Tuple[int, int], p: float = -inf, p_b: float = -inf, p_nb: float = -inf, n_p_b: float = -inf, n_p_nb: float = -inf, score: float = -inf, score_ctc: float = -inf)[source]

基础类:object

该类处理解码过程中的CTC波束信息。

Parameters:
  • text (str) – 光束的当前文本。

  • full_text (str) – 光束的完整文本。

  • next_word (str) – 要添加到beam中的下一个单词。

  • partial_word (str) – 正在添加到beam中的部分单词。

  • last_token (str, optional) – 光束的最后一个标记。

  • last_token_index (int, optional) – 光束中最后一个标记的索引。

  • text_frames (List[Tuple[int, int]]) – 文本的起始和结束帧。

  • partial_frames (Tuple[int, int]) – 部分单词的起始帧和结束帧。

  • p (float) – 光束的概率。

  • p_b (float) – 光束以空白结束的概率。

  • p_nb (float) – 光束不以空白结束的概率。

  • n_p_b (float) – 光束以空白结束的先前概率。

  • n_p_nb (float) – 光束不以空白结束的先前概率。

  • score (float) – 光束的得分(语言模型 + CTC)

  • score_ctc (float) – 计算得到的CTC分数。

Example

>>> beam = CTCBeam(
...     text="",
...     full_text="",
...     next_word="",
...     partial_word="",
...     last_token=None,
...     last_token_index=None,
...     text_frames=[(0, 0)],
...     partial_frames=(0, 0),
...     p=-math.inf,
...     p_b=-math.inf,
...     p_nb=-math.inf,
...     n_p_b=-math.inf,
...     n_p_nb=-math.inf,
...     score=-math.inf,
...     score_ctc=-math.inf,
... )
text: str
full_text: str
next_word: str
partial_word: str
last_token: str | None
last_token_index: int | None
text_frames: List[Tuple[int, int]]
partial_frames: Tuple[int, int]
p: float = -inf
p_b: float = -inf
p_nb: float = -inf
n_p_b: float = -inf
n_p_nb: float = -inf
score: float = -inf
score_ctc: float = -inf
classmethod from_lm_beam(lm_beam: LMCTCBeam) CTCBeam[source]

从LMCTCBeam创建一个CTCBeam

Parameters:

lm_beam (LMCTCBeam) – 要转换的LMCTCBeam。

Returns:

CTCBeam 已转换。

Return type:

CTCBeam

step() None[source]

更新光束概率。

class speechbrain.decoders.ctc.LMCTCBeam(text: str, full_text: str, next_word: str, partial_word: str, last_token: str | None, last_token_index: int | None, text_frames: List[Tuple[int, int]], partial_frames: Tuple[int, int], p: float = -inf, p_b: float = -inf, p_nb: float = -inf, n_p_b: float = -inf, n_p_nb: float = -inf, score: float = -inf, score_ctc: float = -inf, lm_score: float = -inf)[source]

基础类: CTCBeam

该类处理解码过程中的LM分数。

Parameters:
  • lm_score (float) – 光束的LM分数。

  • **kwargs – 请参阅 CTCBeam 以了解其他参数。

lm_score: float = -inf
class speechbrain.decoders.ctc.CTCHypothesis(text: str, last_lm_state: None, score: float, lm_score: float, text_frames: list = None)[source]

基础类:object

这个类是一个数据处理器,用于处理生成的假设。

此类是CTC波束搜索器的默认输出。

如果以在线方式使用波束搜索器,它可以被其他解码器重复使用。

Parameters:
  • 文本 (str) – 假设的文本。

  • last_lm_state (None) – 假设的最后一个语言模型状态。

  • score (float) – 假设的得分。

  • lm_score (float) – 假设的LM分数。

  • text_frames (List[Tuple[str, Tuple[int, int]]], optional) – 文本及其对应帧的列表。

text: str
last_lm_state: None
score: float
lm_score: float
text_frames: list = None
class speechbrain.decoders.ctc.CTCBaseSearcher(blank_index: int, vocab_list: List[str], space_token: str = ' ', kenlm_model_path: None | str = None, unigrams: None | List[str] = None, alpha: float = 0.5, beta: float = 1.5, unk_score_offset: float = -10.0, score_boundary: bool = True, beam_size: int = 100, beam_prune_logp: int = -10.0, token_prune_min_logp: int = -5.0, prune_history: bool = True, blank_skip_threshold: None | int = 1.0, topk: int = 1, spm_token: str = '▁')[source]

基础:Module

CTCBaseSearcher 类将被其他 CTC 波束搜索器继承。

该类提供了CTC束搜索解码的基本功能。

如果您的转录预期包含空格,则在使用非句子片段词汇表时需要 space_token。

Parameters:
  • blank_index (int) – 空白标记的索引。

  • vocab_list (list) – 词汇标记的列表。

  • space_token (int, 可选) – 空间令牌的索引。(默认值:-1)

  • kenlm_model_path (str, optional) – kenlm模型的路径。使用.bin文件可以加快加载速度。 如果为None,则不使用语言模型。(默认值: None)

  • unigrams (list, optional) – 已知单词unigrams的列表。(默认值:None)

  • alpha (float) – 浅层融合期间语言模型的权重。(默认值:0.5)

  • beta (float) – 评分期间长度分数调整的权重。(默认值:1.5)

  • unk_score_offset (float) – 未知标记的日志分数偏移量。(默认值:-10.0)

  • score_boundary (bool) – 是否在评分时让kenlm尊重边界。(默认值: True)

  • beam_size (int, optional) – 光束的宽度。(默认值:100)

  • beam_prune_logp (float, optional) – 光束的修剪阈值。(默认值: -10.0)

  • token_prune_min_logp (float, optional) – 用于标记的修剪阈值。(默认值: -5.0)

  • prune_history (bool, optional) – 是否修剪历史记录。(默认值:True) 注意:当使用 topk > 1 时,应将其设置为 False,因为它会修剪大量光束。

  • blank_skip_threshold (float, optional) – 如果 log_prob(blank) > log(blank_skip_threshold),则跳过帧,以加快解码速度。 注意:这仅在使用了CUDA解码器时使用,并且可能会使WER/CER结果变差。使用时请自行承担风险。(默认值: 1.0)

  • topk (int, 可选) – 返回的前几个假设的数量。(默认值:1)

  • spm_token (str, 可选) – 句子片段标记。(默认值:“▁”)

Example

>>> blank_index = 0
>>> vocab_list = ['blank', 'a', 'b', 'c', ' ']
>>> space_token = ' '
>>> kenlm_model_path = None
>>> unigrams = None
>>> beam_size = 100
>>> beam_prune_logp = -10.0
>>> token_prune_min_logp = -5.0
>>> prune_history = True
>>> blank_skip_threshold = 1.0
>>> topk = 1
>>> searcher = CTCBaseSearcher(
...     blank_index=blank_index,
...     vocab_list=vocab_list,
...     space_token=space_token,
...     kenlm_model_path=kenlm_model_path,
...     unigrams=unigrams,
...     beam_size=beam_size,
...     beam_prune_logp=beam_prune_logp,
...     token_prune_min_logp=token_prune_min_logp,
...     prune_history=prune_history,
...     blank_skip_threshold=blank_skip_threshold,
...     topk=topk,
... )
partial_decoding(log_probs: Tensor, beams: List[CTCBeam], cached_lm_scores: dict, cached_p_lm_scores: dict, processed_frames: int = 0)[source]

执行解码的单个步骤。

Parameters:
  • log_probs (torch.Tensor) – CTC输出的对数概率。

  • beams (list) – 光束的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_p_lm_scores (dict) – 缓存的prefix语言模型分数。

  • processed_frames (int, 默认值: 0) – 当前解码步骤的起始帧。

normalize_whitespace(text: str) str[source]

高效地规范化空白字符。

Parameters:

文本 (str) – 要标准化的文本。

Returns:

标准化文本。

Return type:

str

merge_tokens(token_1: str, token_2: str) str[source]

合并两个令牌,并避免空的令牌。

取自:https://github.com/kensho-technologies/pyctcdecode

Parameters:
  • token_1 (str) – 第一个token。

  • token_2 (str) – 第二个令牌。

Returns:

合并后的令牌。

Return type:

str

merge_beams(beams: List[CTCBeam]) List[CTCBeam][source]

合并具有相同文本的光束。

取自:https://github.com/kensho-technologies/pyctcdecode

Parameters:

beams (list) – 光束的列表。

Returns:

CTCBeam合并的列表。

Return type:

list

sort_beams(beams: List[CTCBeam]) List[CTCBeam][source]

按lm_score排序光束。

Parameters:

beams (list) – CTCBeam的列表。

Returns:

CTCBeam排序后的列表。

Return type:

list

finalize_decoding(beams: List[CTCBeam], cached_lm_scores: dict, cached_p_lm_scores: dict, force_next_word=False, is_end=False) List[CTCBeam][source]

通过添加并评分最后一个部分词来完成解码过程。

Parameters:
  • beams (list) – CTCBeam的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_p_lm_scores (dict) – 缓存的prefix语言模型分数。

  • force_next_word (bool, 默认值: False) – 是否强制下一个单词。

  • is_end (bool, 默认值: False) – 是否已达到序列的末尾。

Returns:

CTCBeam的列表。

Return type:

list

decode_beams(log_probs: Tensor, wav_lens: Tensor | None = None, lm_start_state: Any = None) List[List[CTCHypothesis]][source]

解码CTC输出的输入对数概率。

它会自动将SpeechBrain的wav输入的相对长度转换为绝对长度。

确保输入在log域中。解码器将无法解码logits或概率。输入应为CTC输出的log概率。

Parameters:
  • log_probs (torch.Tensor) – CTC输出的对数概率。 预期的形状是[batch_size, seq_length, vocab_size]。

  • wav_lens (torch.Tensor, optional (default: None)) – SpeechBrain 的 wav 输入的相对长度。

  • lm_start_state (Any, optional (default: None)) – 语言模型的初始状态。

Returns:

CTCHypothesis的topk列表。

Return type:

listlist

__call__(log_probs: Tensor, wav_lens: Tensor | None = None, lm_start_state: Any = None) List[List[CTCHypothesis]][source]

解码CTC输出的对数概率。

它会自动将SpeechBrain的wav输入的相对长度转换为绝对长度。

每个张量都被转换为numpy和CPU,因为它更快且消耗更少的内存。

Parameters:
  • log_probs (torch.Tensor) – CTC输出的对数概率。 预期的形状是[batch_size, seq_length, vocab_size]。

  • wav_lens (torch.Tensor, optional (默认值: None)) – SpeechBrain 中 wav 输入的相对长度。

  • lm_start_state (Any, optional (default: None)) – 语言模型的初始状态。

Returns:

CTCHypothesis 的 topk 列表。

Return type:

listlist

partial_decode_beams(log_probs: Tensor, cached_lm_scores: dict, cached_p_lm_scores: dict, beams: List[CTCBeam], processed_frames: int, force_next_word=False, is_end=False) List[CTCBeam][source]

执行解码的单个步骤。

Parameters:
  • log_probs (torch.Tensor) – CTC输出的对数概率。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_p_lm_scores (dict) – 缓存的prefix语言模型分数。

  • beams (list) – 光束的列表。

  • processed_frames (int) – 当前解码步骤的起始帧。

  • force_next_word (bool, 可选 (默认: False)) – 是否强制下一个单词。

  • is_end (bool, 可选 (默认: False)) – 是否已达到序列的末尾。

Returns:

CTCBeam的列表。

Return type:

list

decode_log_probs(log_probs: Tensor, wav_len: int, lm_start_state: Any | None = None) List[CTCHypothesis][source]

解码CTC输出的对数概率。

Parameters:
  • log_probs (torch.Tensor) – CTC输出的对数概率。 预期的形状是[seq_length, vocab_size]。

  • wav_len (int) – wav输入的长度。

  • lm_start_state (Any, optional (default: None)) – 语言模型的初始状态。

Returns:

CTCHypothesis 的 topk 列表。

Return type:

list

class speechbrain.decoders.ctc.CTCBeamSearcher(blank_index: int, vocab_list: List[str], space_token: str = ' ', kenlm_model_path: None | str = None, unigrams: None | List[str] = None, alpha: float = 0.5, beta: float = 1.5, unk_score_offset: float = -10.0, score_boundary: bool = True, beam_size: int = 100, beam_prune_logp: int = -10.0, token_prune_min_logp: int = -5.0, prune_history: bool = True, blank_skip_threshold: None | int = 1.0, topk: int = 1, spm_token: str = '▁')[source]

基础类: CTCBaseSearcher

CTC Beam Search 是一种用于 CTC 的 Beam Search,它不跟踪空白和非空白概率。每个新标记的概率被添加到总分数中,并且共享相同文本的每个 beam 都会被合并在一起。

该实现支持对单词和SentencePiece标记进行n-gram评分。输入应为形状为[batch, time, vocab_size]的对数概率张量。

与CTCPrefixBeamSearcher相比,CTCBeamSearcher的主要优势在于它相对更快,并且能获得稍好的结果。然而,该实现基于PyCTCDecode工具包中的实现,根据SpeechBrain的需求进行了调整,并没有遵循特定的论文。如果您想引用解码方法的适当论文,我们建议使用CTCPrefixBeamSearcher。

为了实现解码过程的加速,采用了多种启发式方法: - 剪枝光束:如果光束的得分低于某个值,则对其进行剪枝

最佳光束分数减去beam_prune_logp

  • pruning of the tokensthe tokens are pruned if their score is lower than

    token_prune_min_logp

  • pruning of the historythe beams are pruned if they are the same over

    最大n元历史

  • skipping of the blankthe frame is skipped if the blank probability is

    高于 blank_skip_threshold

注意:如果声学模型未经过训练,波束搜索将花费大量时间。我们建议在验证期间使用贪心搜索,直到模型完全训练并准备好用于测试集评估。

Parameters:
  • CTCBaseSearcher (参见)

  • 通过。 (参数是直接的)

Example

>>> import torch
>>> from speechbrain.decoders import CTCBeamSearcher
>>> probs = torch.tensor([[[0.2, 0.0, 0.8],
...                   [0.4, 0.0, 0.6]]])
>>> log_probs = torch.log(probs)
>>> lens = torch.tensor([1.0])
>>> blank_index = 2
>>> vocab_list = ['a', 'b', '-']
>>> searcher = CTCBeamSearcher(blank_index=blank_index, vocab_list=vocab_list)
>>> hyps = searcher(probs, lens)
get_lm_beams(beams: List[CTCBeam], cached_lm_scores: dict, cached_partial_token_scores: dict, is_eos=False) List[LMCTCBeam][source]

如果语言模型不为None,则对光束进行评分,并返回新的光束。

此函数修改并改编自 https://github.com/kensho-technologies/pyctcdecode

Parameters:
  • beams (list) – 光束的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_partial_token_scores (dict) – 缓存的局部令牌分数。

  • is_eos (bool (默认值: False)) – 是否已达到序列的末尾。

Returns:

new_beams – 新光束的列表。

Return type:

list

partial_decoding(log_probs: Tensor, wav_len: int, beams: List[CTCBeam], cached_lm_scores: dict, cached_p_lm_scores: dict, processed_frames: int = 0) List[CTCBeam][source]

执行CTC前缀束搜索解码。

如果 self.lm 不是 None,则计算语言模型分数并将其添加到 CTC 分数中。

Parameters:
  • log_probs (torch.Tensor) – CTC输入的log概率。 形状: (seq_length, vocab_size)

  • wav_len (int) – 输入序列的长度。

  • beams (list) – CTCBeam 对象的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_p_lm_scores (dict) – 缓存的prefix语言模型分数。

  • processed_frames (int) – 当前解码步骤的起始帧。(默认值: 0)

Returns:

beams – CTCBeam 对象的列表。

Return type:

list

class speechbrain.decoders.ctc.CTCPrefixBeamSearcher(blank_index: int, vocab_list: List[str], space_token: str = ' ', kenlm_model_path: None | str = None, unigrams: None | List[str] = None, alpha: float = 0.5, beta: float = 1.5, unk_score_offset: float = -10.0, score_boundary: bool = True, beam_size: int = 100, beam_prune_logp: int = -10.0, token_prune_min_logp: int = -5.0, prune_history: bool = True, blank_skip_threshold: None | int = 1.0, topk: int = 1, spm_token: str = '▁')[source]

基础类: CTCBaseSearcher

CTC前缀束搜索基于Awni Y. Hannun等人的论文 First-Pass Large Vocabulary Continuous Speech Recognition using Bi-Directional Recurrent DNNs (https://arxiv.org/abs/1408.2873)。

实现跟踪空白和非空白概率。 它还支持对单词和SentencePiece标记进行n-gram评分。输入 应为形状为[batch, time, vocab_size]的对数概率张量。

为了实现解码过程的加速,采用了多种启发式方法: - 剪枝光束:如果光束的得分低于某个值,则对其进行剪枝

最佳光束分数减去beam_prune_logp

  • pruning of the tokensthe tokens are pruned if their score is lower than

    token_prune_min_logp

  • pruning of the historythe beams are pruned if they are the same over

    最大n元历史

  • skipping of the blankthe frame is skipped if the blank probability is

    高于 blank_skip_threshold

注意:CTCPrefixBeamSearcher 可能比 CTCBeamSearcher 或 TorchAudioCTCPrefixBeamSearch 搜索器更不稳定。请谨慎使用,并仔细检查结果。

注意:如果声学模型未经过训练,波束搜索将花费大量时间。我们建议在验证期间使用贪心搜索,直到模型完全训练并准备好用于测试集评估。

注意:此实现不提供假设的时间对齐。如果您需要,请使用CTCBeamSearcher。

Parameters:
  • CTCBaseSearcher (参见)

  • 通过。 (参数是直接的)

Example

>>> import torch
>>> from speechbrain.decoders import CTCPrefixBeamSearcher
>>> probs = torch.tensor([[[0.2, 0.0, 0.8],
...                   [0.4, 0.0, 0.6]]])
>>> log_probs = torch.log(probs)
>>> lens = torch.tensor([1.0])
>>> blank_index = 2
>>> vocab_list = ['a', 'b', '-']
>>> searcher = CTCPrefixBeamSearcher(blank_index=blank_index, vocab_list=vocab_list)
>>> hyps = searcher(probs, lens)
get_lm_beams(beams: List[CTCBeam], cached_lm_scores: dict, cached_partial_token_scores: dict, is_eos=False) List[LMCTCBeam][source]

如果语言模型不为None,则对光束进行评分,并返回新的光束。

此函数修改并改编自 https://github.com/kensho-technologies/pyctcdecode

Parameters:
  • beams (list) – 光束的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_partial_token_scores (dict) – 缓存的局部令牌分数。

  • is_eos (bool (默认值: False)) – 是否已达到序列的末尾。

Returns:

new_beams – 新光束的列表。

Return type:

list

partial_decoding(log_probs: Tensor, wav_len: int, beams: List[CTCBeam], cached_lm_scores: dict, cached_p_lm_scores: dict, processed_frames: int = 0) List[CTCBeam][source]

执行CTC前缀束搜索解码。

如果 self.lm 不是 None,则计算语言模型分数并将其添加到 CTC 分数中。

Parameters:
  • log_probs (torch.Tensor) – CTC输入的log概率。 形状: (seq_length, vocab_size)

  • wav_len (int) – 输入序列的长度。

  • beams (list) – CTCBeam对象的列表。

  • cached_lm_scores (dict) – 缓存的语言模型分数。

  • cached_p_lm_scores (dict) – 缓存的prefix语言模型分数。

  • processed_frames (int) – 当前解码步骤的起始帧。(默认值: 0)

Returns:

beams – CTCBeam 对象的列表。

Return type:

list

class speechbrain.decoders.ctc.TorchAudioCTCPrefixBeamSearcher(tokens: list | str, lexicon: str | None = None, lm: str | None = None, lm_dict: str | None = None, topk: int = 1, beam_size: int = 50, beam_size_token: int | None = None, beam_threshold: float = 50, lm_weight: float = 2, word_score: float = 0, unk_score: float = -inf, sil_score: float = 0, log_add: bool = False, blank_index: str | int = 0, sil_index: str | int = 0, unk_word: str = '<unk>', using_cpu_decoder: bool = True, blank_skip_threshold: float = 1.0)[source]

基础类:object

TorchAudio CTC前缀束搜索解码器。

该类是TorchAudio中CTC解码器的封装。它提供了一个简单的接口,您可以使用CPU或CUDA CTC解码器。

CPU解码器速度较慢,但占用内存较少。CUDA解码器速度较快,但占用内存较多。CUDA解码器仅在torchaudio的夜间版本中可用。

CUDA解码器缺少许多功能,例如使用语言模型、约束搜索等功能。如果你想使用这些功能,你必须使用CPU解码器。

有关CPU解码器的更多信息,请参阅TorchAudio的文档: https://pytorch.org/audio/main/generated/torchaudio.models.decoder.ctc_decoder.html

有关CUDA解码器的更多信息,请参阅TorchAudio的文档: https://pytorch.org/audio/main/generated/torchaudio.models.decoder.cuda_ctc_decoder.html#torchaudio.models.decoder.cuda_ctc_decoder

如果你想使用语言模型或词典搜索,请确保你的分词器/声学模型使用与语言模型/词典相同的标记。否则,解码将失败。

该实现与SentencePiece Tokens兼容。

注意:使用CUDA CTC解码器时,blank_index必须为0。此外,使用CUDA CTC解码器需要torchaudio的nightly版本和大量的VRAM内存(如果你想使用大量的beam)。总的来说,我们建议在SpeechBrain中使用CTCBeamSearcher或CTCPrefixBeamSearcher,如果你想使用n-gram + beam search解码。如果你想要进行约束搜索,请使用torchaudio的CPU版本,如果你想尽可能加快解码速度,请使用CUDA版本。

Parameters:
  • tokens (liststr) – 令牌列表或令牌文件的路径。 如果这是一个路径,那么文件应该每行包含一个令牌。

  • 词汇表 (str, 默认: None) – 包含可能的单词及其对应拼写的词汇表文件。每行包含一个单词及其空格分隔的拼写。 如果为None,则使用无词汇表解码。(默认: None)

  • lm (str, 可选) – 包含KenLM语言模型的路径,如果不使用语言模型则为None。(默认值:None)

  • lm_dict (str, 可选) – 用于语言模型的字典文件,每行一个单词,按语言模型索引排序。 如果使用词典进行解码,lm_dict中的条目也必须在词典文件中出现。 如果为None,则使用词典文件构建语言模型的字典。(默认值:None)

  • topk (int, optional) – 返回的顶级CTCHypothesis的数量。(默认值:1)

  • beam_size (int, 可选) – 每次解码步骤后保留的假设数量。(默认值:50)

  • beam_size_token (int, optional) – 在每个解码步骤中考虑的最大令牌数。如果为None,则设置为令牌的总数。(默认值: None)

  • beam_threshold (float, optional) – 用于剪枝假设的阈值。(默认值: 50)

  • lm_weight (float, optional) – 语言模型的权重。(默认值: 2)

  • word_score (float, optional) – 单词插入分数。(默认值:0)

  • unk_score (float, 可选) – 未知词插入分数。 (默认: float("-inf"))

  • sil_score (float, 可选) – 静音插入分数。 (默认: 0)

  • log_add (bool, optional) – 是否在合并假设时使用logadd。(默认值:False)

  • blank_index (intstr, 可选) – 空白标记的索引。如果tokens是文件路径,则这应该是一个str。否则,这应该是一个int。(默认值:0)

  • sil_index (intstr, 可选) – 静音标记的索引。如果tokens是文件路径,则这应该是一个str。否则,这应该是一个int。(默认值:0)

  • unk_word (str, 可选) – 未知词标记。 (默认: “”)

  • using_cpu_decoder (bool, optional) – 是否使用CPU搜索器。如果为False,则使用CUDA解码器。(默认值:True)

  • blank_skip_threshold (float, 可选) – 如果 log_prob(blank) > log(blank_skip_threshold),则跳过帧,以加快解码速度(默认值:1.0)。 注意:这仅在使用了CUDA解码器时使用,并且可能会使WER/CER结果变差。使用时请自行承担风险。

Example

>>> import torch
>>> from speechbrain.decoders import TorchAudioCTCPrefixBeamSearcher
>>> probs = torch.tensor([[[0.2, 0.0, 0.8],
...                   [0.4, 0.0, 0.6]]])
>>> log_probs = torch.log(probs)
>>> lens = torch.tensor([1.0])
>>> blank_index = 2
>>> vocab_list = ['a', 'b', '-']
>>> searcher = TorchAudioCTCPrefixBeamSearcher(tokens=vocab_list, blank_index=blank_index, sil_index=blank_index) 
>>> hyps = searcher(probs, lens) 
decode_beams(log_probs: Tensor, wav_len: Tensor | None = None) List[List[CTCHypothesis]][source]

使用TorchAudio CTC解码器解码log_probs。

如果 using_cpu_decoder=True 则在解码之前将 log_probs 和 wav_len 移动到 CPU。 当使用 CUDA CTC 解码器时,时间步信息不可用。因此,返回的假设中的时间步被设置为 None。

确保输入在log域中。解码器将无法解码logits或概率。输入应为CTC输出的log概率。

Parameters:
  • log_probs (torch.Tensor) – 输入音频的对数概率。 形状: (batch_size, seq_length, vocab_size)

  • wav_len (torch.Tensor, 默认值: None) – 语音大脑风格的相对长度。形状: (batch_size,) 如果为None,则假定每个音频的长度为seq_length。

Returns:

解码后的假设。外部列表是批处理维度,内部列表是topk维度。

Return type:

listlistCTCHypothesis

__call__(log_probs: Tensor, wav_len: Tensor | None = None) List[List[CTCHypothesis]][source]

使用TorchAudio CTC解码器解码log_probs。

如果 using_cpu_decoder=True 则在解码之前将 log_probs 和 wav_len 移动到 CPU。 当使用 CUDA CTC 解码器时,时间步信息不可用。因此,返回的假设中的时间步被设置为 None。

Parameters:
  • log_probs (torch.Tensor) – 输入音频的对数概率。 形状: (batch_size, seq_length, vocab_size)

  • wav_len (torch.Tensor, 默认值: None) – 语音大脑风格的相对长度。形状: (batch_size,) 如果为None,则假定每个音频的长度为seq_length。

Returns:

解码后的假设。外部列表是批处理维度,内部列表是topk维度。

Return type:

listlistCTCHypothesis