mirror of
https://github.com/storytold/storyteller-ml.git
synced 2026-10-29 09:18:21 +00:00
Adding NSFW Filter and the Style TTS2 Train Branch
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
log_dir: "logs/20201006"
|
||||
save_freq: 5
|
||||
device: "cuda"
|
||||
epochs: 180
|
||||
batch_size: 64
|
||||
pretrained_model: ""
|
||||
train_data: "ASRDataset/train_list.txt"
|
||||
val_data: "ASRDataset/val_list.txt"
|
||||
|
||||
dataset_params:
|
||||
data_augmentation: false
|
||||
|
||||
preprocess_parasm:
|
||||
sr: 24000
|
||||
spect_params:
|
||||
n_fft: 2048
|
||||
win_length: 1200
|
||||
hop_length: 300
|
||||
mel_params:
|
||||
n_mels: 80
|
||||
|
||||
model_params:
|
||||
input_dim: 80
|
||||
hidden_dim: 256
|
||||
n_token: 178
|
||||
token_embedding_dim: 512
|
||||
|
||||
optimizer_params:
|
||||
lr: 0.0005
|
||||
Binary file not shown.
@@ -0,0 +1,354 @@
|
||||
import math
|
||||
import torch
|
||||
from torch import nn
|
||||
from typing import Optional, Any
|
||||
from torch import Tensor
|
||||
import torch.nn.functional as F
|
||||
import torchaudio
|
||||
import torchaudio.functional as audio_F
|
||||
|
||||
import random
|
||||
random.seed(0)
|
||||
|
||||
|
||||
def _get_activation_fn(activ):
|
||||
if activ == 'relu':
|
||||
return nn.ReLU()
|
||||
elif activ == 'lrelu':
|
||||
return nn.LeakyReLU(0.2)
|
||||
elif activ == 'swish':
|
||||
return lambda x: x*torch.sigmoid(x)
|
||||
else:
|
||||
raise RuntimeError('Unexpected activ type %s, expected [relu, lrelu, swish]' % activ)
|
||||
|
||||
class LinearNorm(torch.nn.Module):
|
||||
def __init__(self, in_dim, out_dim, bias=True, w_init_gain='linear'):
|
||||
super(LinearNorm, self).__init__()
|
||||
self.linear_layer = torch.nn.Linear(in_dim, out_dim, bias=bias)
|
||||
|
||||
torch.nn.init.xavier_uniform_(
|
||||
self.linear_layer.weight,
|
||||
gain=torch.nn.init.calculate_gain(w_init_gain))
|
||||
|
||||
def forward(self, x):
|
||||
return self.linear_layer(x)
|
||||
|
||||
|
||||
class ConvNorm(torch.nn.Module):
|
||||
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
|
||||
padding=None, dilation=1, bias=True, w_init_gain='linear', param=None):
|
||||
super(ConvNorm, self).__init__()
|
||||
if padding is None:
|
||||
assert(kernel_size % 2 == 1)
|
||||
padding = int(dilation * (kernel_size - 1) / 2)
|
||||
|
||||
self.conv = torch.nn.Conv1d(in_channels, out_channels,
|
||||
kernel_size=kernel_size, stride=stride,
|
||||
padding=padding, dilation=dilation,
|
||||
bias=bias)
|
||||
|
||||
torch.nn.init.xavier_uniform_(
|
||||
self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain, param=param))
|
||||
|
||||
def forward(self, signal):
|
||||
conv_signal = self.conv(signal)
|
||||
return conv_signal
|
||||
|
||||
class CausualConv(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=1, dilation=1, bias=True, w_init_gain='linear', param=None):
|
||||
super(CausualConv, self).__init__()
|
||||
if padding is None:
|
||||
assert(kernel_size % 2 == 1)
|
||||
padding = int(dilation * (kernel_size - 1) / 2) * 2
|
||||
else:
|
||||
self.padding = padding * 2
|
||||
self.conv = nn.Conv1d(in_channels, out_channels,
|
||||
kernel_size=kernel_size, stride=stride,
|
||||
padding=self.padding,
|
||||
dilation=dilation,
|
||||
bias=bias)
|
||||
|
||||
torch.nn.init.xavier_uniform_(
|
||||
self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain, param=param))
|
||||
|
||||
def forward(self, x):
|
||||
x = self.conv(x)
|
||||
x = x[:, :, :-self.padding]
|
||||
return x
|
||||
|
||||
class CausualBlock(nn.Module):
|
||||
def __init__(self, hidden_dim, n_conv=3, dropout_p=0.2, activ='lrelu'):
|
||||
super(CausualBlock, self).__init__()
|
||||
self.blocks = nn.ModuleList([
|
||||
self._get_conv(hidden_dim, dilation=3**i, activ=activ, dropout_p=dropout_p)
|
||||
for i in range(n_conv)])
|
||||
|
||||
def forward(self, x):
|
||||
for block in self.blocks:
|
||||
res = x
|
||||
x = block(x)
|
||||
x += res
|
||||
return x
|
||||
|
||||
def _get_conv(self, hidden_dim, dilation, activ='lrelu', dropout_p=0.2):
|
||||
layers = [
|
||||
CausualConv(hidden_dim, hidden_dim, kernel_size=3, padding=dilation, dilation=dilation),
|
||||
_get_activation_fn(activ),
|
||||
nn.BatchNorm1d(hidden_dim),
|
||||
nn.Dropout(p=dropout_p),
|
||||
CausualConv(hidden_dim, hidden_dim, kernel_size=3, padding=1, dilation=1),
|
||||
_get_activation_fn(activ),
|
||||
nn.Dropout(p=dropout_p)
|
||||
]
|
||||
return nn.Sequential(*layers)
|
||||
|
||||
class ConvBlock(nn.Module):
|
||||
def __init__(self, hidden_dim, n_conv=3, dropout_p=0.2, activ='relu'):
|
||||
super().__init__()
|
||||
self._n_groups = 8
|
||||
self.blocks = nn.ModuleList([
|
||||
self._get_conv(hidden_dim, dilation=3**i, activ=activ, dropout_p=dropout_p)
|
||||
for i in range(n_conv)])
|
||||
|
||||
|
||||
def forward(self, x):
|
||||
for block in self.blocks:
|
||||
res = x
|
||||
x = block(x)
|
||||
x += res
|
||||
return x
|
||||
|
||||
def _get_conv(self, hidden_dim, dilation, activ='relu', dropout_p=0.2):
|
||||
layers = [
|
||||
ConvNorm(hidden_dim, hidden_dim, kernel_size=3, padding=dilation, dilation=dilation),
|
||||
_get_activation_fn(activ),
|
||||
nn.GroupNorm(num_groups=self._n_groups, num_channels=hidden_dim),
|
||||
nn.Dropout(p=dropout_p),
|
||||
ConvNorm(hidden_dim, hidden_dim, kernel_size=3, padding=1, dilation=1),
|
||||
_get_activation_fn(activ),
|
||||
nn.Dropout(p=dropout_p)
|
||||
]
|
||||
return nn.Sequential(*layers)
|
||||
|
||||
class LocationLayer(nn.Module):
|
||||
def __init__(self, attention_n_filters, attention_kernel_size,
|
||||
attention_dim):
|
||||
super(LocationLayer, self).__init__()
|
||||
padding = int((attention_kernel_size - 1) / 2)
|
||||
self.location_conv = ConvNorm(2, attention_n_filters,
|
||||
kernel_size=attention_kernel_size,
|
||||
padding=padding, bias=False, stride=1,
|
||||
dilation=1)
|
||||
self.location_dense = LinearNorm(attention_n_filters, attention_dim,
|
||||
bias=False, w_init_gain='tanh')
|
||||
|
||||
def forward(self, attention_weights_cat):
|
||||
processed_attention = self.location_conv(attention_weights_cat)
|
||||
processed_attention = processed_attention.transpose(1, 2)
|
||||
processed_attention = self.location_dense(processed_attention)
|
||||
return processed_attention
|
||||
|
||||
|
||||
class Attention(nn.Module):
|
||||
def __init__(self, attention_rnn_dim, embedding_dim, attention_dim,
|
||||
attention_location_n_filters, attention_location_kernel_size):
|
||||
super(Attention, self).__init__()
|
||||
self.query_layer = LinearNorm(attention_rnn_dim, attention_dim,
|
||||
bias=False, w_init_gain='tanh')
|
||||
self.memory_layer = LinearNorm(embedding_dim, attention_dim, bias=False,
|
||||
w_init_gain='tanh')
|
||||
self.v = LinearNorm(attention_dim, 1, bias=False)
|
||||
self.location_layer = LocationLayer(attention_location_n_filters,
|
||||
attention_location_kernel_size,
|
||||
attention_dim)
|
||||
self.score_mask_value = -float("inf")
|
||||
|
||||
def get_alignment_energies(self, query, processed_memory,
|
||||
attention_weights_cat):
|
||||
"""
|
||||
PARAMS
|
||||
------
|
||||
query: decoder output (batch, n_mel_channels * n_frames_per_step)
|
||||
processed_memory: processed encoder outputs (B, T_in, attention_dim)
|
||||
attention_weights_cat: cumulative and prev. att weights (B, 2, max_time)
|
||||
RETURNS
|
||||
-------
|
||||
alignment (batch, max_time)
|
||||
"""
|
||||
|
||||
processed_query = self.query_layer(query.unsqueeze(1))
|
||||
processed_attention_weights = self.location_layer(attention_weights_cat)
|
||||
energies = self.v(torch.tanh(
|
||||
processed_query + processed_attention_weights + processed_memory))
|
||||
|
||||
energies = energies.squeeze(-1)
|
||||
return energies
|
||||
|
||||
def forward(self, attention_hidden_state, memory, processed_memory,
|
||||
attention_weights_cat, mask):
|
||||
"""
|
||||
PARAMS
|
||||
------
|
||||
attention_hidden_state: attention rnn last output
|
||||
memory: encoder outputs
|
||||
processed_memory: processed encoder outputs
|
||||
attention_weights_cat: previous and cummulative attention weights
|
||||
mask: binary mask for padded data
|
||||
"""
|
||||
alignment = self.get_alignment_energies(
|
||||
attention_hidden_state, processed_memory, attention_weights_cat)
|
||||
|
||||
if mask is not None:
|
||||
alignment.data.masked_fill_(mask, self.score_mask_value)
|
||||
|
||||
attention_weights = F.softmax(alignment, dim=1)
|
||||
attention_context = torch.bmm(attention_weights.unsqueeze(1), memory)
|
||||
attention_context = attention_context.squeeze(1)
|
||||
|
||||
return attention_context, attention_weights
|
||||
|
||||
|
||||
class ForwardAttentionV2(nn.Module):
|
||||
def __init__(self, attention_rnn_dim, embedding_dim, attention_dim,
|
||||
attention_location_n_filters, attention_location_kernel_size):
|
||||
super(ForwardAttentionV2, self).__init__()
|
||||
self.query_layer = LinearNorm(attention_rnn_dim, attention_dim,
|
||||
bias=False, w_init_gain='tanh')
|
||||
self.memory_layer = LinearNorm(embedding_dim, attention_dim, bias=False,
|
||||
w_init_gain='tanh')
|
||||
self.v = LinearNorm(attention_dim, 1, bias=False)
|
||||
self.location_layer = LocationLayer(attention_location_n_filters,
|
||||
attention_location_kernel_size,
|
||||
attention_dim)
|
||||
self.score_mask_value = -float(1e20)
|
||||
|
||||
def get_alignment_energies(self, query, processed_memory,
|
||||
attention_weights_cat):
|
||||
"""
|
||||
PARAMS
|
||||
------
|
||||
query: decoder output (batch, n_mel_channels * n_frames_per_step)
|
||||
processed_memory: processed encoder outputs (B, T_in, attention_dim)
|
||||
attention_weights_cat: prev. and cumulative att weights (B, 2, max_time)
|
||||
RETURNS
|
||||
-------
|
||||
alignment (batch, max_time)
|
||||
"""
|
||||
|
||||
processed_query = self.query_layer(query.unsqueeze(1))
|
||||
processed_attention_weights = self.location_layer(attention_weights_cat)
|
||||
energies = self.v(torch.tanh(
|
||||
processed_query + processed_attention_weights + processed_memory))
|
||||
|
||||
energies = energies.squeeze(-1)
|
||||
return energies
|
||||
|
||||
def forward(self, attention_hidden_state, memory, processed_memory,
|
||||
attention_weights_cat, mask, log_alpha):
|
||||
"""
|
||||
PARAMS
|
||||
------
|
||||
attention_hidden_state: attention rnn last output
|
||||
memory: encoder outputs
|
||||
processed_memory: processed encoder outputs
|
||||
attention_weights_cat: previous and cummulative attention weights
|
||||
mask: binary mask for padded data
|
||||
"""
|
||||
log_energy = self.get_alignment_energies(
|
||||
attention_hidden_state, processed_memory, attention_weights_cat)
|
||||
|
||||
#log_energy =
|
||||
|
||||
if mask is not None:
|
||||
log_energy.data.masked_fill_(mask, self.score_mask_value)
|
||||
|
||||
#attention_weights = F.softmax(alignment, dim=1)
|
||||
|
||||
#content_score = log_energy.unsqueeze(1) #[B, MAX_TIME] -> [B, 1, MAX_TIME]
|
||||
#log_alpha = log_alpha.unsqueeze(2) #[B, MAX_TIME] -> [B, MAX_TIME, 1]
|
||||
|
||||
#log_total_score = log_alpha + content_score
|
||||
|
||||
#previous_attention_weights = attention_weights_cat[:,0,:]
|
||||
|
||||
log_alpha_shift_padded = []
|
||||
max_time = log_energy.size(1)
|
||||
for sft in range(2):
|
||||
shifted = log_alpha[:,:max_time-sft]
|
||||
shift_padded = F.pad(shifted, (sft,0), 'constant', self.score_mask_value)
|
||||
log_alpha_shift_padded.append(shift_padded.unsqueeze(2))
|
||||
|
||||
biased = torch.logsumexp(torch.cat(log_alpha_shift_padded,2), 2)
|
||||
|
||||
log_alpha_new = biased + log_energy
|
||||
|
||||
attention_weights = F.softmax(log_alpha_new, dim=1)
|
||||
|
||||
attention_context = torch.bmm(attention_weights.unsqueeze(1), memory)
|
||||
attention_context = attention_context.squeeze(1)
|
||||
|
||||
return attention_context, attention_weights, log_alpha_new
|
||||
|
||||
|
||||
class PhaseShuffle2d(nn.Module):
|
||||
def __init__(self, n=2):
|
||||
super(PhaseShuffle2d, self).__init__()
|
||||
self.n = n
|
||||
self.random = random.Random(1)
|
||||
|
||||
def forward(self, x, move=None):
|
||||
# x.size = (B, C, M, L)
|
||||
if move is None:
|
||||
move = self.random.randint(-self.n, self.n)
|
||||
|
||||
if move == 0:
|
||||
return x
|
||||
else:
|
||||
left = x[:, :, :, :move]
|
||||
right = x[:, :, :, move:]
|
||||
shuffled = torch.cat([right, left], dim=3)
|
||||
return shuffled
|
||||
|
||||
class PhaseShuffle1d(nn.Module):
|
||||
def __init__(self, n=2):
|
||||
super(PhaseShuffle1d, self).__init__()
|
||||
self.n = n
|
||||
self.random = random.Random(1)
|
||||
|
||||
def forward(self, x, move=None):
|
||||
# x.size = (B, C, M, L)
|
||||
if move is None:
|
||||
move = self.random.randint(-self.n, self.n)
|
||||
|
||||
if move == 0:
|
||||
return x
|
||||
else:
|
||||
left = x[:, :, :move]
|
||||
right = x[:, :, move:]
|
||||
shuffled = torch.cat([right, left], dim=2)
|
||||
|
||||
return shuffled
|
||||
|
||||
class MFCC(nn.Module):
|
||||
def __init__(self, n_mfcc=40, n_mels=80):
|
||||
super(MFCC, self).__init__()
|
||||
self.n_mfcc = n_mfcc
|
||||
self.n_mels = n_mels
|
||||
self.norm = 'ortho'
|
||||
dct_mat = audio_F.create_dct(self.n_mfcc, self.n_mels, self.norm)
|
||||
self.register_buffer('dct_mat', dct_mat)
|
||||
|
||||
def forward(self, mel_specgram):
|
||||
if len(mel_specgram.shape) == 2:
|
||||
mel_specgram = mel_specgram.unsqueeze(0)
|
||||
unsqueezed = True
|
||||
else:
|
||||
unsqueezed = False
|
||||
# (channel, n_mels, time).tranpose(...) dot (n_mels, n_mfcc)
|
||||
# -> (channel, time, n_mfcc).tranpose(...)
|
||||
mfcc = torch.matmul(mel_specgram.transpose(1, 2), self.dct_mat).transpose(1, 2)
|
||||
|
||||
# unpack batch
|
||||
if unsqueezed:
|
||||
mfcc = mfcc.squeeze(0)
|
||||
return mfcc
|
||||
@@ -0,0 +1,186 @@
|
||||
import math
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import TransformerEncoder
|
||||
import torch.nn.functional as F
|
||||
from .layers import MFCC, Attention, LinearNorm, ConvNorm, ConvBlock
|
||||
|
||||
class ASRCNN(nn.Module):
|
||||
def __init__(self,
|
||||
input_dim=80,
|
||||
hidden_dim=256,
|
||||
n_token=35,
|
||||
n_layers=6,
|
||||
token_embedding_dim=256,
|
||||
|
||||
):
|
||||
super().__init__()
|
||||
self.n_token = n_token
|
||||
self.n_down = 1
|
||||
self.to_mfcc = MFCC()
|
||||
self.init_cnn = ConvNorm(input_dim//2, hidden_dim, kernel_size=7, padding=3, stride=2)
|
||||
self.cnns = nn.Sequential(
|
||||
*[nn.Sequential(
|
||||
ConvBlock(hidden_dim),
|
||||
nn.GroupNorm(num_groups=1, num_channels=hidden_dim)
|
||||
) for n in range(n_layers)])
|
||||
self.projection = ConvNorm(hidden_dim, hidden_dim // 2)
|
||||
self.ctc_linear = nn.Sequential(
|
||||
LinearNorm(hidden_dim//2, hidden_dim),
|
||||
nn.ReLU(),
|
||||
LinearNorm(hidden_dim, n_token))
|
||||
self.asr_s2s = ASRS2S(
|
||||
embedding_dim=token_embedding_dim,
|
||||
hidden_dim=hidden_dim//2,
|
||||
n_token=n_token)
|
||||
|
||||
def forward(self, x, src_key_padding_mask=None, text_input=None):
|
||||
x = self.to_mfcc(x)
|
||||
x = self.init_cnn(x)
|
||||
x = self.cnns(x)
|
||||
x = self.projection(x)
|
||||
x = x.transpose(1, 2)
|
||||
ctc_logit = self.ctc_linear(x)
|
||||
if text_input is not None:
|
||||
_, s2s_logit, s2s_attn = self.asr_s2s(x, src_key_padding_mask, text_input)
|
||||
return ctc_logit, s2s_logit, s2s_attn
|
||||
else:
|
||||
return ctc_logit
|
||||
|
||||
def get_feature(self, x):
|
||||
x = self.to_mfcc(x.squeeze(1))
|
||||
x = self.init_cnn(x)
|
||||
x = self.cnns(x)
|
||||
x = self.projection(x)
|
||||
return x
|
||||
|
||||
def length_to_mask(self, lengths):
|
||||
mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
|
||||
mask = torch.gt(mask+1, lengths.unsqueeze(1)).to(lengths.device)
|
||||
return mask
|
||||
|
||||
def get_future_mask(self, out_length, unmask_future_steps=0):
|
||||
"""
|
||||
Args:
|
||||
out_length (int): returned mask shape is (out_length, out_length).
|
||||
unmask_futre_steps (int): unmasking future step size.
|
||||
Return:
|
||||
mask (torch.BoolTensor): mask future timesteps mask[i, j] = True if i > j + unmask_future_steps else False
|
||||
"""
|
||||
index_tensor = torch.arange(out_length).unsqueeze(0).expand(out_length, -1)
|
||||
mask = torch.gt(index_tensor, index_tensor.T + unmask_future_steps)
|
||||
return mask
|
||||
|
||||
class ASRS2S(nn.Module):
|
||||
def __init__(self,
|
||||
embedding_dim=256,
|
||||
hidden_dim=512,
|
||||
n_location_filters=32,
|
||||
location_kernel_size=63,
|
||||
n_token=40):
|
||||
super(ASRS2S, self).__init__()
|
||||
self.embedding = nn.Embedding(n_token, embedding_dim)
|
||||
val_range = math.sqrt(6 / hidden_dim)
|
||||
self.embedding.weight.data.uniform_(-val_range, val_range)
|
||||
|
||||
self.decoder_rnn_dim = hidden_dim
|
||||
self.project_to_n_symbols = nn.Linear(self.decoder_rnn_dim, n_token)
|
||||
self.attention_layer = Attention(
|
||||
self.decoder_rnn_dim,
|
||||
hidden_dim,
|
||||
hidden_dim,
|
||||
n_location_filters,
|
||||
location_kernel_size
|
||||
)
|
||||
self.decoder_rnn = nn.LSTMCell(self.decoder_rnn_dim + embedding_dim, self.decoder_rnn_dim)
|
||||
self.project_to_hidden = nn.Sequential(
|
||||
LinearNorm(self.decoder_rnn_dim * 2, hidden_dim),
|
||||
nn.Tanh())
|
||||
self.sos = 1
|
||||
self.eos = 2
|
||||
|
||||
def initialize_decoder_states(self, memory, mask):
|
||||
"""
|
||||
moemory.shape = (B, L, H) = (Batchsize, Maxtimestep, Hiddendim)
|
||||
"""
|
||||
B, L, H = memory.shape
|
||||
self.decoder_hidden = torch.zeros((B, self.decoder_rnn_dim)).type_as(memory)
|
||||
self.decoder_cell = torch.zeros((B, self.decoder_rnn_dim)).type_as(memory)
|
||||
self.attention_weights = torch.zeros((B, L)).type_as(memory)
|
||||
self.attention_weights_cum = torch.zeros((B, L)).type_as(memory)
|
||||
self.attention_context = torch.zeros((B, H)).type_as(memory)
|
||||
self.memory = memory
|
||||
self.processed_memory = self.attention_layer.memory_layer(memory)
|
||||
self.mask = mask
|
||||
self.unk_index = 3
|
||||
self.random_mask = 0.1
|
||||
|
||||
def forward(self, memory, memory_mask, text_input):
|
||||
"""
|
||||
moemory.shape = (B, L, H) = (Batchsize, Maxtimestep, Hiddendim)
|
||||
moemory_mask.shape = (B, L, )
|
||||
texts_input.shape = (B, T)
|
||||
"""
|
||||
self.initialize_decoder_states(memory, memory_mask)
|
||||
# text random mask
|
||||
random_mask = (torch.rand(text_input.shape) < self.random_mask).to(text_input.device)
|
||||
_text_input = text_input.clone()
|
||||
_text_input.masked_fill_(random_mask, self.unk_index)
|
||||
decoder_inputs = self.embedding(_text_input).transpose(0, 1) # -> [T, B, channel]
|
||||
start_embedding = self.embedding(
|
||||
torch.LongTensor([self.sos]*decoder_inputs.size(1)).to(decoder_inputs.device))
|
||||
decoder_inputs = torch.cat((start_embedding.unsqueeze(0), decoder_inputs), dim=0)
|
||||
|
||||
hidden_outputs, logit_outputs, alignments = [], [], []
|
||||
while len(hidden_outputs) < decoder_inputs.size(0):
|
||||
|
||||
decoder_input = decoder_inputs[len(hidden_outputs)]
|
||||
hidden, logit, attention_weights = self.decode(decoder_input)
|
||||
hidden_outputs += [hidden]
|
||||
logit_outputs += [logit]
|
||||
alignments += [attention_weights]
|
||||
|
||||
hidden_outputs, logit_outputs, alignments = \
|
||||
self.parse_decoder_outputs(
|
||||
hidden_outputs, logit_outputs, alignments)
|
||||
|
||||
return hidden_outputs, logit_outputs, alignments
|
||||
|
||||
|
||||
def decode(self, decoder_input):
|
||||
|
||||
cell_input = torch.cat((decoder_input, self.attention_context), -1)
|
||||
self.decoder_hidden, self.decoder_cell = self.decoder_rnn(
|
||||
cell_input,
|
||||
(self.decoder_hidden, self.decoder_cell))
|
||||
|
||||
attention_weights_cat = torch.cat(
|
||||
(self.attention_weights.unsqueeze(1),
|
||||
self.attention_weights_cum.unsqueeze(1)),dim=1)
|
||||
|
||||
self.attention_context, self.attention_weights = self.attention_layer(
|
||||
self.decoder_hidden,
|
||||
self.memory,
|
||||
self.processed_memory,
|
||||
attention_weights_cat,
|
||||
self.mask)
|
||||
|
||||
self.attention_weights_cum += self.attention_weights
|
||||
|
||||
hidden_and_context = torch.cat((self.decoder_hidden, self.attention_context), -1)
|
||||
hidden = self.project_to_hidden(hidden_and_context)
|
||||
|
||||
# dropout to increasing g
|
||||
logit = self.project_to_n_symbols(F.dropout(hidden, 0.5, self.training))
|
||||
|
||||
return hidden, logit, self.attention_weights
|
||||
|
||||
def parse_decoder_outputs(self, hidden, logit, alignments):
|
||||
|
||||
# -> [B, T_out + 1, max_time]
|
||||
alignments = torch.stack(alignments).transpose(0,1)
|
||||
# [T_out + 1, B, n_symbols] -> [B, T_out + 1, n_symbols]
|
||||
logit = torch.stack(logit).transpose(0, 1).contiguous()
|
||||
hidden = torch.stack(hidden).transpose(0, 1).contiguous()
|
||||
|
||||
return hidden, logit, alignments
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,190 @@
|
||||
"""
|
||||
Implementation of model from:
|
||||
Kum et al. - "Joint Detection and Classification of Singing Voice Melody Using
|
||||
Convolutional Recurrent Neural Networks" (2019)
|
||||
Link: https://www.semanticscholar.org/paper/Joint-Detection-and-Classification-of-Singing-Voice-Kum-Nam/60a2ad4c7db43bace75805054603747fcd062c0d
|
||||
"""
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
class JDCNet(nn.Module):
|
||||
"""
|
||||
Joint Detection and Classification Network model for singing voice melody.
|
||||
"""
|
||||
def __init__(self, num_class=722, seq_len=31, leaky_relu_slope=0.01):
|
||||
super().__init__()
|
||||
self.num_class = num_class
|
||||
|
||||
# input = (b, 1, 31, 513), b = batch size
|
||||
self.conv_block = nn.Sequential(
|
||||
nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, padding=1, bias=False), # out: (b, 64, 31, 513)
|
||||
nn.BatchNorm2d(num_features=64),
|
||||
nn.LeakyReLU(leaky_relu_slope, inplace=True),
|
||||
nn.Conv2d(64, 64, 3, padding=1, bias=False), # (b, 64, 31, 513)
|
||||
)
|
||||
|
||||
# res blocks
|
||||
self.res_block1 = ResBlock(in_channels=64, out_channels=128) # (b, 128, 31, 128)
|
||||
self.res_block2 = ResBlock(in_channels=128, out_channels=192) # (b, 192, 31, 32)
|
||||
self.res_block3 = ResBlock(in_channels=192, out_channels=256) # (b, 256, 31, 8)
|
||||
|
||||
# pool block
|
||||
self.pool_block = nn.Sequential(
|
||||
nn.BatchNorm2d(num_features=256),
|
||||
nn.LeakyReLU(leaky_relu_slope, inplace=True),
|
||||
nn.MaxPool2d(kernel_size=(1, 4)), # (b, 256, 31, 2)
|
||||
nn.Dropout(p=0.2),
|
||||
)
|
||||
|
||||
# maxpool layers (for auxiliary network inputs)
|
||||
# in = (b, 128, 31, 513) from conv_block, out = (b, 128, 31, 2)
|
||||
self.maxpool1 = nn.MaxPool2d(kernel_size=(1, 40))
|
||||
# in = (b, 128, 31, 128) from res_block1, out = (b, 128, 31, 2)
|
||||
self.maxpool2 = nn.MaxPool2d(kernel_size=(1, 20))
|
||||
# in = (b, 128, 31, 32) from res_block2, out = (b, 128, 31, 2)
|
||||
self.maxpool3 = nn.MaxPool2d(kernel_size=(1, 10))
|
||||
|
||||
# in = (b, 640, 31, 2), out = (b, 256, 31, 2)
|
||||
self.detector_conv = nn.Sequential(
|
||||
nn.Conv2d(640, 256, 1, bias=False),
|
||||
nn.BatchNorm2d(256),
|
||||
nn.LeakyReLU(leaky_relu_slope, inplace=True),
|
||||
nn.Dropout(p=0.2),
|
||||
)
|
||||
|
||||
# input: (b, 31, 512) - resized from (b, 256, 31, 2)
|
||||
self.bilstm_classifier = nn.LSTM(
|
||||
input_size=512, hidden_size=256,
|
||||
batch_first=True, bidirectional=True) # (b, 31, 512)
|
||||
|
||||
# input: (b, 31, 512) - resized from (b, 256, 31, 2)
|
||||
self.bilstm_detector = nn.LSTM(
|
||||
input_size=512, hidden_size=256,
|
||||
batch_first=True, bidirectional=True) # (b, 31, 512)
|
||||
|
||||
# input: (b * 31, 512)
|
||||
self.classifier = nn.Linear(in_features=512, out_features=self.num_class) # (b * 31, num_class)
|
||||
|
||||
# input: (b * 31, 512)
|
||||
self.detector = nn.Linear(in_features=512, out_features=2) # (b * 31, 2) - binary classifier
|
||||
|
||||
# initialize weights
|
||||
self.apply(self.init_weights)
|
||||
|
||||
def get_feature_GAN(self, x):
|
||||
seq_len = x.shape[-2]
|
||||
x = x.float().transpose(-1, -2)
|
||||
|
||||
convblock_out = self.conv_block(x)
|
||||
|
||||
resblock1_out = self.res_block1(convblock_out)
|
||||
resblock2_out = self.res_block2(resblock1_out)
|
||||
resblock3_out = self.res_block3(resblock2_out)
|
||||
poolblock_out = self.pool_block[0](resblock3_out)
|
||||
poolblock_out = self.pool_block[1](poolblock_out)
|
||||
|
||||
return poolblock_out.transpose(-1, -2)
|
||||
|
||||
def get_feature(self, x):
|
||||
seq_len = x.shape[-2]
|
||||
x = x.float().transpose(-1, -2)
|
||||
|
||||
convblock_out = self.conv_block(x)
|
||||
|
||||
resblock1_out = self.res_block1(convblock_out)
|
||||
resblock2_out = self.res_block2(resblock1_out)
|
||||
resblock3_out = self.res_block3(resblock2_out)
|
||||
poolblock_out = self.pool_block[0](resblock3_out)
|
||||
poolblock_out = self.pool_block[1](poolblock_out)
|
||||
|
||||
return self.pool_block[2](poolblock_out)
|
||||
|
||||
def forward(self, x):
|
||||
"""
|
||||
Returns:
|
||||
classification_prediction, detection_prediction
|
||||
sizes: (b, 31, 722), (b, 31, 2)
|
||||
"""
|
||||
###############################
|
||||
# forward pass for classifier #
|
||||
###############################
|
||||
seq_len = x.shape[-1]
|
||||
x = x.float().transpose(-1, -2)
|
||||
|
||||
convblock_out = self.conv_block(x)
|
||||
|
||||
resblock1_out = self.res_block1(convblock_out)
|
||||
resblock2_out = self.res_block2(resblock1_out)
|
||||
resblock3_out = self.res_block3(resblock2_out)
|
||||
|
||||
|
||||
poolblock_out = self.pool_block[0](resblock3_out)
|
||||
poolblock_out = self.pool_block[1](poolblock_out)
|
||||
GAN_feature = poolblock_out.transpose(-1, -2)
|
||||
poolblock_out = self.pool_block[2](poolblock_out)
|
||||
|
||||
# (b, 256, 31, 2) => (b, 31, 256, 2) => (b, 31, 512)
|
||||
classifier_out = poolblock_out.permute(0, 2, 1, 3).contiguous().view((-1, seq_len, 512))
|
||||
classifier_out, _ = self.bilstm_classifier(classifier_out) # ignore the hidden states
|
||||
|
||||
classifier_out = classifier_out.contiguous().view((-1, 512)) # (b * 31, 512)
|
||||
classifier_out = self.classifier(classifier_out)
|
||||
classifier_out = classifier_out.view((-1, seq_len, self.num_class)) # (b, 31, num_class)
|
||||
|
||||
# sizes: (b, 31, 722), (b, 31, 2)
|
||||
# classifier output consists of predicted pitch classes per frame
|
||||
# detector output consists of: (isvoice, notvoice) estimates per frame
|
||||
return torch.abs(classifier_out.squeeze((1, 2))), GAN_feature, poolblock_out
|
||||
|
||||
@staticmethod
|
||||
def init_weights(m):
|
||||
if isinstance(m, nn.Linear):
|
||||
nn.init.kaiming_uniform_(m.weight)
|
||||
if m.bias is not None:
|
||||
nn.init.constant_(m.bias, 0)
|
||||
elif isinstance(m, nn.Conv2d):
|
||||
nn.init.xavier_normal_(m.weight)
|
||||
elif isinstance(m, nn.LSTM) or isinstance(m, nn.LSTMCell):
|
||||
for p in m.parameters():
|
||||
if p.data is None:
|
||||
continue
|
||||
|
||||
if len(p.shape) >= 2:
|
||||
nn.init.orthogonal_(p.data)
|
||||
else:
|
||||
nn.init.normal_(p.data)
|
||||
|
||||
|
||||
class ResBlock(nn.Module):
|
||||
def __init__(self, in_channels: int, out_channels: int, leaky_relu_slope=0.01):
|
||||
super().__init__()
|
||||
self.downsample = in_channels != out_channels
|
||||
|
||||
# BN / LReLU / MaxPool layer before the conv layer - see Figure 1b in the paper
|
||||
self.pre_conv = nn.Sequential(
|
||||
nn.BatchNorm2d(num_features=in_channels),
|
||||
nn.LeakyReLU(leaky_relu_slope, inplace=True),
|
||||
nn.MaxPool2d(kernel_size=(1, 2)), # apply downsampling on the y axis only
|
||||
)
|
||||
|
||||
# conv layers
|
||||
self.conv = nn.Sequential(
|
||||
nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
|
||||
kernel_size=3, padding=1, bias=False),
|
||||
nn.BatchNorm2d(out_channels),
|
||||
nn.LeakyReLU(leaky_relu_slope, inplace=True),
|
||||
nn.Conv2d(out_channels, out_channels, 3, padding=1, bias=False),
|
||||
)
|
||||
|
||||
# 1 x 1 convolution layer to match the feature dimensions
|
||||
self.conv1by1 = None
|
||||
if self.downsample:
|
||||
self.conv1by1 = nn.Conv2d(in_channels, out_channels, 1, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.pre_conv(x)
|
||||
if self.downsample:
|
||||
x = self.conv(x) + self.conv1by1(x)
|
||||
else:
|
||||
x = self.conv(x) + x
|
||||
return x
|
||||
@@ -0,0 +1,30 @@
|
||||
log_dir: "Checkpoint"
|
||||
mixed_precision: "fp16"
|
||||
data_folder: "wikipedia_20220301.en.processed"
|
||||
batch_size: 192
|
||||
save_interval: 5000
|
||||
log_interval: 10
|
||||
num_process: 1 # number of GPUs
|
||||
num_steps: 1000000
|
||||
|
||||
dataset_params:
|
||||
tokenizer: "transfo-xl-wt103"
|
||||
token_separator: " " # token used for phoneme separator (space)
|
||||
token_mask: "M" # token used for phoneme mask (M)
|
||||
word_separator: 3039 # token used for word separator (<formula>)
|
||||
token_maps: "token_maps.pkl" # token map path
|
||||
|
||||
max_mel_length: 512 # max phoneme length
|
||||
|
||||
word_mask_prob: 0.15 # probability to mask the entire word
|
||||
phoneme_mask_prob: 0.1 # probability to mask each phoneme
|
||||
replace_prob: 0.2 # probablity to replace phonemes
|
||||
|
||||
model_params:
|
||||
vocab_size: 178
|
||||
hidden_size: 768
|
||||
num_attention_heads: 12
|
||||
intermediate_size: 2048
|
||||
max_position_embeddings: 512
|
||||
num_hidden_layers: 12
|
||||
dropout: 0.1
|
||||
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
import os
|
||||
import yaml
|
||||
import torch
|
||||
from transformers import AlbertConfig, AlbertModel
|
||||
|
||||
class CustomAlbert(AlbertModel):
|
||||
def forward(self, *args, **kwargs):
|
||||
# Call the original forward method
|
||||
outputs = super().forward(*args, **kwargs)
|
||||
|
||||
# Only return the last_hidden_state
|
||||
return outputs.last_hidden_state
|
||||
|
||||
|
||||
def load_plbert(log_dir):
|
||||
config_path = os.path.join(log_dir, "config.yml")
|
||||
plbert_config = yaml.safe_load(open(config_path))
|
||||
|
||||
albert_base_configuration = AlbertConfig(**plbert_config['model_params'])
|
||||
bert = CustomAlbert(albert_base_configuration)
|
||||
|
||||
files = os.listdir(log_dir)
|
||||
ckpts = []
|
||||
for f in os.listdir(log_dir):
|
||||
if f.startswith("step_"): ckpts.append(f)
|
||||
|
||||
iters = [int(f.split('_')[-1].split('.')[0]) for f in ckpts if os.path.isfile(os.path.join(log_dir, f))]
|
||||
iters = sorted(iters)[-1]
|
||||
|
||||
checkpoint = torch.load(log_dir + "/step_" + str(iters) + ".t7", map_location='cpu')
|
||||
state_dict = checkpoint['net']
|
||||
from collections import OrderedDict
|
||||
new_state_dict = OrderedDict()
|
||||
for k, v in state_dict.items():
|
||||
name = k[7:] # remove `module.`
|
||||
if name.startswith('encoder.'):
|
||||
name = name[8:] # remove `encoder.`
|
||||
new_state_dict[name] = v
|
||||
del new_state_dict["embeddings.position_ids"]
|
||||
bert.load_state_dict(new_state_dict, strict=False)
|
||||
|
||||
return bert
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Reference in New Issue
Block a user