Microsoft Wav2Lm Pre-trained Model TDNN Code for Speaker Recognition

Microsoft Wav2Lm is a pre-trained model that utilizes Time Delay Neural Networks (TDNN) for speaker recognition. Below is the code for this model:

import torch
import torch.nn as nn

class TDNN(nn.Module):
    def __init__(self, input_dim, output_dim, context_size, stride=1, dilation=1):
        super(TDNN, self).__init__()

        self.context_size = context_size
        self.stride = stride
        self.dilation = dilation
        self.num_filters = output_dim
        self.input_dim = input_dim

        self.kernel = nn.Linear(input_dim*context_size, output_dim)
        self.init_weights()

    def init_weights(self):
        nn.init.kaiming_normal_(self.kernel.weight.data)

    def forward(self, x):
        '''
        input: size (batch, input_features, seq_len)
        output: size (batch, output_dim, new_seq_len)
        '''
        x = x.transpose(1,2) # batch_size * seq_len * input_dim
        n_frames = x.size(1)
        out_frames = (n_frames - self.context_size*self.dilation) // self.stride + 1
        x = x.unfold(1, self.context_size, self.stride*self.dilation).contiguous()
        x = x.view(-1, self.context_size*self.input_dim)
        x = self.kernel(x)
        x = x.view(-1, out_frames, self.num_filters).transpose(1,2).contiguous()
        return x

This code defines the TDNN class, implementing the TDNN model. The input_dim argument specifies the input dimensionality, output_dim the output dimensionality, context_size the context window size, stride the context window stride, and dilation the dilation rate of the context window.

To utilize this model, instantiate a TDNN object and pass input data through it using the forward method.

Note that this is solely the code for the TDNN model within Microsoft Wav2Lm. Additional code is required for using this model for speaker recognition.

Microsoft Wav2Lm Pre-trained Model TDNN Code for Speaker Recognition

原文地址: https://www.cveoy.top/t/topic/lkuP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录