-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer.py
More file actions
63 lines (55 loc) · 2.87 KB
/
Copy pathinfer.py
File metadata and controls
63 lines (55 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
"""
This script loads a trained model checkpoint and runs inference on a provided audio file. It uses beam search decoding to generate the final transcription.
"""
import os
import argparse
import torch
import torchaudio
from utils.config import load_config
from utils.logger import get_logger
from utils.audio_processing import load_audio, compute_melspectrogram
from model.mixture_of_experts import MixtureOfExpertsModel
def beam_search_decode(model, features, config, beam_width=5, max_decode_length=200):
"""
A placeholder beam search decoder.
In practice, this function should incorporate language models and proper decoding.
"""
# For demonstration, we use greedy decoding as a simple beam search alternative.
model.eval()
with torch.no_grad():
outputs = model(features.unsqueeze(0)) # [1, T, vocab_size]
best_indices = torch.argmax(outputs, dim=-1).squeeze(0).tolist()
# Map indices to characters/words using a dummy vocabulary
# In production, load the appropriate vocabulary file based on language
vocab = {i: chr(96 + i) for i in range(1, 27)}
transcription = "".join([vocab.get(idx, "") for idx in best_indices if idx != 0])
return transcription
def main():
parser = argparse.ArgumentParser(description="Inference for Speech Recognition")
parser.add_argument("--config", type=str, default="config.yaml", help="Path to configuration file")
parser.add_argument("--audio_path", type=str, required=True, help="Path to the audio file for transcription")
parser.add_argument("--checkpoint", type=str, default="", help="Path to the model checkpoint")
args = parser.parse_args()
config = load_config(args.config)
device = torch.device(config["general"]["device"])
logger = get_logger(config["general"]["log_dir"])
# Initialize model and load checkpoint if provided
model = MixtureOfExpertsModel(config=config["model"]).to(device)
if args.checkpoint and os.path.isfile(args.checkpoint):
checkpoint = torch.load(args.checkpoint, map_location=device)
model.load_state_dict(checkpoint["model_state_dict"])
logger.info(f"Loaded checkpoint from {args.checkpoint}")
else:
logger.warning("No valid checkpoint provided. Using randomly initialized model.")
# Process the audio file
waveform, sample_rate = load_audio(args.audio_path, target_sample_rate=config["data"]["sample_rate"])
features = compute_melspectrogram(waveform, config["data"])
features = features.to(device)
# Perform beam search decoding
transcription = beam_search_decode(model, features, config["inference"],
beam_width=config["inference"]["beam_width"],
max_decode_length=config["inference"]["max_decode_length"])
print("Transcription:", transcription)
if __name__ == "__main__":
main()