Skip to content
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

Improve alignment accuracy by normalizing audio features #625

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion whisperx/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def align(
return_char_alignments: bool = False,
print_progress: bool = False,
combined_progress: bool = False,
preprocess: bool = True,
) -> AlignedTranscriptionResult:
"""
Align phoneme recognition predictions to known transcription.
Expand All @@ -120,6 +121,11 @@ def align(
model_lang = align_model_metadata["language"]
model_type = align_model_metadata["type"]

# Load align model Huggingface processor for audio feature extraction (Normalization)
if preprocess and model_type == 'huggingface':
processor = Wav2Vec2Processor.from_pretrained(
DEFAULT_ALIGN_MODELS_HF[model_lang])

# 1. Preprocess to keep only characters in dictionary
total_segments = len(transcript)
for sdx, segment in enumerate(transcript):
Expand Down Expand Up @@ -222,7 +228,11 @@ def align(
if model_type == "torchaudio":
emissions, _ = model(waveform_segment.to(device), lengths=lengths)
elif model_type == "huggingface":
emissions = model(waveform_segment.to(device)).logits
if preprocess:
inputs = processor(waveform_segment.squeeze(), sampling_rate=processor.sampling_rate, return_tensors="pt").to(device)
emissions = model(**inputs).logits
else:
emissions = model(waveform_segment.to(device)).logits
else:
raise NotImplementedError(f"Align model of type {model_type} not supported.")
emissions = torch.log_softmax(emissions, dim=-1)
Expand Down