Delete utils/speakerlab/models/rdino
Browse files
utils/speakerlab/models/rdino/ECAPA_TDNN.py
DELETED
|
@@ -1,465 +0,0 @@
|
|
| 1 |
-
# Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved.
|
| 2 |
-
# Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
| 3 |
-
|
| 4 |
-
""" This ECAPA-TDNN implementation is adapted from https://github.com/speechbrain/speechbrain.
|
| 5 |
-
"""
|
| 6 |
-
import math
|
| 7 |
-
import torch
|
| 8 |
-
import torch.nn as nn
|
| 9 |
-
import torch.nn.functional as F
|
| 10 |
-
|
| 11 |
-
def length_to_mask(length, max_len=None, dtype=None, device=None):
|
| 12 |
-
assert len(length.shape) == 1
|
| 13 |
-
|
| 14 |
-
if max_len is None:
|
| 15 |
-
max_len = length.max().long().item()
|
| 16 |
-
mask = torch.arange(
|
| 17 |
-
max_len, device=length.device, dtype=length.dtype).expand(
|
| 18 |
-
len(length), max_len) < length.unsqueeze(1)
|
| 19 |
-
|
| 20 |
-
if dtype is None:
|
| 21 |
-
dtype = length.dtype
|
| 22 |
-
|
| 23 |
-
if device is None:
|
| 24 |
-
device = length.device
|
| 25 |
-
|
| 26 |
-
mask = torch.as_tensor(mask, dtype=dtype, device=device)
|
| 27 |
-
return mask
|
| 28 |
-
|
| 29 |
-
def get_padding_elem(L_in: int, stride: int, kernel_size: int, dilation: int):
|
| 30 |
-
if stride > 1:
|
| 31 |
-
n_steps = math.ceil(((L_in - kernel_size * dilation) / stride) + 1)
|
| 32 |
-
L_out = stride * (n_steps - 1) + kernel_size * dilation
|
| 33 |
-
padding = [kernel_size // 2, kernel_size // 2]
|
| 34 |
-
|
| 35 |
-
else:
|
| 36 |
-
L_out = (L_in - dilation * (kernel_size - 1) - 1) // stride + 1
|
| 37 |
-
|
| 38 |
-
padding = [(L_in - L_out) // 2, (L_in - L_out) // 2]
|
| 39 |
-
return padding
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
class Conv1d(nn.Module):
|
| 43 |
-
|
| 44 |
-
def __init__(
|
| 45 |
-
self,
|
| 46 |
-
out_channels,
|
| 47 |
-
kernel_size,
|
| 48 |
-
in_channels,
|
| 49 |
-
stride=1,
|
| 50 |
-
dilation=1,
|
| 51 |
-
padding='same',
|
| 52 |
-
groups=1,
|
| 53 |
-
bias=True,
|
| 54 |
-
padding_mode='reflect',
|
| 55 |
-
):
|
| 56 |
-
super().__init__()
|
| 57 |
-
self.kernel_size = kernel_size
|
| 58 |
-
self.stride = stride
|
| 59 |
-
self.dilation = dilation
|
| 60 |
-
self.padding = padding
|
| 61 |
-
self.padding_mode = padding_mode
|
| 62 |
-
|
| 63 |
-
self.conv = nn.Conv1d(
|
| 64 |
-
in_channels,
|
| 65 |
-
out_channels,
|
| 66 |
-
self.kernel_size,
|
| 67 |
-
stride=self.stride,
|
| 68 |
-
dilation=self.dilation,
|
| 69 |
-
padding=0,
|
| 70 |
-
groups=groups,
|
| 71 |
-
bias=bias,
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
def forward(self, x):
|
| 75 |
-
if self.padding == 'same':
|
| 76 |
-
x = self._manage_padding(x, self.kernel_size, self.dilation,
|
| 77 |
-
self.stride)
|
| 78 |
-
|
| 79 |
-
elif self.padding == 'causal':
|
| 80 |
-
num_pad = (self.kernel_size - 1) * self.dilation
|
| 81 |
-
x = F.pad(x, (num_pad, 0))
|
| 82 |
-
|
| 83 |
-
elif self.padding == 'valid':
|
| 84 |
-
pass
|
| 85 |
-
|
| 86 |
-
else:
|
| 87 |
-
raise ValueError(
|
| 88 |
-
"Padding must be 'same', 'valid' or 'causal'. Got "
|
| 89 |
-
+ self.padding)
|
| 90 |
-
|
| 91 |
-
wx = self.conv(x)
|
| 92 |
-
|
| 93 |
-
return wx
|
| 94 |
-
|
| 95 |
-
def _manage_padding(
|
| 96 |
-
self,
|
| 97 |
-
x,
|
| 98 |
-
kernel_size: int,
|
| 99 |
-
dilation: int,
|
| 100 |
-
stride: int,
|
| 101 |
-
):
|
| 102 |
-
L_in = x.shape[-1]
|
| 103 |
-
padding = get_padding_elem(L_in, stride, kernel_size, dilation)
|
| 104 |
-
x = F.pad(x, padding, mode=self.padding_mode)
|
| 105 |
-
|
| 106 |
-
return x
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
class BatchNorm1d(nn.Module):
|
| 110 |
-
def __init__(
|
| 111 |
-
self,
|
| 112 |
-
input_size,
|
| 113 |
-
eps=1e-05,
|
| 114 |
-
momentum=0.1,
|
| 115 |
-
):
|
| 116 |
-
super().__init__()
|
| 117 |
-
self.norm = nn.BatchNorm1d(
|
| 118 |
-
input_size,
|
| 119 |
-
eps=eps,
|
| 120 |
-
momentum=momentum,
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
def forward(self, x):
|
| 124 |
-
return self.norm(x)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
class TDNNBlock(nn.Module):
|
| 128 |
-
"""An implementation of TDNN.
|
| 129 |
-
"""
|
| 130 |
-
def __init__(
|
| 131 |
-
self,
|
| 132 |
-
in_channels,
|
| 133 |
-
out_channels,
|
| 134 |
-
kernel_size,
|
| 135 |
-
dilation,
|
| 136 |
-
activation=nn.ReLU,
|
| 137 |
-
groups=1,
|
| 138 |
-
):
|
| 139 |
-
super(TDNNBlock, self).__init__()
|
| 140 |
-
self.conv = Conv1d(
|
| 141 |
-
in_channels=in_channels,
|
| 142 |
-
out_channels=out_channels,
|
| 143 |
-
kernel_size=kernel_size,
|
| 144 |
-
dilation=dilation,
|
| 145 |
-
groups=groups,
|
| 146 |
-
)
|
| 147 |
-
self.activation = activation()
|
| 148 |
-
self.norm = BatchNorm1d(input_size=out_channels)
|
| 149 |
-
|
| 150 |
-
def forward(self, x):
|
| 151 |
-
return self.norm(self.activation(self.conv(x)))
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
class Res2NetBlock(torch.nn.Module):
|
| 155 |
-
"""An implementation of Res2NetBlock w/ dilation.
|
| 156 |
-
"""
|
| 157 |
-
def __init__(
|
| 158 |
-
self, in_channels, out_channels, scale=8, kernel_size=3, dilation=1
|
| 159 |
-
):
|
| 160 |
-
super(Res2NetBlock, self).__init__()
|
| 161 |
-
assert in_channels % scale == 0
|
| 162 |
-
assert out_channels % scale == 0
|
| 163 |
-
|
| 164 |
-
in_channel = in_channels // scale
|
| 165 |
-
hidden_channel = out_channels // scale
|
| 166 |
-
|
| 167 |
-
self.blocks = nn.ModuleList(
|
| 168 |
-
[
|
| 169 |
-
TDNNBlock(
|
| 170 |
-
in_channel,
|
| 171 |
-
hidden_channel,
|
| 172 |
-
kernel_size=kernel_size,
|
| 173 |
-
dilation=dilation,
|
| 174 |
-
)
|
| 175 |
-
for i in range(scale - 1)
|
| 176 |
-
]
|
| 177 |
-
)
|
| 178 |
-
self.scale = scale
|
| 179 |
-
|
| 180 |
-
def forward(self, x):
|
| 181 |
-
y = []
|
| 182 |
-
for i, x_i in enumerate(torch.chunk(x, self.scale, dim=1)):
|
| 183 |
-
if i == 0:
|
| 184 |
-
y_i = x_i
|
| 185 |
-
elif i == 1:
|
| 186 |
-
y_i = self.blocks[i - 1](x_i)
|
| 187 |
-
else:
|
| 188 |
-
y_i = self.blocks[i - 1](x_i + y_i)
|
| 189 |
-
y.append(y_i)
|
| 190 |
-
y = torch.cat(y, dim=1)
|
| 191 |
-
return y
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
class SEBlock(nn.Module):
|
| 195 |
-
"""An implementation of squeeze-and-excitation block.
|
| 196 |
-
"""
|
| 197 |
-
def __init__(self, in_channels, se_channels, out_channels):
|
| 198 |
-
super(SEBlock, self).__init__()
|
| 199 |
-
|
| 200 |
-
self.conv1 = Conv1d(
|
| 201 |
-
in_channels=in_channels, out_channels=se_channels, kernel_size=1
|
| 202 |
-
)
|
| 203 |
-
self.relu = torch.nn.ReLU(inplace=True)
|
| 204 |
-
self.conv2 = Conv1d(
|
| 205 |
-
in_channels=se_channels, out_channels=out_channels, kernel_size=1
|
| 206 |
-
)
|
| 207 |
-
self.sigmoid = torch.nn.Sigmoid()
|
| 208 |
-
|
| 209 |
-
def forward(self, x, lengths=None):
|
| 210 |
-
L = x.shape[-1]
|
| 211 |
-
if lengths is not None:
|
| 212 |
-
mask = length_to_mask(lengths * L, max_len=L, device=x.device)
|
| 213 |
-
mask = mask.unsqueeze(1)
|
| 214 |
-
total = mask.sum(dim=2, keepdim=True)
|
| 215 |
-
s = (x * mask).sum(dim=2, keepdim=True) / total
|
| 216 |
-
else:
|
| 217 |
-
s = x.mean(dim=2, keepdim=True)
|
| 218 |
-
|
| 219 |
-
s = self.relu(self.conv1(s))
|
| 220 |
-
s = self.sigmoid(self.conv2(s))
|
| 221 |
-
|
| 222 |
-
return s * x
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
class AttentiveStatisticsPooling(nn.Module):
|
| 226 |
-
"""This class implements an attentive statistic pooling layer for each channel.
|
| 227 |
-
It returns the concatenated mean and std of the input tensor.
|
| 228 |
-
"""
|
| 229 |
-
def __init__(self, channels, attention_channels=128, global_context=True):
|
| 230 |
-
super().__init__()
|
| 231 |
-
|
| 232 |
-
self.eps = 1e-12
|
| 233 |
-
self.global_context = global_context
|
| 234 |
-
if global_context:
|
| 235 |
-
self.tdnn = TDNNBlock(channels * 3, attention_channels, 1, 1)
|
| 236 |
-
else:
|
| 237 |
-
self.tdnn = TDNNBlock(channels, attention_channels, 1, 1)
|
| 238 |
-
self.tanh = nn.Tanh()
|
| 239 |
-
self.conv = Conv1d(
|
| 240 |
-
in_channels=attention_channels, out_channels=channels, kernel_size=1
|
| 241 |
-
)
|
| 242 |
-
|
| 243 |
-
def forward(self, x, lengths=None):
|
| 244 |
-
"""Calculates mean and std for a batch (input tensor).
|
| 245 |
-
"""
|
| 246 |
-
L = x.shape[-1]
|
| 247 |
-
|
| 248 |
-
def _compute_statistics(x, m, dim=2, eps=self.eps):
|
| 249 |
-
mean = (m * x).sum(dim)
|
| 250 |
-
std = torch.sqrt(
|
| 251 |
-
(m * (x - mean.unsqueeze(dim)).pow(2)).sum(dim).clamp(eps)
|
| 252 |
-
)
|
| 253 |
-
return mean, std
|
| 254 |
-
|
| 255 |
-
if lengths is None:
|
| 256 |
-
lengths = torch.ones(x.shape[0], device=x.device)
|
| 257 |
-
|
| 258 |
-
# Make binary mask of shape [N, 1, L]
|
| 259 |
-
mask = length_to_mask(lengths * L, max_len=L, device=x.device)
|
| 260 |
-
mask = mask.unsqueeze(1)
|
| 261 |
-
|
| 262 |
-
# Expand the temporal context of the pooling layer by allowing the
|
| 263 |
-
# self-attention to look at global properties of the utterance.
|
| 264 |
-
if self.global_context:
|
| 265 |
-
# torch.std is unstable for backward computation
|
| 266 |
-
# https://github.com/pytorch/pytorch/issues/4320
|
| 267 |
-
total = mask.sum(dim=2, keepdim=True).float()
|
| 268 |
-
mean, std = _compute_statistics(x, mask / total)
|
| 269 |
-
mean = mean.unsqueeze(2).repeat(1, 1, L)
|
| 270 |
-
std = std.unsqueeze(2).repeat(1, 1, L)
|
| 271 |
-
attn = torch.cat([x, mean, std], dim=1)
|
| 272 |
-
else:
|
| 273 |
-
attn = x
|
| 274 |
-
|
| 275 |
-
# Apply layers
|
| 276 |
-
attn = self.conv(self.tanh(self.tdnn(attn)))
|
| 277 |
-
|
| 278 |
-
# Filter out zero-paddings
|
| 279 |
-
attn = attn.masked_fill(mask == 0, float("-inf"))
|
| 280 |
-
|
| 281 |
-
attn = F.softmax(attn, dim=2)
|
| 282 |
-
mean, std = _compute_statistics(x, attn)
|
| 283 |
-
# Append mean and std of the batch
|
| 284 |
-
pooled_stats = torch.cat((mean, std), dim=1)
|
| 285 |
-
pooled_stats = pooled_stats.unsqueeze(2)
|
| 286 |
-
|
| 287 |
-
return pooled_stats
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
class SERes2NetBlock(nn.Module):
|
| 291 |
-
"""An implementation of building block in ECAPA-TDNN, i.e.,
|
| 292 |
-
TDNN-Res2Net-TDNN-SEBlock.
|
| 293 |
-
"""
|
| 294 |
-
|
| 295 |
-
def __init__(
|
| 296 |
-
self,
|
| 297 |
-
in_channels,
|
| 298 |
-
out_channels,
|
| 299 |
-
res2net_scale=8,
|
| 300 |
-
se_channels=128,
|
| 301 |
-
kernel_size=1,
|
| 302 |
-
dilation=1,
|
| 303 |
-
activation=torch.nn.ReLU,
|
| 304 |
-
groups=1,
|
| 305 |
-
):
|
| 306 |
-
super().__init__()
|
| 307 |
-
self.out_channels = out_channels
|
| 308 |
-
self.tdnn1 = TDNNBlock(
|
| 309 |
-
in_channels,
|
| 310 |
-
out_channels,
|
| 311 |
-
kernel_size=1,
|
| 312 |
-
dilation=1,
|
| 313 |
-
activation=activation,
|
| 314 |
-
groups=groups,
|
| 315 |
-
)
|
| 316 |
-
self.res2net_block = Res2NetBlock(
|
| 317 |
-
out_channels, out_channels, res2net_scale, kernel_size, dilation
|
| 318 |
-
)
|
| 319 |
-
self.tdnn2 = TDNNBlock(
|
| 320 |
-
out_channels,
|
| 321 |
-
out_channels,
|
| 322 |
-
kernel_size=1,
|
| 323 |
-
dilation=1,
|
| 324 |
-
activation=activation,
|
| 325 |
-
groups=groups,
|
| 326 |
-
)
|
| 327 |
-
self.se_block = SEBlock(out_channels, se_channels, out_channels)
|
| 328 |
-
|
| 329 |
-
self.shortcut = None
|
| 330 |
-
if in_channels != out_channels:
|
| 331 |
-
self.shortcut = Conv1d(
|
| 332 |
-
in_channels=in_channels,
|
| 333 |
-
out_channels=out_channels,
|
| 334 |
-
kernel_size=1,
|
| 335 |
-
)
|
| 336 |
-
|
| 337 |
-
def forward(self, x, lengths=None):
|
| 338 |
-
residual = x
|
| 339 |
-
if self.shortcut:
|
| 340 |
-
residual = self.shortcut(x)
|
| 341 |
-
|
| 342 |
-
x = self.tdnn1(x)
|
| 343 |
-
x = self.res2net_block(x)
|
| 344 |
-
x = self.tdnn2(x)
|
| 345 |
-
x = self.se_block(x, lengths)
|
| 346 |
-
|
| 347 |
-
return x + residual
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
class ECAPA_TDNN(torch.nn.Module):
|
| 351 |
-
"""An implementation of the speaker embedding model in a paper.
|
| 352 |
-
"ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in
|
| 353 |
-
TDNN Based Speaker Verification" (https://arxiv.org/abs/2005.07143).
|
| 354 |
-
"""
|
| 355 |
-
|
| 356 |
-
def __init__(
|
| 357 |
-
self,
|
| 358 |
-
input_size,
|
| 359 |
-
device="cpu",
|
| 360 |
-
lin_neurons=512,
|
| 361 |
-
activation=torch.nn.ReLU,
|
| 362 |
-
channels=[512, 512, 512, 512, 1536],
|
| 363 |
-
kernel_sizes=[5, 3, 3, 3, 1],
|
| 364 |
-
dilations=[1, 2, 3, 4, 1],
|
| 365 |
-
attention_channels=128,
|
| 366 |
-
res2net_scale=8,
|
| 367 |
-
se_channels=128,
|
| 368 |
-
global_context=True,
|
| 369 |
-
groups=[1, 1, 1, 1, 1],
|
| 370 |
-
log_input=True,
|
| 371 |
-
n_mels=80,
|
| 372 |
-
):
|
| 373 |
-
|
| 374 |
-
super().__init__()
|
| 375 |
-
assert len(channels) == len(kernel_sizes)
|
| 376 |
-
assert len(channels) == len(dilations)
|
| 377 |
-
self.instancenorm = nn.InstanceNorm1d(n_mels)
|
| 378 |
-
self.n_mels = n_mels
|
| 379 |
-
self.log_input = log_input
|
| 380 |
-
self.channels = channels
|
| 381 |
-
self.blocks = nn.ModuleList()
|
| 382 |
-
|
| 383 |
-
# The initial TDNN layer
|
| 384 |
-
self.blocks.append(
|
| 385 |
-
TDNNBlock(
|
| 386 |
-
input_size,
|
| 387 |
-
channels[0],
|
| 388 |
-
kernel_sizes[0],
|
| 389 |
-
dilations[0],
|
| 390 |
-
activation,
|
| 391 |
-
groups[0],
|
| 392 |
-
)
|
| 393 |
-
)
|
| 394 |
-
|
| 395 |
-
# SE-Res2Net layers
|
| 396 |
-
for i in range(1, len(channels) - 1):
|
| 397 |
-
self.blocks.append(
|
| 398 |
-
SERes2NetBlock(
|
| 399 |
-
channels[i - 1],
|
| 400 |
-
channels[i],
|
| 401 |
-
res2net_scale=res2net_scale,
|
| 402 |
-
se_channels=se_channels,
|
| 403 |
-
kernel_size=kernel_sizes[i],
|
| 404 |
-
dilation=dilations[i],
|
| 405 |
-
activation=activation,
|
| 406 |
-
groups=groups[i],
|
| 407 |
-
)
|
| 408 |
-
)
|
| 409 |
-
|
| 410 |
-
# Multi-layer feature aggregation
|
| 411 |
-
self.mfa = TDNNBlock(
|
| 412 |
-
channels[-1],
|
| 413 |
-
channels[-1],
|
| 414 |
-
kernel_sizes[-1],
|
| 415 |
-
dilations[-1],
|
| 416 |
-
activation,
|
| 417 |
-
groups=groups[-1],
|
| 418 |
-
)
|
| 419 |
-
|
| 420 |
-
# Attentive Statistical Pooling
|
| 421 |
-
self.asp = AttentiveStatisticsPooling(
|
| 422 |
-
channels[-1],
|
| 423 |
-
attention_channels=attention_channels,
|
| 424 |
-
global_context=global_context,
|
| 425 |
-
)
|
| 426 |
-
self.asp_bn = BatchNorm1d(input_size=channels[-1] * 2)
|
| 427 |
-
|
| 428 |
-
# Final linear transformation
|
| 429 |
-
self.fc = Conv1d(
|
| 430 |
-
in_channels=channels[-1] * 2,
|
| 431 |
-
out_channels=lin_neurons,
|
| 432 |
-
kernel_size=1,
|
| 433 |
-
)
|
| 434 |
-
|
| 435 |
-
def forward(self, x, lengths=None):
|
| 436 |
-
"""Returns the embedding vector.
|
| 437 |
-
"""
|
| 438 |
-
x = x + 1e-6
|
| 439 |
-
if self.log_input:
|
| 440 |
-
x = x.log()
|
| 441 |
-
x = self.instancenorm(x).detach()
|
| 442 |
-
|
| 443 |
-
xl = []
|
| 444 |
-
for layer in self.blocks:
|
| 445 |
-
try:
|
| 446 |
-
x = layer(x, lengths=lengths)
|
| 447 |
-
except TypeError:
|
| 448 |
-
x = layer(x)
|
| 449 |
-
xl.append(x)
|
| 450 |
-
|
| 451 |
-
# Multi-layer feature aggregation
|
| 452 |
-
x = torch.cat(xl[1:], dim=1)
|
| 453 |
-
x = self.mfa(x)
|
| 454 |
-
|
| 455 |
-
# Attentive Statistical Pooling
|
| 456 |
-
x = self.asp(x, lengths=lengths)
|
| 457 |
-
x = self.asp_bn(x)
|
| 458 |
-
|
| 459 |
-
# Final linear transformation
|
| 460 |
-
x = self.fc(x)
|
| 461 |
-
|
| 462 |
-
x = x.transpose(1, 2)
|
| 463 |
-
x = x.squeeze(1)
|
| 464 |
-
|
| 465 |
-
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
# Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved.
|
| 2 |
-
# Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
import torch.nn.functional as F
|
| 7 |
-
|
| 8 |
-
from speakerlab.utils.utils_rdino import trunc_normal_
|
| 9 |
-
|
| 10 |
-
class RDINOHead(nn.Module):
|
| 11 |
-
def __init__(
|
| 12 |
-
self,
|
| 13 |
-
in_dim,
|
| 14 |
-
out_dim,
|
| 15 |
-
use_bn=False,
|
| 16 |
-
norm_last_layer=True,
|
| 17 |
-
nlayers=3,
|
| 18 |
-
hidden_dim=2048,
|
| 19 |
-
bottleneck_dim=256,
|
| 20 |
-
add_dim=8192,
|
| 21 |
-
):
|
| 22 |
-
super().__init__()
|
| 23 |
-
layers = [nn.Linear(in_dim, hidden_dim)]
|
| 24 |
-
if use_bn:
|
| 25 |
-
layers.append(nn.BatchNorm1d(hidden_dim))
|
| 26 |
-
layers.append(nn.GELU())
|
| 27 |
-
for _ in range(nlayers - 2):
|
| 28 |
-
layers.append(nn.Linear(hidden_dim, hidden_dim))
|
| 29 |
-
if use_bn:
|
| 30 |
-
layers.append(nn.BatchNorm1d(hidden_dim))
|
| 31 |
-
layers.append(nn.GELU())
|
| 32 |
-
layers.append(nn.Linear(hidden_dim, add_dim))
|
| 33 |
-
self.mlp = nn.Sequential(*layers)
|
| 34 |
-
self.add_layer = nn.Linear(add_dim, bottleneck_dim)
|
| 35 |
-
self.apply(self._init_weights)
|
| 36 |
-
self.last_layer = nn.utils.weight_norm(nn.Linear(bottleneck_dim, out_dim, bias=False))
|
| 37 |
-
self.last_layer.weight_g.data.fill_(1)
|
| 38 |
-
if norm_last_layer:
|
| 39 |
-
self.last_layer.weight_g.requires_grad = False
|
| 40 |
-
|
| 41 |
-
def _init_weights(self, m):
|
| 42 |
-
if isinstance(m, nn.Linear):
|
| 43 |
-
trunc_normal_(m.weight, std=.02)
|
| 44 |
-
if isinstance(m, nn.Linear) and m.bias is not None:
|
| 45 |
-
nn.init.constant_(m.bias, 0)
|
| 46 |
-
|
| 47 |
-
def forward(self, x):
|
| 48 |
-
reg_out = self.mlp(x)
|
| 49 |
-
x = self.add_layer(reg_out)
|
| 50 |
-
x = F.normalize(x, dim=-1, p=2)
|
| 51 |
-
x = self.last_layer(x)
|
| 52 |
-
return reg_out, x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/speakerlab/models/rdino/combiner.py
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
# Copyright 3D-Speaker (https://github.com/alibaba-damo-academy/3D-Speaker). All Rights Reserved.
|
| 2 |
-
# Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
class Combiner(torch.nn.Module):
|
| 7 |
-
"""
|
| 8 |
-
Combine backbone (ECAPA) and head (MLP)
|
| 9 |
-
"""
|
| 10 |
-
def __init__(self, backbone, head):
|
| 11 |
-
super(Combiner, self).__init__()
|
| 12 |
-
self.backbone = backbone
|
| 13 |
-
self.head = head
|
| 14 |
-
|
| 15 |
-
def forward(self, x):
|
| 16 |
-
x = self.backbone(x)
|
| 17 |
-
output = self.head(x)
|
| 18 |
-
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|