Views
No views yet
pip install mlx numpy1import mlx.core as mx
2import mlx.nn as nn
3import numpy as np
4
5# Load model
6from resnet_embedding import load_resnet34_embedding
7
8model = load_resnet34_embedding("weights.npz")
9
10# Prepare mel spectrogram input (batch, time, freq)
11# Example: 150 time frames, 80 mel bins
12mel_spectrogram = mx.array(np.random.randn(1, 150, 80).astype(np.float32))
13
14# Extract speaker embedding
15embedding = model(mel_spectrogram) # Shape: (1, 256)
16
17print(f"Embedding shape: {embedding.shape}")
18print(f"Embedding norm: {float(mx.linalg.norm(embedding)):.4f}")1# Extract embeddings for two audio segments
2embedding1 = model(mel_spec1) # (1, 256)
3embedding2 = model(mel_spec2) # (1, 256)
4
5# Compute cosine similarity
6similarity = mx.sum(embedding1 * embedding2) / (
7 mx.linalg.norm(embedding1) * mx.linalg.norm(embedding2)
8)
9
10print(f"Speaker similarity: {float(similarity):.4f}")
11# High similarity (>0.9) = same speaker
12# Low similarity (<0.5) = different speakerspyannote.audio for feature extraction:1from pyannote.audio import Model
2import torch
3
4# Load feature extractor from original model
5pt_model = Model.from_pretrained("pyannote/wespeaker-voxceleb-resnet34-LM")
6
7# Extract features
8waveform = torch.randn(1, 16000) # 1 second at 16kHz
9with torch.no_grad():
10 # Features are automatically extracted by the model
11 # You can access them via: pt_model.sincnet, pt_model.tdnn, etc.
12 passlibrosa:1import librosa
2import numpy as np
3
4# Load audio
5audio, sr = librosa.load("audio.wav", sr=16000)
6
7# Extract mel spectrogram
8mel_spec = librosa.feature.melspectrogram(
9 y=audio,
10 sr=sr,
11 n_fft=512,
12 hop_length=160, # 10ms at 16kHz
13 n_mels=80
14)
15
16# Convert to log scale
17mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)
18
19# Transpose to (time, freq) and add batch dimension
20mel_spec_input = mel_spec_db.T[np.newaxis, :, :] # (1, time, 80)Input: (batch, time, freq=80)
↓ Add channel dimension
(batch, time, freq, 1)
↓ Transpose to match PyTorch layout
(batch, freq, time, 1)
↓ Conv2d (1→32, 3x3, padding=1)
(batch, freq, time, 32)
↓ BatchNorm + ReLU
↓ ResNet Layer1 (3 blocks, 32 channels)
↓ ResNet Layer2 (4 blocks, 32→64, stride=2)
↓ ResNet Layer3 (6 blocks, 64→128, stride=2)
↓ ResNet Layer4 (3 blocks, 128→256, stride=2)
(batch, freq', time', 256)
↓ Temporal Statistics Pooling (mean + std over time)
(batch, 5120)
↓ Fully Connected (5120→256)
Output: (batch, 256) speaker embeddings1@inproceedings{wang2023wespeaker,
2 title={Wespeaker: A research and production oriented speaker embedding learning toolkit},
3 author={Wang, Hongji and Liang, Chengdong and Wang, Shuai and Chen, Zhengyang and Zhang, Binbin and Xiang, Xu and Deng, Yanlei and Qian, Yanmin},
4 booktitle={IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP)},
5 year={2023},
6 organization={IEEE}
7}1@inproceedings{Bredin2020,
2 title={pyannote.audio: neural building blocks for speaker diarization},
3 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},
4 booktitle={ICASSP 2020, IEEE International Conference on Acoustics, Speech, and Signal Processing},
5 year={2020},
6}