Skip to content

Enabling word-level timestamps for Wav2Vec 2.0 #3627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/speech_recognition/new/decoders/flashlight_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ def __init__(self, cfg: FlashlightDecoderConfig, tgt_dict: Dictionary) -> None:
self.decoder_opts, self.lm, self.silence, self.blank, []
)

def get_timesteps(self, token_idxs: List[int]) -> List[int]:
"""Returns frame numbers corresponding to every non-blank token.

Parameters
----------
token_idxs : List[int]
IDs of decoded tokens.

Returns
-------
List[int]
Frame numbers corresponding to every non-blank token.
"""
timesteps = []
for i, token_idx in enumerate(token_idxs):
if token_idx == self.blank:
continue
if i == 0 or token_idx != token_idxs[i-1]:
timesteps.append(i)
return timesteps

def decode(
self,
emissions: torch.FloatTensor,
Expand All @@ -134,6 +155,7 @@ def decode(
{
"tokens": self.get_tokens(result.tokens),
"score": result.score,
"timesteps": self.get_timesteps(result.tokens),
"words": [
self.word_dict.get_entry(x) for x in result.words if x >= 0
],
Expand Down
22 changes: 22 additions & 0 deletions examples/speech_recognition/w2l_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gc
import itertools as it
import os.path as osp
from typing import List
import warnings
from collections import deque, namedtuple

Expand Down Expand Up @@ -194,6 +195,26 @@ def __init__(self, args, tgt_dict):
self.decoder_opts, self.lm, self.silence, self.blank, []
)

def get_timesteps(self, token_idxs: List[int]) -> List[int]:
"""Returns frame numbers corresponding to every non-blank token.

Parameters
----------
token_idxs : List[int]
IDs of decoded tokens.

Returns
-------
List[int]
Frame numbers corresponding to every non-blank token.
"""
timesteps = []
for i, token_idx in enumerate(token_idxs):
if token_idx == self.blank:
continue
if i == 0 or token_idx != token_idxs[i-1]:
timesteps.append(i)
return timesteps

def decode(self, emissions):
B, T, N = emissions.size()
Expand All @@ -208,6 +229,7 @@ def decode(self, emissions):
{
"tokens": self.get_tokens(result.tokens),
"score": result.score,
"timesteps": self.get_timesteps(result.tokens),
"words": [
self.word_dict.get_entry(x) for x in result.words if x >= 0
],
Expand Down