Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
improved = mask_conf.pop('improved')
# We will take magnitude and concat with ReIm
if improved:
masker = TDCNpp(in_chan=3 * enc.filterbank.n_feats_out // 2,
out_chan=enc.filterbank.n_feats_out,
n_src=3, # Hardcoded here because of FUSS
**mask_conf)
else:
masker = TDConvNet(in_chan=3 * enc.filterbank.n_feats_out // 2,
out_chan=enc.filterbank.n_feats_out,
n_src=3, # Hardcoded here because of FUSS
**mask_conf)
model = Model(enc, masker, dec, learnable_scaling=mask_conf["learnable_scaling"])
# Define optimizer of this model
optimizer = make_optimizer(model.parameters(), **conf['optim'])
return model, optimizer
def make_model_and_optimizer(conf):
""" Function to define the model and optimizer for a config dictionary.
Args:
conf: Dictionary containing the output of hierachical argparse.
Returns:
model, optimizer.
The main goal of this function is to make reloading for resuming
and evaluation very simple.
"""
enc, dec = fb.make_enc_dec('stft', **conf['filterbank'])
masker = Chimera(enc.n_feats_out // 2,
**conf['masknet'])
model = Model(enc, masker, dec)
optimizer = make_optimizer(model.parameters(), **conf['optim'])
return model, optimizer
def make_model_and_optimizer(conf):
""" Function to define the model and optimizer for a config dictionary.
Args:
conf: Dictionary containing the output of hierachical argparse.
Returns:
model, optimizer.
The main goal of this function is to make reloading for resuming
and evaluation very simple.
"""
# Define building blocks for local model
# Filterbank can be either stftfb or freefb
enc, dec = fb.make_enc_dec(**conf['filterbank'])
mask_conf = dict(conf['masknet']) # Make a copy
improved = mask_conf.pop('improved')
# We will take magnitude and concat with ReIm
if improved:
masker = TDCNpp(in_chan=3 * enc.filterbank.n_feats_out // 2,
out_chan=enc.filterbank.n_feats_out,
n_src=3, # Hardcoded here because of FUSS
**mask_conf)
else:
masker = TDConvNet(in_chan=3 * enc.filterbank.n_feats_out // 2,
out_chan=enc.filterbank.n_feats_out,
n_src=3, # Hardcoded here because of FUSS
**mask_conf)
model = Model(enc, masker, dec, learnable_scaling=mask_conf["learnable_scaling"])
# Define optimizer of this model
super().__init__()
self.in_chan = in_chan
self.n_src = n_src
out_chan = out_chan if out_chan else in_chan
self.out_chan = out_chan
self.n_blocks = n_blocks
self.n_repeats = n_repeats
self.bn_chan = bn_chan
self.hid_chan = hid_chan
self.skip_chan = skip_chan
self.kernel_size = kernel_size
self.norm_type = norm_type
self.mask_act = mask_act
self.learnable_scaling = learnable_scaling
layer_norm = norms.get(norm_type)(in_chan)
bottleneck_conv = nn.Conv1d(in_chan, bn_chan, 1)
self.bottleneck = nn.Sequential(layer_norm, bottleneck_conv)
# Succession of Conv1DBlock with exponentially increasing dilation.
self.TCN = nn.ModuleList()
for r in range(n_repeats):
for x in range(n_blocks):
padding = (kernel_size - 1) * 2**x // 2
self.TCN.append(Conv1DBlock(bn_chan, hid_chan, skip_chan,
kernel_size, padding=padding,
dilation=2**x, norm_type=norm_type))
# Dense connection in TDCNpp
self.dense_skip = nn.ModuleList()
for r in range(n_repeats-1):
self.dense_skip.append(nn.Conv1d(bn_chan, bn_chan, 1))
scaling_param = torch.tensor([0.9**l for l in range(1, n_blocks)])
norm_type="gLN", mask_act='relu'):
super(TDConvNet, self).__init__()
self.in_chan = in_chan
self.n_src = n_src
out_chan = out_chan if out_chan else in_chan
self.out_chan = out_chan
self.n_blocks = n_blocks
self.n_repeats = n_repeats
self.bn_chan = bn_chan
self.hid_chan = hid_chan
self.skip_chan = skip_chan
self.kernel_size = kernel_size
self.norm_type = norm_type
self.mask_act = mask_act
layer_norm = norms.get(norm_type)(in_chan)
bottleneck_conv = nn.Conv1d(in_chan, bn_chan, 1)
self.bottleneck = nn.Sequential(layer_norm, bottleneck_conv)
# Succession of Conv1DBlock with exponentially increasing dilation.
self.TCN = nn.ModuleList()
for r in range(n_repeats):
for x in range(n_blocks):
padding = (kernel_size - 1) * 2**x // 2
self.TCN.append(Conv1DBlock(bn_chan, hid_chan, skip_chan,
kernel_size, padding=padding,
dilation=2**x, norm_type=norm_type))
mask_conv_inp = skip_chan if skip_chan else bn_chan
mask_conv = nn.Conv1d(mask_conv_inp, n_src*out_chan, 1)
self.mask_net = nn.Sequential(nn.PReLU(), mask_conv)
# Get activation function.
mask_nl_class = activations.get(mask_act)
# For softmax, feed the source dimension.
self.out_chan = out_chan
self.bn_chan = bn_chan
self.hid_size = hid_size
self.chunk_size = chunk_size
hop_size = hop_size if hop_size is not None else chunk_size // 2
self.hop_size = hop_size
self.n_repeats = n_repeats
self.n_src = n_src
self.norm_type = norm_type
self.mask_act = mask_act
self.bidirectional = bidirectional
self.rnn_type = rnn_type
self.num_layers = num_layers
self.dropout = dropout
layer_norm = norms.get(norm_type)(in_chan)
bottleneck_conv = nn.Conv1d(in_chan, bn_chan, 1)
self.bottleneck = nn.Sequential(layer_norm, bottleneck_conv)
# Succession of DPRNNBlocks.
net = []
for x in range(self.n_repeats):
net += [DPRNNBlock(bn_chan, hid_size, norm_type=norm_type,
bidirectional=bidirectional, rnn_type=rnn_type,
num_layers=num_layers, dropout=dropout)]
self.net = nn.Sequential(*net)
# Masking in 3D space
net_out_conv = nn.Conv2d(bn_chan, n_src*bn_chan, 1)
self.first_out = nn.Sequential(nn.PReLU(), net_out_conv)
# Gating and masking in 2D space (after fold)
self.net_out = nn.Sequential(nn.Conv1d(bn_chan, bn_chan, 1), nn.Tanh())
self.net_gate = nn.Sequential(nn.Conv1d(bn_chan, bn_chan, 1),
def make_model_and_optimizer(conf):
""" Function to define the model and optimizer for a config dictionary.
Args:
conf: Dictionary containing the output of hierachical argparse.
Returns:
model, optimizer.
The main goal of this function is to make reloading for resuming
and evaluation very simple.
"""
enc, dec = fb.make_enc_dec('stft', **conf['filterbank'])
masker = Chimera(enc.n_feats_out // 2,
**conf['masknet'])
model = Model(enc, masker, dec)
optimizer = make_optimizer(model.parameters(), **conf['optim'])
return model, optimizer
import torch
import torch.nn as nn
from .enc_dec import Filterbank
class FreeFB(Filterbank):
""" Free filterbank without any constraints. Equivalent to
:class:`nn.Conv1d`.
Args:
n_filters (int): Number of filters.
kernel_size (int): Length of the filters.
stride (int, optional): Stride of the convolution.
If None (default), set to ``kernel_size // 2``.
Attributes:
n_feats_out (int): Number of output filters.
References:
[1] : "Filterbank design for end-to-end speech separation".
Submitted to ICASSP 2020. Manuel Pariente, Samuele Cornell,
Antoine Deleforge, Emmanuel Vincent.
import numpy as np
import torch
import torch.nn as nn
import warnings
from .enc_dec import Filterbank
class ParamSincFB(Filterbank):
"""Extension of the parameterized filterbank from [1] proposed in [2].
Modified and extended from from ``__
Args:
n_filters (int): Number of filters. Half of `n_filters` (the real
parts) will have parameters, the other half will correspond to the
imaginary parts. `n_filters` should be even.
kernel_size (int): Length of the filters.
stride (int, optional): Stride of the convolution. If None (default),
set to ``kernel_size // 2``.
sample_rate (int, optional): The sample rate (used for initialization).
min_low_hz (int, optional): Lowest low frequency allowed (Hz).
min_band_hz (int, optional): Lowest band frequency allowed (Hz).
Attributes:
n_feats_out (int): Number of output filters.
import torch
import numpy as np
from .enc_dec import Filterbank
class STFTFB(Filterbank):
""" STFT filterbank.
Args:
n_filters (int): Number of filters. Determines the length of the STFT
filters before windowing.
kernel_size (int): Length of the filters (i.e the window).
stride (int, optional): Stride of the convolution (hop size). If None
(default), set to ``kernel_size // 2``.
window (:class:`numpy.ndarray`, optional): If None, defaults to
``np.sqrt(np.hanning())``.
Attributes:
n_feats_out (int): Number of output filters.
"""
def __init__(self, n_filters, kernel_size, stride=None, window=None,
**kwargs):