Spaces:
Runtime error
Runtime error
Delete network_wrapper.py
Browse files- network_wrapper.py +0 -176
network_wrapper.py
DELETED
|
@@ -1,176 +0,0 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
import yamlargparse
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
|
| 5 |
-
class network_wrapper(nn.Module):
|
| 6 |
-
"""
|
| 7 |
-
A wrapper class for loading different neural network models for tasks such as
|
| 8 |
-
speech enhancement (SE), speech separation (SS), and target speaker extraction (TSE).
|
| 9 |
-
It manages argument parsing, model configuration loading, and model instantiation
|
| 10 |
-
based on the task and model name.
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
def __init__(self):
|
| 14 |
-
"""
|
| 15 |
-
Initializes the network wrapper without any predefined model or arguments.
|
| 16 |
-
"""
|
| 17 |
-
super(network_wrapper, self).__init__()
|
| 18 |
-
self.args = None # Placeholder for command-line arguments
|
| 19 |
-
self.config_path = None # Path to the YAML configuration file
|
| 20 |
-
self.model_name = None # Model name to be loaded based on the task
|
| 21 |
-
|
| 22 |
-
def load_args_se(self):
|
| 23 |
-
"""
|
| 24 |
-
Loads the arguments for the speech enhancement task using a YAML config file.
|
| 25 |
-
Sets the configuration path and parses all the required parameters such as
|
| 26 |
-
input/output paths, model settings, and FFT parameters.
|
| 27 |
-
"""
|
| 28 |
-
self.config_path = 'config/inference/' + self.model_name + '.yaml'
|
| 29 |
-
parser = yamlargparse.ArgumentParser("Settings")
|
| 30 |
-
|
| 31 |
-
# General model and inference settings
|
| 32 |
-
parser.add_argument('--config', help='Config file path', action=yamlargparse.ActionConfigFile)
|
| 33 |
-
parser.add_argument('--mode', type=str, default='inference', help='Modes: train or inference')
|
| 34 |
-
parser.add_argument('--checkpoint-dir', dest='checkpoint_dir', type=str, default='checkpoints/FRCRN_SE_16K', help='Checkpoint directory')
|
| 35 |
-
parser.add_argument('--input-path', dest='input_path', type=str, help='Path for noisy audio input')
|
| 36 |
-
parser.add_argument('--output-dir', dest='output_dir', type=str, help='Directory for enhanced audio output')
|
| 37 |
-
parser.add_argument('--use-cuda', dest='use_cuda', default=1, type=int, help='Enable CUDA (1=True, 0=False)')
|
| 38 |
-
parser.add_argument('--num-gpu', dest='num_gpu', type=int, default=1, help='Number of GPUs to use')
|
| 39 |
-
|
| 40 |
-
# Model-specific settings
|
| 41 |
-
parser.add_argument('--network', type=str, help='Select SE models: FRCRN_SE_16K, MossFormer2_SE_48K')
|
| 42 |
-
parser.add_argument('--sampling-rate', dest='sampling_rate', type=int, default=16000, help='Sampling rate')
|
| 43 |
-
parser.add_argument('--one-time-decode-length', dest='one_time_decode_length', type=int, default=60, help='Max segment length for one-pass decoding')
|
| 44 |
-
parser.add_argument('--decode-window', dest='decode_window', type=int, default=1, help='Decoding chunk size')
|
| 45 |
-
|
| 46 |
-
# FFT parameters for feature extraction
|
| 47 |
-
parser.add_argument('--window-len', dest='win_len', type=int, default=400, help='Window length for framing')
|
| 48 |
-
parser.add_argument('--window-inc', dest='win_inc', type=int, default=100, help='Window shift for framing')
|
| 49 |
-
parser.add_argument('--fft-len', dest='fft_len', type=int, default=512, help='FFT length for feature extraction')
|
| 50 |
-
parser.add_argument('--num-mels', dest='num_mels', type=int, default=60, help='Number of mel-spectrogram bins')
|
| 51 |
-
parser.add_argument('--window-type', dest='win_type', type=str, default='hamming', help='Window type: hamming or hanning')
|
| 52 |
-
|
| 53 |
-
# Parse arguments from the config file
|
| 54 |
-
self.args = parser.parse_args(['--config', self.config_path])
|
| 55 |
-
|
| 56 |
-
def load_args_ss(self):
|
| 57 |
-
"""
|
| 58 |
-
Loads the arguments for the speech separation task using a YAML config file.
|
| 59 |
-
This method sets parameters such as input/output paths, model configurations,
|
| 60 |
-
and encoder/decoder settings for the MossFormer2-based speech separation model.
|
| 61 |
-
"""
|
| 62 |
-
self.config_path = 'config/inference/' + self.model_name + '.yaml'
|
| 63 |
-
parser = yamlargparse.ArgumentParser("Settings")
|
| 64 |
-
|
| 65 |
-
# General model and inference settings
|
| 66 |
-
parser.add_argument('--config', default=self.config_path, help='Config file path', action=yamlargparse.ActionConfigFile)
|
| 67 |
-
parser.add_argument('--mode', type=str, default='inference', help='Modes: train or inference')
|
| 68 |
-
parser.add_argument('--checkpoint-dir', dest='checkpoint_dir', type=str, default='checkpoints/FRCRN_SE_16K', help='Checkpoint directory')
|
| 69 |
-
parser.add_argument('--input-path', dest='input_path', type=str, help='Path for mixed audio input')
|
| 70 |
-
parser.add_argument('--output-dir', dest='output_dir', type=str, help='Directory for separated audio output')
|
| 71 |
-
parser.add_argument('--use-cuda', dest='use_cuda', default=1, type=int, help='Enable CUDA (1=True, 0=False)')
|
| 72 |
-
parser.add_argument('--num-gpu', dest='num_gpu', type=int, default=1, help='Number of GPUs to use')
|
| 73 |
-
|
| 74 |
-
# Model-specific settings for speech separation
|
| 75 |
-
parser.add_argument('--network', type=str, help='Select SS models: MossFormer2_SS_16K')
|
| 76 |
-
parser.add_argument('--sampling-rate', dest='sampling_rate', type=int, default=16000, help='Sampling rate')
|
| 77 |
-
parser.add_argument('--num-spks', dest='num_spks', type=int, default=2, help='Number of speakers to separate')
|
| 78 |
-
parser.add_argument('--one-time-decode-length', dest='one_time_decode_length', type=int, default=60, help='Max segment length for one-pass decoding')
|
| 79 |
-
parser.add_argument('--decode-window', dest='decode_window', type=int, default=1, help='Decoding chunk size')
|
| 80 |
-
|
| 81 |
-
# Encoder settings
|
| 82 |
-
parser.add_argument('--encoder_kernel-size', dest='encoder_kernel_size', type=int, default=16, help='Kernel size for Conv1D encoder')
|
| 83 |
-
parser.add_argument('--encoder-embedding-dim', dest='encoder_embedding_dim', type=int, default=512, help='Embedding dimension from encoder')
|
| 84 |
-
|
| 85 |
-
# MossFormer model parameters
|
| 86 |
-
parser.add_argument('--mossformer-squence-dim', dest='mossformer_sequence_dim', type=int, default=512, help='Sequence dimension for MossFormer')
|
| 87 |
-
parser.add_argument('--num-mossformer_layer', dest='num_mossformer_layer', type=int, default=24, help='Number of MossFormer layers')
|
| 88 |
-
|
| 89 |
-
# Parse arguments from the config file
|
| 90 |
-
self.args = parser.parse_args(['--config', self.config_path])
|
| 91 |
-
|
| 92 |
-
def load_args_tse(self):
|
| 93 |
-
"""
|
| 94 |
-
Loads the arguments for the target speaker extraction (TSE) task using a YAML config file.
|
| 95 |
-
Parameters include input/output paths, CUDA configurations, and decoding parameters.
|
| 96 |
-
"""
|
| 97 |
-
self.config_path = 'config/inference/' + self.model_name + '.yaml'
|
| 98 |
-
parser = yamlargparse.ArgumentParser("Settings")
|
| 99 |
-
|
| 100 |
-
# General model and inference settings
|
| 101 |
-
parser.add_argument('--config', default=self.config_path, help='Config file path', action=yamlargparse.ActionConfigFile)
|
| 102 |
-
parser.add_argument('--mode', type=str, default='inference', help='Modes: train or inference')
|
| 103 |
-
parser.add_argument('--checkpoint-dir', dest='checkpoint_dir', type=str, default='checkpoint_dir/AV_MossFormer2_TSE_16K', help='Checkpoint directory')
|
| 104 |
-
parser.add_argument('--input-path', dest='input_path', type=str, help='Path for mixed audio input')
|
| 105 |
-
parser.add_argument('--output-dir', dest='output_dir', type=str, help='Directory for separated audio output')
|
| 106 |
-
parser.add_argument('--use-cuda', dest='use_cuda', default=1, type=int, help='Enable CUDA (1=True, 0=False)')
|
| 107 |
-
parser.add_argument('--num-gpu', dest='num_gpu', type=int, default=1, help='Number of GPUs to use')
|
| 108 |
-
|
| 109 |
-
# Model-specific settings for target speaker extraction
|
| 110 |
-
parser.add_argument('--network', type=str, help='Select TSE models(currently supports AV_MossFormer2_TSE_16K)')
|
| 111 |
-
parser.add_argument('--sampling-rate', dest='sampling_rate', type=int, default=16000, help='Sampling rate (currently supports 16 kHz)')
|
| 112 |
-
parser.add_argument('--network_reference', type=dict, help='a dictionary that contains the parameters of auxilary reference signal')
|
| 113 |
-
parser.add_argument('--network_audio', type=dict, help='a dictionary that contains the network parameters')
|
| 114 |
-
|
| 115 |
-
# Decode parameters for streaming or chunk-based decoding
|
| 116 |
-
parser.add_argument('--one-time-decode-length', dest='one_time_decode_length', type=int, default=60, help='Max segment length for one-pass decoding')
|
| 117 |
-
parser.add_argument('--decode-window', dest='decode_window', type=int, default=1, help='Chunk length for streaming')
|
| 118 |
-
|
| 119 |
-
# Parse arguments from the config file
|
| 120 |
-
self.args = parser.parse_args(['--config', self.config_path])
|
| 121 |
-
|
| 122 |
-
def __call__(self, task, model_name):
|
| 123 |
-
"""
|
| 124 |
-
Calls the appropriate argument-loading function based on the task type
|
| 125 |
-
(e.g., 'speech_enhancement', 'speech_separation', or 'target_speaker_extraction').
|
| 126 |
-
It then loads the corresponding model based on the selected task and model name.
|
| 127 |
-
|
| 128 |
-
Args:
|
| 129 |
-
- task (str): The task type ('speech_enhancement', 'speech_separation', 'target_speaker_extraction').
|
| 130 |
-
- model_name (str): The name of the model to load (e.g., 'FRCRN_SE_16K').
|
| 131 |
-
|
| 132 |
-
Returns:
|
| 133 |
-
- self.network: The instantiated neural network model.
|
| 134 |
-
"""
|
| 135 |
-
|
| 136 |
-
self.model_name = model_name # Set the model name based on user input
|
| 137 |
-
|
| 138 |
-
# Load arguments specific to the task
|
| 139 |
-
if task == 'speech_enhancement':
|
| 140 |
-
self.load_args_se() # Load arguments for speech enhancement
|
| 141 |
-
elif task == 'speech_separation':
|
| 142 |
-
self.load_args_ss() # Load arguments for speech separation
|
| 143 |
-
elif task == 'target_speaker_extraction':
|
| 144 |
-
self.load_args_tse() # Load arguments for target speaker extraction
|
| 145 |
-
else:
|
| 146 |
-
# Print error message if the task is unsupported
|
| 147 |
-
print(f'{task} is not supported, please select from: '
|
| 148 |
-
'speech_enhancement, speech_separation, or target_speaker_extraction')
|
| 149 |
-
return
|
| 150 |
-
|
| 151 |
-
print(self.args) # Display the parsed arguments
|
| 152 |
-
self.args.task = task
|
| 153 |
-
self.args.network = self.model_name # Set the network name to the model name
|
| 154 |
-
|
| 155 |
-
# Initialize the corresponding network based on the selected model
|
| 156 |
-
if self.args.network == 'FRCRN_SE_16K':
|
| 157 |
-
from networks import CLS_FRCRN_SE_16K
|
| 158 |
-
self.network = CLS_FRCRN_SE_16K(self.args) # Load FRCRN model
|
| 159 |
-
elif self.args.network == 'MossFormer2_SE_48K':
|
| 160 |
-
from networks import CLS_MossFormer2_SE_48K
|
| 161 |
-
self.network = CLS_MossFormer2_SE_48K(self.args) # Load MossFormer2 model
|
| 162 |
-
elif self.args.network == 'MossFormerGAN_SE_16K':
|
| 163 |
-
from networks import CLS_MossFormerGAN_SE_16K
|
| 164 |
-
self.network = CLS_MossFormerGAN_SE_16K(self.args) # Load MossFormerGAN model
|
| 165 |
-
elif self.args.network == 'MossFormer2_SS_16K':
|
| 166 |
-
from networks import CLS_MossFormer2_SS_16K
|
| 167 |
-
self.network = CLS_MossFormer2_SS_16K(self.args) # Load MossFormer2 for separation
|
| 168 |
-
elif self.args.network == 'AV_MossFormer2_TSE_16K':
|
| 169 |
-
from networks import CLS_AV_MossFormer2_TSE_16K
|
| 170 |
-
self.network = CLS_AV_MossFormer2_TSE_16K(self.args) # Load AV MossFormer2 model for target speaker extraction
|
| 171 |
-
else:
|
| 172 |
-
# Print error message if no matching network is found
|
| 173 |
-
print("No network found!")
|
| 174 |
-
return
|
| 175 |
-
|
| 176 |
-
return self.network # Return the instantiated network model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|