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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# WeSpeaker ResNet34 Speaker Embedding Model (MLX)
|
| 2 |
+
|
| 3 |
+
This is an MLX port of the [pyannote/wespeaker-voxceleb-resnet34-LM](https://huggingface.co/pyannote/wespeaker-voxceleb-resnet34-LM) speaker embedding model from the WeSpeaker toolkit.
|
| 4 |
+
|
| 5 |
+
## Model Description
|
| 6 |
+
|
| 7 |
+
**ResNet34-based speaker embedding model** trained on VoxCeleb for speaker recognition and diarization tasks. This MLX implementation provides identical functionality to the PyTorch original, optimized for Apple Silicon.
|
| 8 |
+
|
| 9 |
+
- **Architecture**: ResNet34 with [3, 4, 6, 3] block configuration
|
| 10 |
+
- **Input**: Mel spectrogram (batch, time_frames, freq_bins=80)
|
| 11 |
+
- **Output**: 256-dimensional speaker embeddings
|
| 12 |
+
- **Parameters**: 6.6M
|
| 13 |
+
- **Model Size**: 25MB
|
| 14 |
+
|
| 15 |
+
## Performance
|
| 16 |
+
|
| 17 |
+
**Speaker Similarity Preservation** (vs PyTorch original):
|
| 18 |
+
- Max cosine similarity difference: **2.4%**
|
| 19 |
+
- Mean cosine similarity difference: **0.8%**
|
| 20 |
+
- Numerical accuracy: Max abs diff ~0.17
|
| 21 |
+
|
| 22 |
+
The model preserves speaker similarity relationships excellently, making it suitable for production speaker diarization and verification tasks.
|
| 23 |
+
|
| 24 |
+
## Installation
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
pip install mlx numpy
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Usage
|
| 31 |
+
|
| 32 |
+
```python
|
| 33 |
+
import mlx.core as mx
|
| 34 |
+
import mlx.nn as nn
|
| 35 |
+
import numpy as np
|
| 36 |
+
|
| 37 |
+
# Load model
|
| 38 |
+
from resnet_embedding import load_resnet34_embedding
|
| 39 |
+
|
| 40 |
+
model = load_resnet34_embedding("weights.npz")
|
| 41 |
+
|
| 42 |
+
# Prepare mel spectrogram input (batch, time, freq)
|
| 43 |
+
# Example: 150 time frames, 80 mel bins
|
| 44 |
+
mel_spectrogram = mx.array(np.random.randn(1, 150, 80).astype(np.float32))
|
| 45 |
+
|
| 46 |
+
# Extract speaker embedding
|
| 47 |
+
embedding = model(mel_spectrogram) # Shape: (1, 256)
|
| 48 |
+
|
| 49 |
+
print(f"Embedding shape: {embedding.shape}")
|
| 50 |
+
print(f"Embedding norm: {float(mx.linalg.norm(embedding)):.4f}")
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
### Computing Speaker Similarity
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
# Extract embeddings for two audio segments
|
| 57 |
+
embedding1 = model(mel_spec1) # (1, 256)
|
| 58 |
+
embedding2 = model(mel_spec2) # (1, 256)
|
| 59 |
+
|
| 60 |
+
# Compute cosine similarity
|
| 61 |
+
similarity = mx.sum(embedding1 * embedding2) / (
|
| 62 |
+
mx.linalg.norm(embedding1) * mx.linalg.norm(embedding2)
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
print(f"Speaker similarity: {float(similarity):.4f}")
|
| 66 |
+
# High similarity (>0.9) = same speaker
|
| 67 |
+
# Low similarity (<0.5) = different speakers
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
## Input Requirements
|
| 71 |
+
|
| 72 |
+
The model expects mel spectrogram features with:
|
| 73 |
+
- **Frequency bins**: 80 (mel filterbanks)
|
| 74 |
+
- **Time frames**: Variable length (e.g., 100-300 frames)
|
| 75 |
+
- **Format**: (batch_size, time_frames, freq_bins)
|
| 76 |
+
- **Data type**: float32
|
| 77 |
+
|
| 78 |
+
### Extracting Mel Spectrograms
|
| 79 |
+
|
| 80 |
+
You can use `pyannote.audio` for feature extraction:
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
from pyannote.audio import Model
|
| 84 |
+
import torch
|
| 85 |
+
|
| 86 |
+
# Load feature extractor from original model
|
| 87 |
+
pt_model = Model.from_pretrained("pyannote/wespeaker-voxceleb-resnet34-LM")
|
| 88 |
+
|
| 89 |
+
# Extract features
|
| 90 |
+
waveform = torch.randn(1, 16000) # 1 second at 16kHz
|
| 91 |
+
with torch.no_grad():
|
| 92 |
+
# Features are automatically extracted by the model
|
| 93 |
+
# You can access them via: pt_model.sincnet, pt_model.tdnn, etc.
|
| 94 |
+
pass
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
Or use `librosa`:
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
import librosa
|
| 101 |
+
import numpy as np
|
| 102 |
+
|
| 103 |
+
# Load audio
|
| 104 |
+
audio, sr = librosa.load("audio.wav", sr=16000)
|
| 105 |
+
|
| 106 |
+
# Extract mel spectrogram
|
| 107 |
+
mel_spec = librosa.feature.melspectrogram(
|
| 108 |
+
y=audio,
|
| 109 |
+
sr=sr,
|
| 110 |
+
n_fft=512,
|
| 111 |
+
hop_length=160, # 10ms at 16kHz
|
| 112 |
+
n_mels=80
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Convert to log scale
|
| 116 |
+
mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)
|
| 117 |
+
|
| 118 |
+
# Transpose to (time, freq) and add batch dimension
|
| 119 |
+
mel_spec_input = mel_spec_db.T[np.newaxis, :, :] # (1, time, 80)
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
## Model Architecture
|
| 123 |
+
|
| 124 |
+
```
|
| 125 |
+
Input: (batch, time, freq=80)
|
| 126 |
+
β Add channel dimension
|
| 127 |
+
(batch, time, freq, 1)
|
| 128 |
+
β Transpose to match PyTorch layout
|
| 129 |
+
(batch, freq, time, 1)
|
| 130 |
+
β Conv2d (1β32, 3x3, padding=1)
|
| 131 |
+
(batch, freq, time, 32)
|
| 132 |
+
β BatchNorm + ReLU
|
| 133 |
+
β ResNet Layer1 (3 blocks, 32 channels)
|
| 134 |
+
β ResNet Layer2 (4 blocks, 32β64, stride=2)
|
| 135 |
+
β ResNet Layer3 (6 blocks, 64β128, stride=2)
|
| 136 |
+
β ResNet Layer4 (3 blocks, 128β256, stride=2)
|
| 137 |
+
(batch, freq', time', 256)
|
| 138 |
+
β Temporal Statistics Pooling (mean + std over time)
|
| 139 |
+
(batch, 5120)
|
| 140 |
+
β Fully Connected (5120β256)
|
| 141 |
+
Output: (batch, 256) speaker embeddings
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
## Conversion Details
|
| 145 |
+
|
| 146 |
+
This model was converted from PyTorch to MLX with the following key fixes:
|
| 147 |
+
1. **Dimension ordering**: Transposed input to match PyTorch's (freq, time) layout
|
| 148 |
+
2. **BatchNorm**: Loaded running statistics and set model to eval mode
|
| 149 |
+
3. **No final normalization**: PyTorch model doesn't apply L2 normalization
|
| 150 |
+
4. **Weight format**: Conv2d weights transposed from (O,I,H,W) to (O,H,W,I)
|
| 151 |
+
|
| 152 |
+
## Limitations
|
| 153 |
+
|
| 154 |
+
- **Eval mode only**: Model uses frozen BatchNorm statistics (not suitable for fine-tuning without modifications)
|
| 155 |
+
- **Numerical precision**: Small differences from PyTorch (~0.17 max abs diff) due to implementation differences
|
| 156 |
+
- **Fixed architecture**: 80 mel bins required (model architecture is hardcoded for this)
|
| 157 |
+
|
| 158 |
+
## Applications
|
| 159 |
+
|
| 160 |
+
This model is suitable for:
|
| 161 |
+
- β
**Speaker diarization** (who spoke when)
|
| 162 |
+
- β
**Speaker verification** (is this the same speaker?)
|
| 163 |
+
- β
**Speaker identification** (which speaker is this?)
|
| 164 |
+
- β
**Voice biometrics**
|
| 165 |
+
- β
**Speaker clustering**
|
| 166 |
+
|
| 167 |
+
## Citation
|
| 168 |
+
|
| 169 |
+
Original model from WeSpeaker toolkit:
|
| 170 |
+
|
| 171 |
+
```bibtex
|
| 172 |
+
@inproceedings{wang2023wespeaker,
|
| 173 |
+
title={Wespeaker: A research and production oriented speaker embedding learning toolkit},
|
| 174 |
+
author={Wang, Hongji and Liang, Chengdong and Wang, Shuai and Chen, Zhengyang and Zhang, Binbin and Xiang, Xu and Deng, Yanlei and Qian, Yanmin},
|
| 175 |
+
booktitle={IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},
|
| 176 |
+
year={2023},
|
| 177 |
+
organization={IEEE}
|
| 178 |
+
}
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
Pyannote.audio implementation:
|
| 182 |
+
|
| 183 |
+
```bibtex
|
| 184 |
+
@inproceedings{Bredin2020,
|
| 185 |
+
title={pyannote.audio: neural building blocks for speaker diarization},
|
| 186 |
+
author={Herv{\'e} Bredin and Ruiqing Yin and Juan Manuel Coria and Gregory Gelly and Pavel Korshunov and Marvin Lavechin and Diego Fustes and Hadrien Titeux and Wassim Bouaziz and Marie-Philippe Gill},
|
| 187 |
+
booktitle={ICASSP 2020, IEEE International Conference on Acoustics, Speech, and Signal Processing},
|
| 188 |
+
year={2020},
|
| 189 |
+
}
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
## License
|
| 193 |
+
|
| 194 |
+
This model follows the same license as the original PyTorch model. Please check the [original model card](https://huggingface.co/pyannote/wespeaker-voxceleb-resnet34-LM) for license details.
|
| 195 |
+
|
| 196 |
+
## Conversion
|
| 197 |
+
|
| 198 |
+
Converted to MLX by the community. Original PyTorch model: [pyannote/wespeaker-voxceleb-resnet34-LM](https://huggingface.co/pyannote/wespeaker-voxceleb-resnet34-LM)
|
| 199 |
+
|
| 200 |
+
**Validation**: Speaker similarity preserved to within 2.4% of PyTorch implementation.
|