File size: 10,921 Bytes
adde09b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""High-level Speaker Encoder API.

Provides a unified interface for extracting speaker embeddings from audio
files using either a PyTorch checkpoint or an ONNX model.

Usage (ONNX -- recommended for inference):
    encoder = SpeakerEncoder.from_onnx("speaker_encoder.onnx")
    emb = encoder.encode("audio.wav")

Usage (PyTorch):
    encoder = SpeakerEncoder.from_pytorch("speaker_encoder.ckpt")
    emb = encoder.encode("audio.wav")
"""

from __future__ import annotations

import logging
from pathlib import Path
from typing import TYPE_CHECKING

import numpy as np

from .audio_utils import (
    DEFAULT_FMAX,
    DEFAULT_FMIN,
    DEFAULT_HOP_LENGTH,
    DEFAULT_N_FFT,
    DEFAULT_N_MELS,
    DEFAULT_SR,
    compute_mel_spectrogram,
    load_audio,
    normalize_audio,
)


if TYPE_CHECKING:
    import onnxruntime
    import torch

_LOGGER = logging.getLogger(__name__)


def _infer_hparams(state_dict: dict) -> dict:
    """Infer ECAPA-TDNN hyperparameters from a state_dict.

    Examines key tensor shapes to determine input_dim, channels, emb_dim,
    and scale so that the model can be reconstructed without explicit
    configuration.

    Args:
        state_dict: Model state dictionary.

    Returns:
        Dict of keyword arguments for :class:`ECAPATDNN`.
    """
    # layer1.0.weight has shape (channels, input_dim, kernel_size)
    layer1_weight = state_dict.get("layer1.0.weight")
    if layer1_weight is None:
        raise ValueError(
            "Cannot infer hparams: 'layer1.0.weight' not found in state_dict"
        )

    channels = layer1_weight.shape[0]
    input_dim = layer1_weight.shape[1]

    # fc.weight has shape (emb_dim, channels * 2)
    fc_weight = state_dict.get("fc.weight")
    if fc_weight is None:
        raise ValueError("Cannot infer hparams: 'fc.weight' not found in state_dict")
    emb_dim = fc_weight.shape[0]

    # Infer Res2Net scale from the number of conv modules in layer2.res2net.convs
    # convs has (scale - 1) entries: convs.0, convs.1, ..., convs.(scale-2)
    scale_minus_1 = 0
    for key in state_dict:
        if key.startswith("layer2.res2net.convs.") and key.endswith(".weight"):
            scale_minus_1 += 1
    scale = scale_minus_1 + 1 if scale_minus_1 > 0 else 8

    # Infer SE bottleneck from layer2.se.se.1.weight shape (bottleneck, channels, 1)
    se_weight = state_dict.get("layer2.se.se.1.weight")
    se_bottleneck = se_weight.shape[0] if se_weight is not None else 128

    return {
        "input_dim": input_dim,
        "channels": channels,
        "emb_dim": emb_dim,
        "scale": scale,
        "se_bottleneck": se_bottleneck,
    }


class SpeakerEncoder:
    """Speaker Encoder high-level API.

    Loads a PyTorch or ONNX speaker encoder model and provides methods
    to extract 256-dimensional speaker embeddings from audio files.

    Do not instantiate directly; use :meth:`from_pytorch` or :meth:`from_onnx`.
    """

    def __init__(self) -> None:
        self._mode: str = "none"  # "pytorch" or "onnx"
        self._pytorch_model: torch.nn.Module | None = None
        self._pytorch_device: str = "cpu"
        self._onnx_session: onnxruntime.InferenceSession | None = None

    @classmethod
    def from_pytorch(
        cls,
        checkpoint_path: str | Path,
        device: str = "cpu",
    ) -> SpeakerEncoder:
        """Load a speaker encoder from a PyTorch checkpoint.

        The checkpoint should contain either:
          - A raw state_dict (keys like ``layer1.0.weight``), or
          - A dict with a ``"model_state_dict"`` key.

        Args:
            checkpoint_path: Path to the ``.ckpt`` or ``.pt`` file.
            device: Torch device string (default: ``"cpu"``).

        Returns:
            Configured :class:`SpeakerEncoder` instance.
        """
        import torch  # noqa: PLC0415

        from .ecapa_tdnn import ECAPATDNN  # noqa: PLC0415

        checkpoint_path = Path(checkpoint_path)
        if not checkpoint_path.exists():
            raise FileNotFoundError(f"Checkpoint not found: {checkpoint_path}")

        ckpt = torch.load(str(checkpoint_path), map_location=device, weights_only=True)

        if isinstance(ckpt, dict) and "model_state_dict" in ckpt:
            state_dict = ckpt["model_state_dict"]
        elif isinstance(ckpt, dict) and all(isinstance(k, str) for k in ckpt.keys()):
            state_dict = ckpt
        else:
            raise ValueError(
                "Checkpoint format not recognised. Expected a state_dict or a "
                "dict with 'model_state_dict' key."
            )

        # Infer model hyperparameters from the state_dict shapes
        hparams = _infer_hparams(state_dict)
        model = ECAPATDNN(**hparams)
        model.load_state_dict(state_dict)
        model.eval()
        model.to(device)

        encoder = cls()
        encoder._mode = "pytorch"
        encoder._pytorch_model = model
        encoder._pytorch_device = device
        _LOGGER.info(
            "Loaded PyTorch speaker encoder from %s (device=%s)",
            checkpoint_path,
            device,
        )
        return encoder

    @classmethod
    def from_onnx(cls, onnx_path: str | Path) -> SpeakerEncoder:
        """Load a speaker encoder from an ONNX model.

        Uses the project's shared ORT session utilities for optimised
        session creation with caching support.

        Args:
            onnx_path: Path to the ``.onnx`` file.

        Returns:
            Configured :class:`SpeakerEncoder` instance.
        """
        onnx_path = Path(onnx_path)
        if not onnx_path.exists():
            raise FileNotFoundError(f"ONNX model not found: {onnx_path}")

        from ..ort_utils import create_session_with_cache  # noqa: PLC0415

        session = create_session_with_cache(onnx_path, device="cpu")

        encoder = cls()
        encoder._mode = "onnx"
        encoder._onnx_session = session
        _LOGGER.info("Loaded ONNX speaker encoder from %s", onnx_path)
        return encoder

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def encode(self, audio_path: str | Path) -> np.ndarray:
        """Extract a 256-dimensional speaker embedding from an audio file.

        The audio is loaded, peak-normalized, converted to a log-mel
        spectrogram, and passed through the encoder model.

        Args:
            audio_path: Path to an audio file (WAV, FLAC, OGG, etc.).

        Returns:
            1-D float32 array of shape ``(256,)``, L2-normalized.
        """
        mel = self._audio_to_mel(audio_path)
        return self._infer(mel)

    def encode_batch(self, audio_paths: list[str | Path]) -> np.ndarray:
        """Extract speaker embeddings for multiple audio files.

        All mel spectrograms are zero-padded to the longest in the batch
        so they can be processed in a single forward pass.

        Args:
            audio_paths: List of audio file paths.

        Returns:
            2-D float32 array of shape ``(len(audio_paths), 256)``.
        """
        if not audio_paths:
            return np.empty((0, 256), dtype=np.float32)

        mels = [self._audio_to_mel(p) for p in audio_paths]

        # Pad to uniform time length
        max_time = max(m.shape[1] for m in mels)
        padded = np.zeros((len(mels), mels[0].shape[0], max_time), dtype=np.float32)
        for i, m in enumerate(mels):
            padded[i, :, : m.shape[1]] = m

        return self._infer_batch(padded)

    @staticmethod
    def similarity(emb1: np.ndarray, emb2: np.ndarray) -> float:
        """Compute cosine similarity between two embeddings.

        Both embeddings should already be L2-normalized (as returned by
        :meth:`encode`), but this method re-normalizes for safety.

        Args:
            emb1: 1-D float32 array of shape ``(emb_dim,)``.
            emb2: 1-D float32 array of shape ``(emb_dim,)``.

        Returns:
            Cosine similarity in [-1, 1].
        """
        emb1 = emb1.flatten().astype(np.float64)
        emb2 = emb2.flatten().astype(np.float64)

        norm1 = np.linalg.norm(emb1)
        norm2 = np.linalg.norm(emb2)
        if norm1 < 1e-12 or norm2 < 1e-12:
            return 0.0

        return float(np.dot(emb1, emb2) / (norm1 * norm2))

    # ------------------------------------------------------------------
    # Internal helpers
    # ------------------------------------------------------------------

    def _audio_to_mel(self, audio_path: str | Path) -> np.ndarray:
        """Load audio and compute mel spectrogram.

        Returns:
            (n_mels, time) float32 array.
        """
        audio = load_audio(audio_path, sr=DEFAULT_SR)
        audio = normalize_audio(audio)
        mel = compute_mel_spectrogram(
            audio,
            sr=DEFAULT_SR,
            n_fft=DEFAULT_N_FFT,
            hop_length=DEFAULT_HOP_LENGTH,
            n_mels=DEFAULT_N_MELS,
            fmin=DEFAULT_FMIN,
            fmax=DEFAULT_FMAX,
        )
        return mel

    def _infer(self, mel: np.ndarray) -> np.ndarray:
        """Run inference on a single mel spectrogram.

        Args:
            mel: (n_mels, time) float32 array.

        Returns:
            1-D float32 array of shape ``(emb_dim,)``.
        """
        # Add batch dimension: (1, n_mels, time)
        mel_batch = mel[np.newaxis, :, :]
        return self._infer_batch(mel_batch)[0]

    def _infer_batch(self, mel_batch: np.ndarray) -> np.ndarray:
        """Run inference on a batch of mel spectrograms.

        Args:
            mel_batch: (batch, n_mels, time) float32 array.

        Returns:
            (batch, emb_dim) float32 array.
        """
        if self._mode == "pytorch":
            return self._infer_pytorch(mel_batch)
        elif self._mode == "onnx":
            return self._infer_onnx(mel_batch)
        else:
            raise RuntimeError(
                "SpeakerEncoder not initialised. Use from_pytorch() or from_onnx()."
            )

    def _infer_pytorch(self, mel_batch: np.ndarray) -> np.ndarray:
        """Run inference with the PyTorch model."""
        import torch  # noqa: PLC0415

        assert self._pytorch_model is not None

        tensor = torch.from_numpy(mel_batch).to(self._pytorch_device)
        with torch.no_grad():
            embedding = self._pytorch_model(tensor)

        return embedding.cpu().numpy()

    def _infer_onnx(self, mel_batch: np.ndarray) -> np.ndarray:
        """Run inference with the ONNX model."""
        assert self._onnx_session is not None

        input_name = self._onnx_session.get_inputs()[0].name
        output_name = self._onnx_session.get_outputs()[0].name

        result = self._onnx_session.run(
            [output_name],
            {input_name: mel_batch.astype(np.float32)},
        )
        return result[0]