| """Small helpers used from HyperPyYAML files.""" |
|
|
| from __future__ import annotations |
|
|
| import json |
| from pathlib import Path |
|
|
|
|
| def read_output_neurons(metadata_path: str) -> int: |
| """Read output_neurons from a metadata JSON file.""" |
| path = Path(metadata_path) |
| with path.open("r", encoding="utf-8") as f: |
| metadata = json.load(f) |
| return int(metadata["output_neurons"]) |
|
|
|
|
| def read_subphonetic_topology_output_neurons( |
| label_encoder_path: str, |
| context_phone_mode: str = "mono", |
| topology: str = "three_state_skip_two", |
| split_silence: bool = False, |
| ) -> int: |
| """Compute output size for an auxiliary subphonetic topology head.""" |
| from utils.subphonetic_topology import topology_output_neurons |
|
|
| return topology_output_neurons( |
| label_encoder_path=label_encoder_path, |
| context_phone_mode=context_phone_mode, |
| topology=topology, |
| split_silence=split_silence, |
| ) |
|
|
|
|
| def sum_ints(*values) -> int: |
| """Return the sum of integer-like HyperPyYAML values.""" |
| return sum(int(value) for value in values if value is not None) |
|
|
|
|
| def load_ctc_text_encoder_from_file(path: str): |
| """Construct and load a SpeechBrain CTCTextEncoder from a text file.""" |
| import speechbrain as sb |
|
|
| encoder = sb.dataio.encoder.CTCTextEncoder() |
| encoder.load(path) |
| return encoder |
|
|