Skip to content

Commit 530b2e3

Browse files
author
可渊
committed
add support for timestamp
1 parent de00f2b commit 530b2e3

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Online Demo:
4444

4545
<a name="What's News"></a>
4646
# What's New 🔥
47+
- 2024/11: Add support for timestamp based on the CTC alignment.
4748
- 2024/7: Added Export Features for [ONNX](./demo_onnx.py) and [libtorch](./demo_libtorch.py), as well as Python Version Runtimes: [funasr-onnx-0.4.0](https://pypi.org/project/funasr-onnx/), [funasr-torch-0.1.1](https://pypi.org/project/funasr-torch/)
4849
- 2024/7: The [SenseVoice-Small](https://www.modelscope.cn/models/iic/SenseVoiceSmall) voice understanding model is open-sourced, which offers high-precision multilingual speech recognition, emotion recognition, and audio event detection capabilities for Mandarin, Cantonese, English, Japanese, and Korean and leads to exceptionally low inference latency.
4950
- 2024/7: The CosyVoice for natural speech generation with multi-language, timbre, and emotion control. CosyVoice excels in multi-lingual voice generation, zero-shot voice generation, cross-lingual voice cloning, and instruction-following capabilities. [CosyVoice repo](https://github.com/FunAudioLLM/CosyVoice) and [CosyVoice space](https://www.modelscope.cn/studios/iic/CosyVoice-300M).

demo2.py

+14
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@
2121

2222
text = rich_transcription_postprocess(res[0][0]["text"])
2323
print(text)
24+
25+
res = m.inference(
26+
data_in=f"{kwargs['model_path']}/example/en.mp3",
27+
language="auto", # "zh", "en", "yue", "ja", "ko", "nospeech"
28+
use_itn=False,
29+
ban_emo_unk=False,
30+
output_timestamp=True,
31+
**kwargs,
32+
)
33+
34+
timestamp = res[0][0]["timestamp"]
35+
text = rich_transcription_postprocess(res[0][0]["text"])
36+
print(text)
37+
print(timestamp)

model.py

+39-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from funasr.losses.label_smoothing_loss import LabelSmoothingLoss
1414
from funasr.metrics.compute_acc import compute_accuracy, th_accuracy
1515
from funasr.utils.load_utils import load_audio_text_image_video, extract_fbank
16-
16+
from utils.ctc_alignment import ctc_forced_align
1717

1818
class SinusoidalPositionEncoder(torch.nn.Module):
1919
""" """
@@ -830,6 +830,8 @@ def inference(
830830
).repeat(speech.size(0), 1, 1)
831831

832832
use_itn = kwargs.get("use_itn", False)
833+
output_timestamp = kwargs.get("output_timestamp", False)
834+
833835
textnorm = kwargs.get("text_norm", None)
834836
if textnorm is None:
835837
textnorm = "withitn" if use_itn else "woitn"
@@ -878,13 +880,45 @@ def inference(
878880

879881
# Change integer-ids to tokens
880882
text = tokenizer.decode(token_int)
881-
882-
result_i = {"key": key[i], "text": text}
883-
results.append(result_i)
884-
885883
if ibest_writer is not None:
886884
ibest_writer["text"][key[i]] = text
887885

886+
if output_timestamp:
887+
from itertools import groupby
888+
timestamp = []
889+
tokens = tokenizer.text2tokens(text)[4:]
890+
891+
logits_speech = self.ctc.softmax(encoder_out)[i, 4:encoder_out_lens[i].item(), :]
892+
893+
pred = logits_speech.argmax(-1).cpu()
894+
logits_speech[pred==self.blank_id, self.blank_id] = 0
895+
896+
align = ctc_forced_align(
897+
logits_speech.unsqueeze(0).float(),
898+
torch.Tensor(token_int[4:]).unsqueeze(0).long().to(logits_speech.device),
899+
(encoder_out_lens-4).long(),
900+
torch.tensor(len(token_int)-4).unsqueeze(0).long().to(logits_speech.device),
901+
ignore_id=self.ignore_id,
902+
)
903+
904+
pred = groupby(align[0, :encoder_out_lens[0]])
905+
_start = 0
906+
token_id = 0
907+
ts_max = encoder_out_lens[i] - 4
908+
for pred_token, pred_frame in pred:
909+
_end = _start + len(list(pred_frame))
910+
if pred_token != 0:
911+
ts_left = max((_start*60-30)/1000, 0)
912+
ts_right = min((_end*60-30)/1000, (ts_max*60-30)/1000)
913+
timestamp.append([tokens[token_id], ts_left, ts_right])
914+
token_id += 1
915+
_start = _end
916+
917+
result_i = {"key": key[i], "text": text, "timestamp": timestamp}
918+
results.append(result_i)
919+
else:
920+
result_i = {"key": key[i], "text": text}
921+
results.append(result_i)
888922
return results, meta_data
889923

890924
def export(self, **kwargs):

utils/ctc_alignment.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import torch
2+
3+
def ctc_forced_align(
4+
log_probs: torch.Tensor,
5+
targets: torch.Tensor,
6+
input_lengths: torch.Tensor,
7+
target_lengths: torch.Tensor,
8+
blank: int = 0,
9+
ignore_id: int = -1,
10+
) -> torch.Tensor:
11+
"""Align a CTC label sequence to an emission.
12+
13+
Args:
14+
log_probs (Tensor): log probability of CTC emission output.
15+
Tensor of shape `(B, T, C)`. where `B` is the batch size, `T` is the input length,
16+
`C` is the number of characters in alphabet including blank.
17+
targets (Tensor): Target sequence. Tensor of shape `(B, L)`,
18+
where `L` is the target length.
19+
input_lengths (Tensor):
20+
Lengths of the inputs (max value must each be <= `T`). 1-D Tensor of shape `(B,)`.
21+
target_lengths (Tensor):
22+
Lengths of the targets. 1-D Tensor of shape `(B,)`.
23+
blank_id (int, optional): The index of blank symbol in CTC emission. (Default: 0)
24+
ignore_id (int, optional): The index of ignore symbol in CTC emission. (Default: -1)
25+
"""
26+
targets[targets == ignore_id] = blank
27+
28+
batch_size, input_time_size, _ = log_probs.size()
29+
bsz_indices = torch.arange(batch_size, device=input_lengths.device)
30+
31+
_t_a_r_g_e_t_s_ = torch.cat(
32+
(
33+
torch.stack((torch.full_like(targets, blank), targets), dim=-1).flatten(start_dim=1),
34+
torch.full_like(targets[:, :1], blank),
35+
),
36+
dim=-1,
37+
)
38+
diff_labels = torch.cat(
39+
(
40+
torch.as_tensor([[False, False]], device=targets.device).expand(batch_size, -1),
41+
_t_a_r_g_e_t_s_[:, 2:] != _t_a_r_g_e_t_s_[:, :-2],
42+
),
43+
dim=1,
44+
)
45+
46+
neg_inf = torch.tensor(float("-inf"), device=log_probs.device, dtype=log_probs.dtype)
47+
padding_num = 2
48+
padded_t = padding_num + _t_a_r_g_e_t_s_.size(-1)
49+
best_score = torch.full((batch_size, padded_t), neg_inf, device=log_probs.device, dtype=log_probs.dtype)
50+
best_score[:, padding_num + 0] = log_probs[:, 0, blank]
51+
best_score[:, padding_num + 1] = log_probs[bsz_indices, 0, _t_a_r_g_e_t_s_[:, 1]]
52+
53+
backpointers = torch.zeros((batch_size, input_time_size, padded_t), device=log_probs.device, dtype=targets.dtype)
54+
55+
for t in range(1, input_time_size):
56+
prev = torch.stack(
57+
(best_score[:, 2:], best_score[:, 1:-1], torch.where(diff_labels, best_score[:, :-2], neg_inf))
58+
)
59+
prev_max_value, prev_max_idx = prev.max(dim=0)
60+
best_score[:, padding_num:] = log_probs[:, t].gather(-1, _t_a_r_g_e_t_s_) + prev_max_value
61+
backpointers[:, t, padding_num:] = prev_max_idx
62+
63+
l1l2 = best_score.gather(
64+
-1, torch.stack((padding_num + target_lengths * 2 - 1, padding_num + target_lengths * 2), dim=-1)
65+
)
66+
67+
path = torch.zeros((batch_size, input_time_size), device=best_score.device, dtype=torch.long)
68+
path[bsz_indices, input_lengths - 1] = padding_num + target_lengths * 2 - 1 + l1l2.argmax(dim=-1)
69+
70+
for t in range(input_time_size - 1, 0, -1):
71+
target_indices = path[:, t]
72+
prev_max_idx = backpointers[bsz_indices, t, target_indices]
73+
path[:, t - 1] += target_indices - prev_max_idx
74+
75+
alignments = _t_a_r_g_e_t_s_.gather(dim=-1, index=(path - padding_num).clamp(min=0))
76+
return alignments

0 commit comments

Comments
 (0)