Downloads · 30 days
0
tahmidulomi/resnet34-speaker-verification-triplet-loss
resnet34-speaker-verification-triplet-loss is a machine learning model from tahmidulomi. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
A deep learning model for speaker verification using ResNet34 architecture trained with Cosine Triplet Loss on the LibriSpeech dataset.
Downloads · 30 days
0
Access
Public
Updated Apr 3, 2026
Repo size
257 MB
Likes
0
Public
Click a slice to open those files.
Other257 MB · 100%
From the Hugging Face model README
A deep learning model for speaker verification using ResNet34 architecture trained with Cosine Triplet Loss on the LibriSpeech dataset.
This model performs speaker verification by learning speaker embeddings using a triplet loss approach with cosine similarity.
The Cosine Triplet Loss formula:
loss = max(sim(anchor, negative) - sim(anchor, positive) + margin, 0)
The model learns to make sim(anchor, positive) > sim(anchor, negative) by at least the margin (0.3).
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class ResNetSpeaker(nn.Module):
def __init__(self, embedding_dim=256):
super().__init__()
self.backbone = models.resnet34(weights=None)
self.backbone.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
in_features = self.backbone.fc.in_features
self.backbone.fc = nn.Identity()
self.embedding = nn.Linear(in_features, embedding_dim)
def forward(self, x):
features = self.backbone(x)
embedding = self.embedding(features)
return F.normalize(embedding, p=2, dim=1)
# Load the model
model = ResNetSpeaker(embedding_dim=256)
checkpoint = torch.load("checkpoint_resnet34_cosine_triplet.pth")
model.load_state_dict(checkpoint["model_state"])
model.eval()
import soundfile as sf
import torch
import torchaudio.transforms as T
import numpy as np
TARGET_SR = 16000
TARGET_LENGTH = TARGET_SR * 5 # 5 seconds
def crop_or_pad(audio):
length = len(audio)
if length > TARGET_LENGTH:
start = (length - TARGET_LENGTH) // 2
audio = audio[start:start + TARGET_LENGTH]
elif length < TARGET_LENGTH:
audio = np.pad(audio, (0, TARGET_LENGTH - length), mode='constant')
return audio
mel_transform = T.MelSpectrogram(sample_rate=16000, n_fft=400, hop_length=160, n_mels=80)
amplitude_to_db = T.AmplitudeToDB()
def load_audio(path):
audio, sr = sf.read(path)
if len(audio.shape) > 1:
audio = np.mean(audio, axis=1)
audio = crop_or_pad(audio)
audio = torch.tensor(audio).float().unsqueeze(0)
return amplitude_to_db(mel_transform(audio))
# Compare two audio files
audio1 = load_audio("speaker1.wav").unsqueeze(0)
audio2 = load_audio("speaker2.wav").unsqueeze(0)
with torch.no_grad():
emb1 = model(audio1)
emb2 = model(audio2)
similarity = F.cosine_similarity(emb1, emb2)
# Decision threshold: 0.5
same_speaker = similarity.item() > 0.5
print(f"Similarity: {similarity.item():.4f}")
print(f"Same speaker: {same_speaker}")
The complete training notebook is included in this repository: ML Project ResNet34 Triplet Loss.ipynb
torch
torchaudio
torchvision
soundfile
numpy
pandas
tqdm
matplotlib
If you use this model, please cite:
@misc{resnet34-speaker-verification,
author = {Your Name},
title = {Speaker Verification with ResNet34 and Cosine Triplet Loss},
year = {2026},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/YOUR_USERNAME/YOUR_MODEL_NAME}}
}
[Specify your license here, e.g., MIT, Apache 2.0, etc.]