Spaces:
Running
Running
File size: 10,199 Bytes
88b57c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# Third-party
import torch
import torch.nn as nn
# Local
from src.Sound_Feature_Extraction.short_time_fourier_transform import STFT
COMPUTATION_DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
class Conv_TDF(nn.Module):
"""
Convolutional Time-Domain Filter (TDF) Module.
Args:
c (int): The number of input and output channels for the convolutional layers.
l (int): The number of convolutional layers within the module.
f (int): The number of features (or units) in the time-domain filter.
k (int): The size of the convolutional kernels (filters).
bn (int or None): Batch normalization factor (controls TDF behavior). If None, TDF is not used.
bias (bool): A boolean flag indicating whether bias terms are included in the linear layers.
Attributes:
use_tdf (bool): Flag indicating whether TDF is used.
Methods:
forward(x): Forward pass through the TDF module.
"""
def __init__(self, c, l, f, k, bn, bias=True):
super(Conv_TDF, self).__init__()
# Determine whether to use TDF (Time-Domain Filter)
self.use_tdf = bn is not None
# Define a list of convolutional layers within the module
self.H = nn.ModuleList()
for i in range(l):
self.H.append(
nn.Sequential(
nn.Conv2d(
in_channels=c,
out_channels=c,
kernel_size=k,
stride=1,
padding=k // 2,
),
nn.GroupNorm(2, c),
nn.ReLU(),
)
)
# Define the Time-Domain Filter (TDF) layers if enabled
if self.use_tdf:
if bn == 0:
self.tdf = nn.Sequential(
nn.Linear(f, f, bias=bias), nn.GroupNorm(2, c), nn.ReLU()
)
else:
self.tdf = nn.Sequential(
nn.Linear(f, f // bn, bias=bias),
nn.GroupNorm(2, c),
nn.ReLU(),
nn.Linear(f // bn, f, bias=bias),
nn.GroupNorm(2, c),
nn.ReLU(),
)
def forward(self, x):
# Apply the convolutional layers sequentially
for h in self.H:
x = h(x)
# Apply the Time-Domain Filter (TDF) if enabled, and add the result to the orignal input
return x + self.tdf(x) if self.use_tdf else x
class Conv_TDF_net_trimm(nn.Module):
"""
Convolutional Time-Domain Filter (TDF) Network with Trimming.
Args:
L (int): This parameter controls the number of down-sampling (DS) blocks in the network.
It's divided by 2 to determine how many DS blocks should be created.
l (int): This parameter represents the number of convolutional layers (or filters) within each dense (fully connected) block.
g (int): This parameter specifies the number of output channels for the first convolutional layer and is also used to determine the number of channels for subsequent layers in the network.
dim_f (int): This parameter represents the number of frequency bins (spectrogram columns) in the input audio data.
dim_t (int): This parameter represents the number of time frames (spectrogram rows) in the input audio data.
k (int): This parameter specifies the size of convolutional kernels (filters) used in the network's convolutional layers.
bn (int or None): This parameter controls whether batch normalization is used in the network.
If it's None, batch normalization may or may not be used based on other conditions in the code.
bias (bool): This parameter is a boolean flag that controls whether bias terms are included in the convolutional layers.
overlap (int): This parameter specifies the amount of overlap between consecutive chunks of audio data during processing.
Attributes:
n (int): The calculated number of down-sampling (DS) blocks.
dim_f (int): The number of frequency bins (spectrogram columns) in the input audio data.
dim_t (int): The number of time frames (spectrogram rows) in the input audio data.
n_fft (int): The size of the Fast Fourier Transform (FFT) window.
hop (int): The hop size used in the STFT calculations.
n_bins (int): The number of bins in the frequency domain.
chunk_size (int): The size of each chunk of audio data.
target_name (str): The name of the target instrument being separated.
overlap (int): The amount of overlap between consecutive chunks of audio data during processing.
Methods:
forward(x): Forward pass through the Conv_TDF_net_trimm network.
"""
def __init__(
self,
model_path,
use_onnx,
target_name,
L,
l,
g,
dim_f,
dim_t,
k=3,
hop=1024,
bn=None,
bias=True,
overlap=1500,
):
super(Conv_TDF_net_trimm, self).__init__()
# Dictionary specifying the scale for the number of FFT bins for different target names
n_fft_scale = {"vocals": 3, "*": 2}
# Number of input and output channels for the initial and final convolutional layers
out_c = in_c = 4
# Number of down-sampling (DS) blocks
self.n = L // 2
# Dimensions of the frequency and time axes of the input data
self.dim_f = 3072
self.dim_t = 256
# Number of FFT bins (frequencies) and hop size for the Short-Time Fourier Transform (STFT)
self.n_fft = 7680
self.hop = hop
self.n_bins = self.n_fft // 2 + 1
# Chunk size used for processing
self.chunk_size = hop * (self.dim_t - 1)
# Target name for the model
self.target_name = target_name
# Overlap between consecutive chunks of audio data during processing
self.overlap = overlap
# STFT module for audio processing
self.stft = STFT(self.n_fft, self.hop, self.dim_f)
# Check if ONNX representation of the model should be used
if not use_onnx:
# First convolutional layer
self.first_conv = nn.Sequential(
nn.Conv2d(in_channels=in_c, out_channels=g, kernel_size=1, stride=1),
nn.BatchNorm2d(g),
nn.ReLU(),
)
# Initialize variables for dense (fully connected) blocks and downsampling (DS) blocks
f = self.dim_f
c = g
self.ds_dense = nn.ModuleList()
self.ds = nn.ModuleList()
# Loop through down-sampling (DS) blocks
for i in range(self.n):
# Create dense (fully connected) block for down-sampling
self.ds_dense.append(Conv_TDF(c, l, f, k, bn, bias=bias))
# Create down-sampling (DS) block
scale = (2, 2)
self.ds.append(
nn.Sequential(
nn.Conv2d(
in_channels=c,
out_channels=c + g,
kernel_size=scale,
stride=scale,
),
nn.BatchNorm2d(c + g),
nn.ReLU(),
)
)
f = f // 2
c += g
# Middle dense (fully connected block)
self.mid_dense = Conv_TDF(c, l, f, k, bn, bias=bias)
# If batch normalization is not specified and mid_tdf is True, use Conv_TDF with bn=0 and bias=False
if bn is None and mid_tdf:
self.mid_dense = Conv_TDF(c, l, f, k, bn=0, bias=False)
# Initialize variables for up-sampling (US) blocks
self.us_dense = nn.ModuleList()
self.us = nn.ModuleList()
# Loop through up-sampling (US) blocks
for i in range(self.n):
scale = (2, 2)
# Create up-sampling (US) block
self.us.append(
nn.Sequential(
nn.ConvTranspose2d(
in_channels=c,
out_channels=c - g,
kernel_size=scale,
stride=scale,
),
nn.BatchNorm2d(c - g),
nn.ReLU(),
)
)
f = f * 2
c -= g
# Create dense (fully connected) block for up-sampling
self.us_dense.append(Conv_TDF(c, l, f, k, bn, bias=bias))
# Final convolutional layer
self.final_conv = nn.Sequential(
nn.Conv2d(in_channels=c, out_channels=out_c, kernel_size=1, stride=1),
)
try:
# Load model state from a file
self.load_state_dict(
torch.load(
f"{model_path}/{target_name}.pt",
map_location=COMPUTATION_DEVICE,
)
)
print(f"Loading model ({target_name})")
except FileNotFoundError:
print(f"Random init ({target_name})")
def forward(self, x):
"""
Forward pass through the Conv_TDF_net_trimm network.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor after passing through the network.
"""
x = self.first_conv(x)
x = x.transpose(-1, -2)
ds_outputs = []
for i in range(self.n):
x = self.ds_dense[i](x)
ds_outputs.append(x)
x = self.ds[i](x)
x = self.mid_dense(x)
for i in range(self.n):
x = self.us[i](x)
x *= ds_outputs[-i - 1]
x = self.us_dense[i](x)
x = x.transpose(-1, -2)
x = self.final_conv(x)
return x
|