File size: 6,378 Bytes
d67c04d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Speaker encoders β€” the "voice fingerprint" half of Vanta.



Two implementations behind one contract: (B, T) waveform at 16 kHz in, (B, 192)

embedding out.



  TrainedSpeakerEncoder  our own ECAPA-TDNN, trained from scratch by

                         scripts/train_speaker_encoder.py. This is what

                         production runs.

  SpeakerEncoder         speechbrain's VoxCeleb-trained ECAPA. Kept as the

                         baseline `compare_encoders.py` measures against, and

                         as the encoder for the inactive SepFormer backend.



Use `SpeakerEncoder.load(checkpoint=...)` rather than constructing either

directly β€” passing a checkpoint selects ours, omitting it selects the

pretrained one.



Both are frozen during TSE training: fine-tuning a speaker encoder while the

separator learns tends to destabilize the identity space, and we want the

fingerprint to stay recognizable.

"""

from __future__ import annotations

from pathlib import Path

import torch
import torch.nn as nn

from vanta.config import DATA_DIR

ECAPA_EMBED_DIM = 192


class SpeakerEncoder(nn.Module):
    def __init__(

        self,

        savedir: Path | None = None,

        freeze: bool = True,

        run_opts: dict | None = None,

    ):
        super().__init__()
        # Lazy import: loading speechbrain pulls in a lot; we only want it when
        # this class is actually instantiated.
        from speechbrain.inference.speaker import EncoderClassifier
        from speechbrain.utils.fetching import LocalStrategy

        savedir = savedir or (DATA_DIR / "_models" / "ecapa_voxceleb")
        savedir.mkdir(parents=True, exist_ok=True)
        # COPY instead of SYMLINK β€” Windows refuses symlinks without admin or
        # Developer Mode, so defaulting to COPY is portable.
        self.encoder = EncoderClassifier.from_hparams(
            source="speechbrain/spkrec-ecapa-voxceleb",
            savedir=str(savedir),
            run_opts=run_opts or {},
            local_strategy=LocalStrategy.COPY,
        )
        self.embed_dim = ECAPA_EMBED_DIM
        self.freeze = freeze
        if freeze:
            for p in self.encoder.parameters():
                p.requires_grad_(False)
            self.encoder.eval()

    def forward(self, wav: torch.Tensor) -> torch.Tensor:
        """wav: (B, T) at 16 kHz. Returns (B, 192) speaker embeddings."""
        if wav.dim() == 3:
            wav = wav.squeeze(1)
        # ECAPA expects (B, T). speechbrain returns (B, 1, 192) -> squeeze.
        if self.freeze:
            with torch.no_grad():
                emb = self.encoder.encode_batch(wav)
        else:
            emb = self.encoder.encode_batch(wav)
        return emb.squeeze(1)

    def train(self, mode: bool = True):
        # If frozen, keep batchnorm/running stats in eval regardless of parent.
        super().train(mode)
        if self.freeze:
            self.encoder.eval()
        return self

    @staticmethod
    def load(

        checkpoint: "Path | str | None" = None,

        freeze: bool = True,

        run_opts: dict | None = None,

    ) -> nn.Module:
        """Return a speaker encoder: ours if a checkpoint is given, else pretrained.



        Both expose the same contract β€” (B, T) waveform at 16 kHz in, (B, 192)

        embedding out β€” so the separator is indifferent to which one it gets.

        Note that a separator trained against one encoder's embedding space is

        not automatically compatible with another's; see TrainedSpeakerEncoder.

        """
        if checkpoint is None:
            return SpeakerEncoder(freeze=freeze, run_opts=run_opts)
        return TrainedSpeakerEncoder(checkpoint, freeze=freeze)

    def _apply(self, fn, *args, **kwargs):
        # speechbrain's EncoderClassifier keeps its OWN `device` attribute and
        # moves incoming audio onto it. nn.Module.to()/.cpu()/.cuda() only move
        # the weights, so without this the two drift apart and you get
        # "Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor)
        # should be the same" β€” which breaks CPU-only deployments.
        out = super()._apply(fn, *args, **kwargs)
        try:
            dev = next(self.encoder.parameters()).device
            self.encoder.device = dev
            if hasattr(self.encoder, "mods"):
                self.encoder.mods.to(dev)
        except StopIteration:
            pass
        return out


class TrainedSpeakerEncoder(nn.Module):
    """Our own ECAPA-TDNN, trained from scratch (scripts/train_speaker_encoder.py).



    Drop-in replacement for the pretrained wrapper above: same (B, T) -> (B, 192)

    contract, so `Vanta` can take either.



    Compatibility caveat, and it matters: a separator learns to read the specific

    embedding space it trained against. Both encoders emit 192 numbers, but the

    *meaning* of those numbers differs β€” swapping encoders under a separator that

    never saw this one is a domain shift, not a like-for-like exchange. Expect to

    re-train (or at least fine-tune) the separator after switching.

    """

    def __init__(self, checkpoint: "Path | str", freeze: bool = True):
        super().__init__()
        from vanta.models.ecapa_tdnn import EcapaTdnn

        ck = torch.load(str(checkpoint), map_location="cpu", weights_only=False)
        self.encoder = EcapaTdnn(
            embed_dim=ck.get("embed_dim", ECAPA_EMBED_DIM),
            channels=ck.get("channels", 512),
        )
        self.encoder.load_state_dict(ck["model_state"])
        self.embed_dim = ck.get("embed_dim", ECAPA_EMBED_DIM)
        del ck
        import gc
        gc.collect()
        self.freeze = freeze
        if freeze:
            for p in self.encoder.parameters():
                p.requires_grad_(False)
            self.encoder.eval()

    def forward(self, wav: torch.Tensor) -> torch.Tensor:
        if wav.dim() == 3:
            wav = wav.squeeze(1)
        if self.freeze:
            with torch.no_grad():
                return self.encoder(wav)
        return self.encoder(wav)

    def train(self, mode: bool = True):
        super().train(mode)
        if self.freeze:
            self.encoder.eval()
        return self