@@ -59,29 +59,29 @@ def align_with_transcript(
5959 with torch.no_grad():
6060 logits = model(inputs.input_values).logits.cpu()[0 ]
6161 probs = torch.nn.functional.softmax(logits,dim = - 1 )
62-
62+
6363 # Tokenize transcripts
6464 vocab = tokenizer.get_vocab()
6565 inv_vocab = {v:k for k,v in vocab.items()}
6666 unk_id = vocab[" <unk>" ]
67-
67+
6868 tokens = []
6969 for transcript in transcripts:
7070 assert len (transcript) > 0
7171 tok_ids = tokenizer(transcript.replace(" \n " ," " ).lower())[' input_ids' ]
7272 tok_ids = np.array(tok_ids,dtype = np.int)
7373 tokens.append(tok_ids[tok_ids != unk_id])
74-
74+
7575 # Align
7676 char_list = [inv_vocab[i] for i in range (len (inv_vocab))]
7777 config = ctc_segmentation.CtcSegmentationParameters(char_list = char_list)
7878 config.index_duration = audio.shape[0 ] / probs.size()[0 ] / samplerate
79-
79+
8080 ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_token_list(config, tokens)
8181 timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
8282 segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, transcripts)
8383 return [{" text" : t, " start" : p[0 ], " end" : p[1 ], " conf" : p[2 ]} for t,p in zip (transcripts, segments)]
84-
84+
8585def get_word_timestamps (
8686 audio : np.ndarray,
8787 samplerate : int = SAMPLERATE ,
@@ -95,38 +95,38 @@ def get_word_timestamps(
9595 with torch.no_grad():
9696 logits = model(inputs.input_values).logits.cpu()[0 ]
9797 probs = torch.nn.functional.softmax(logits,dim = - 1 )
98-
98+
9999 predicted_ids = torch.argmax(logits, dim = - 1 )
100100 pred_transcript = processor.decode(predicted_ids)
101-
101+
102102 # Split the transcription into words
103103 words = pred_transcript.split(" " )
104-
104+
105105 # Align
106106 vocab = tokenizer.get_vocab()
107107 inv_vocab = {v:k for k,v in vocab.items()}
108108 char_list = [inv_vocab[i] for i in range (len (inv_vocab))]
109109 config = ctc_segmentation.CtcSegmentationParameters(char_list = char_list)
110110 config.index_duration = audio.shape[0 ] / probs.size()[0 ] / samplerate
111-
111+
112112 ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_text(config, words)
113113 timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
114114 segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, words)
115115 return [{" text" : w, " start" : p[0 ], " end" : p[1 ], " conf" : p[2 ]} for w,p in zip (words, segments)]
116116
117117print (align_with_transcript(audio,transcripts))
118- # [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
118+ # [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
119119# {'text': 'SIR I EXIST', 'start': 2.3260775862068965, 'end': 4.078771551724138, 'conf': 0.0}]
120120
121121print (get_word_timestamps(audio))
122- # [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123- # {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124- # {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125- # {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126- # {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127- # {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128- # {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129- # {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
122+ # [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123+ # {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124+ # {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125+ # {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126+ # {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127+ # {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128+ # {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129+ # {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
130130# {'text': 'exist', 'start': 3.347198275862069, 'end': 4.078771551724138, 'conf': 0.0}]
131131```
132132
@@ -229,7 +229,7 @@ For examples, see the `prepare_*` functions in `ctc_segmentation.py`, or the exa
229229
230230### Segments clean-up
231231
232- Segments that were written to a ` segments ` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
232+ Segments that were written to a ` segments ` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
233233
234234Utterances with a low confidence score are discarded in a data clean-up. This parameter may need adjustment depending on dataset, ASR model and used text conversion.
235235
0 commit comments