Views
No views yet
loss = max(sim(anchor, negative) - sim(anchor, positive) + margin, 0)sim(anchor, positive) > sim(anchor, negative) by at least the margin (0.3).1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4import torchvision.models as models
5
6class ResNetSpeaker(nn.Module):
7 def __init__(self, embedding_dim=256):
8 super().__init__()
9 self.backbone = models.resnet34(weights=None)
10 self.backbone.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
11 in_features = self.backbone.fc.in_features
12 self.backbone.fc = nn.Identity()
13 self.embedding = nn.Linear(in_features, embedding_dim)
14
15 def forward(self, x):
16 features = self.backbone(x)
17 embedding = self.embedding(features)
18 return F.normalize(embedding, p=2, dim=1)
19
20# Load the model
21model = ResNetSpeaker(embedding_dim=256)
22checkpoint = torch.load("checkpoint_resnet34_cosine_triplet.pth")
23model.load_state_dict(checkpoint["model_state"])
24model.eval()1import soundfile as sf
2import torch
3import torchaudio.transforms as T
4import numpy as np
5
6TARGET_SR = 16000
7TARGET_LENGTH = TARGET_SR * 5 # 5 seconds
8
9def crop_or_pad(audio):
10 length = len(audio)
11 if length > TARGET_LENGTH:
12 start = (length - TARGET_LENGTH) // 2
13 audio = audio[start:start + TARGET_LENGTH]
14 elif length < TARGET_LENGTH:
15 audio = np.pad(audio, (0, TARGET_LENGTH - length), mode='constant')
16 return audio
17
18mel_transform = T.MelSpectrogram(sample_rate=16000, n_fft=400, hop_length=160, n_mels=80)
19amplitude_to_db = T.AmplitudeToDB()
20
21def load_audio(path):
22 audio, sr = sf.read(path)
23 if len(audio.shape) > 1:
24 audio = np.mean(audio, axis=1)
25 audio = crop_or_pad(audio)
26 audio = torch.tensor(audio).float().unsqueeze(0)
27 return amplitude_to_db(mel_transform(audio))1# Compare two audio files
2audio1 = load_audio("speaker1.wav").unsqueeze(0)
3audio2 = load_audio("speaker2.wav").unsqueeze(0)
4
5with torch.no_grad():
6 emb1 = model(audio1)
7 emb2 = model(audio2)
8 similarity = F.cosine_similarity(emb1, emb2)
9
10 # Decision threshold: 0.5
11 same_speaker = similarity.item() > 0.5
12 print(f"Similarity: {similarity.item():.4f}")
13 print(f"Same speaker: {same_speaker}")ML Project ResNet34 Triplet Loss.ipynbtorch
torchaudio
torchvision
soundfile
numpy
pandas
tqdm
matplotlib1@misc{resnet34-speaker-verification,
2 author = {Your Name},
3 title = {Speaker Verification with ResNet34 and Cosine Triplet Loss},
4 year = {2026},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/YOUR_USERNAME/YOUR_MODEL_NAME}}
7}