### GET /models/config Source: https://context7.com/alibabasglab/mossformer2/llms.txt Retrieves the configuration parameters for specific pretrained MossFormer2 models. ```APIDOC ## GET /models/config ### Description Returns the architecture configuration for supported models like Libri2Mix, WSJ0-3mix, or WHAMR!. ### Method GET ### Endpoint /models/config ### Parameters #### Query Parameters - **config_name** (string) - Required - The identifier for the model configuration (e.g., 'mossformer2-librimix-2spk'). ### Response #### Success Response (200) - **config** (object) - The model hyperparameters including kernel size, channels, and masknet settings. #### Response Example { "model_type": "mossformer2", "sample_rate": 8000, "masknet_numspks": 2, "intra_numlayers": 24 } ``` -------------------------------- ### Perform Batch Inference with PyTorch Source: https://context7.com/alibabasglab/mossformer2/llms.txt Demonstrates loading a pre-trained Mossformer2 wrapper and processing a batch of audio files using custom resampling logic. ```python model = Mossformer2Wrapper.from_pretrained('alibabasglab/mossformer2-librimix-2spk') model.eval() def load_audio(filepath, target_sr=8000): waveform, sr = torchaudio.load(filepath) if sr != target_sr: waveform = torchaudio.transforms.Resample(sr, target_sr)(waveform) return waveform mixed_files = ['mix1.wav', 'mix2.wav', 'mix3.wav'] batch = torch.stack([load_audio(f) for f in mixed_files]).squeeze(1) ``` -------------------------------- ### Initialize Speech Separation Pipeline (Python) Source: https://github.com/alibabasglab/mossformer2/blob/main/README.md This code snippet demonstrates how to initialize the speech separation pipeline using ModelScope. It sets up the task and specifies the model to be used for inference. The pipeline handles pre-processing and post-processing of WAV files. ```Python import numpy import soundfile as sf from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks speech_separation = pipeline(Tasks.speech_separation, model='damo/speech_mossformer2_separation_temporal_8k') ``` -------------------------------- ### Run Batch Inference and Save Separated Audio with MossFormer2 (PyTorch) Source: https://context7.com/alibabasglab/mossformer2/llms.txt This code snippet demonstrates how to run batch inference using a PyTorch model, likely MossFormer2, and save the separated audio streams. It iterates through the batch output, normalizes each speaker's signal to prevent clipping, and saves them as individual WAV files. Dependencies include PyTorch and torchaudio. ```python with torch.no_grad(): separated_batch = model.forward(batch.to(model.device)) print(f"Processed {separated_batch.shape[0]} files") print(f"Each file has {separated_batch.shape[2]} separated speakers") for i in range(separated_batch.shape[0]): for spk in range(separated_batch.shape[2]): signal = separated_batch[i, :, spk] signal = signal / signal.abs().max() # Normalize torchaudio.save(f'output_file{i}_spk{spk}.wav', signal.unsqueeze(0).cpu(), 8000) ``` -------------------------------- ### Initialize MossFormer2 Decoder Source: https://context7.com/alibabasglab/mossformer2/llms.txt Configures the decoder component with specific channel and kernel parameters to reconstruct audio from encoded features. ```python decoder = Decoder(in_channels=512, out_channels=1, kernel_size=16, stride=8, bias=False) encoded_features = torch.randn(2, 512, 999) reconstructed = decoder(encoded_features) print(reconstructed.shape) ``` -------------------------------- ### Configure Dual_Path_Model Separation Network Source: https://context7.com/alibabasglab/mossformer2/llms.txt Sets up the main separation network using intra-chunk processing blocks to separate multiple speakers from mixed audio features. ```python intra_model = SBFLASHBlock_DualA(num_layers=24, d_model=512, nhead=8, d_ffn=1024, dropout=0, use_positional_encoding=True, norm_before=True) dual_path = Dual_Path_Model(in_channels=512, out_channels=512, intra_model=intra_model, num_layers=1, norm="ln", K=250, num_spks=2, skip_around_intra=True, linear_layer_after_inter_intra=False) encoded = torch.randn(1, 512, 2000) separated_masks = dual_path(encoded) print(separated_masks.shape) ``` -------------------------------- ### Deploy via ModelScope Pipeline Source: https://context7.com/alibabasglab/mossformer2/llms.txt Uses the ModelScope library to load a pre-trained speech separation model and process audio files automatically. ```python from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks separation = pipeline(Tasks.speech_separation, model='damo/speech_mossformer2_separation_temporal_8k') result = separation('https://modelscope.cn/api/v1/models/damo/speech_mossformer2_separation_temporal_8k/repo?Revision=master&FilePath=examples/mix_speech1.wav') for i, signal in enumerate(result['output_pcm_list']): save_file = f'output_spk{i}.wav' sf.write(save_file, numpy.frombuffer(signal, dtype=numpy.int16), 8000) ``` -------------------------------- ### MossFormer2 Model Configurations Source: https://context7.com/alibabasglab/mossformer2/llms.txt Defines configurations for MossFormer2, including parameters for different datasets like Libri2Mix, WSJ0-3mix, and WHAMR!. Each configuration specifies the model type, sample rate, number of speakers, and other architectural details. ```python # Configuration for Libri2Mix dataset (2 speakers) mossformer2_librimix_2spk = { 'model_type': "mossformer2", 'sample_rate': 8000, 'config_name': "mossformer2-librimix-2spk", 'encoder_kernel_size': 16, 'encoder_out_nchannels': 512, 'encoder_in_nchannels': 1, 'masknet_numspks': 2, 'masknet_chunksize': 250, 'masknet_numlayers': 1, 'masknet_norm': "ln", 'intra_numlayers': 24, 'intra_nhead': 8, 'intra_dffn': 1024, 'intra_dropout': 0, 'intra_use_positional': True, 'intra_norm_before': True, } # Configuration for WSJ0-3mix dataset (3 speakers) mossformer2_wsj0mix_3spk = { 'model_type': "mossformer2", 'sample_rate': 8000, 'config_name': "mossformer2-wsj0mix-3spk", 'masknet_numspks': 3, # Other parameters same as librimix config } # Configuration for WHAMR! dataset (2 speakers with reverberation) mossformer2_whamr_2spk = { 'model_type': "mossformer2", 'sample_rate': 8000, 'config_name': "mossformer2-whamr-2spk", 'masknet_numspks': 2, # Other parameters same as librimix config } ``` -------------------------------- ### Implement SBFLASHBlock Transformer Block Source: https://context7.com/alibabasglab/mossformer2/llms.txt Initializes the hybrid transformer block that combines FLASH attention and FSMN recurrent modules for sequence modeling. ```python transformer_block = SBFLASHBlock_DualA(num_layers=24, d_model=512, nhead=8, d_ffn=1024, dropout=0, use_positional_encoding=True, norm_before=True, activation="relu") sequence_input = torch.randn(10, 100, 512) output = transformer_block(sequence_input) print(output.shape) ``` -------------------------------- ### Perform Speech Separation with MossFormer2 (Python) Source: https://github.com/alibabasglab/mossformer2/blob/main/README.md This code snippet utilizes the ModelScope library to perform speech separation. It takes an audio input (URL or local path), processes it with the 'damo/speech_mossformer2_separation_temporal_8k' model, and saves the separated audio streams as WAV files. Dependencies include ModelScope, soundfile, and numpy. ```python from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks import soundfile as sf import numpy # input can be a URL or a local path input = 'https://modelscope.cn/api/v1/models/damo/speech_mossformer2_separation_temporal_8k/repo?Revision=master&FilePath=examples/mix_speech1.wav' separation = pipeline( Tasks.speech_separation, model='damo/speech_mossformer2_separation_temporal_8k') result = separation(input) for i, signal in enumerate(result['output_pcm_list']): save_file = f'output_spk{i}.wav' sf.write(save_file, numpy.frombuffer(signal, dtype=numpy.int16), 8000) ``` -------------------------------- ### Speech Separation using MossFormer2 Source: https://github.com/alibabasglab/mossformer2/blob/main/README.md This snippet demonstrates how to perform speech separation using the MossFormer2 model. It takes an audio input (URL or local path), processes it, and saves the separated audio streams to individual files. ```APIDOC ## POST /api/v1/models/damo/speech_mossformer2_separation_temporal_8k/invoke ### Description This endpoint performs speech separation on an audio input using the MossFormer2 model. It can handle audio inputs provided as URLs or local file paths. ### Method POST ### Endpoint /api/v1/models/damo/speech_mossformer2_separation_temporal_8k/invoke ### Parameters #### Request Body - **input** (string) - Required - The URL or local path to the input audio file. ### Request Example ```json { "input": "https://modelscope.cn/api/v1/models/damo/speech_mossformer2_separation_temporal_8k/repo?Revision=master&FilePath=examples/mix_speech1.wav" } ``` ### Response #### Success Response (200) - **output_pcm_list** (list of bytes) - A list containing the separated audio streams in PCM format. #### Response Example ```json { "output_pcm_list": [ "...binary data for speaker 1...", "...binary data for speaker 2..." ] } ``` ### Error Handling - **400 Bad Request**: If the input is invalid or the audio file cannot be processed. - **500 Internal Server Error**: If there is an issue with the model inference or server. ### Related Paper MossFormer2: Combining Transformer and RNN-Free Recurrent Network for Enhanced Time-Domain Monaural Speech Separation [https://arxiv.org/abs/2312.11825](https://arxiv.org/abs/2312.11825) ``` -------------------------------- ### POST /inference Source: https://context7.com/alibabasglab/mossformer2/llms.txt Performs speech separation on a provided audio file or tensor input using the MossFormer2 model. ```APIDOC ## POST /inference ### Description Processes a single-channel 8kHz audio signal to separate overlapping speech into individual speaker streams. ### Method POST ### Endpoint /inference ### Parameters #### Request Body - **audio_path** (string) - Required - Path to the .wav file (8kHz). - **output_dir** (string) - Required - Directory to save separated audio files. - **audio_tensor** (tensor) - Optional - Raw audio tensor [batch_size, num_samples] for direct processing. ### Request Example { "audio_path": "./input/mix.wav", "output_dir": "./output" } ### Response #### Success Response (200) - **separated_audio** (tensor) - The separated audio streams [batch_size, num_samples, num_speakers]. #### Response Example { "status": "success", "output_files": ["./output/index1.wav", "./output/index2.wav"] } ``` -------------------------------- ### Mossformer2Wrapper: Main Model Wrapper for Speech Separation Source: https://context7.com/alibabasglab/mossformer2/llms.txt The primary interface for the MossFormer2 speech separation model. This wrapper class combines the Encoder, Masknet, and Decoder components. It supports loading pretrained models from Hugging Face Hub and provides methods for tensor-based forward passes and file-based inference. ```python import torch from model.mossformer2 import Mossformer2Wrapper # Load pretrained model from Hugging Face Hub # Available configs: "mossformer2-librimix-2spk", "mossformer2-wsj0mix-3spk", "mossformer2-whamr-2spk" model = Mossformer2Wrapper.from_pretrained('alibabasglab/mossformer2-librimix-2spk') # Run inference on a WAV file (8kHz sample rate required) # Outputs separated speaker files to the specified directory model.inference('./test_samples/mossformer2-librimix-2spk/item0_mix.wav', './output') # Creates: ./output/index1.wav, ./output/index2.wav # For direct tensor processing (e.g., batch inference) # Input: [batch_size, num_samples] tensor at 8kHz mixed_audio = torch.rand(1, 80000) # 10 seconds of audio with torch.no_grad(): separated = model.forward(mixed_audio.to(model.device)) # Output shape: [batch_size, num_samples, num_speakers] print(separated.shape) # torch.Size([1, 80000, 2]) ``` -------------------------------- ### Encoder: Convolutional Audio Encoder for MossFormer2 Source: https://context7.com/alibabasglab/mossformer2/llms.txt The Encoder transforms raw audio waveforms into a higher-dimensional feature representation using 1D convolution. It converts time-domain signals into a latent space suitable for the dual-path processing pipeline. Key parameters include kernel size, output channels, and input channels. ```python import torch from model.utils.one_path_flash_fsmn import Encoder # Initialize encoder with standard MossFormer2 parameters encoder = Encoder( kernel_size=16, # Filter length (stride is kernel_size // 2) out_channels=512, # Output feature dimensions in_channels=1 # Mono audio input ) # Process raw audio waveform # Input shape: [batch_size, num_samples] audio_input = torch.randn(2, 8000) # 2 samples, 1 second each at 8kHz encoded = encoder(audio_input) # Output shape: [batch_size, out_channels, time_frames] print(encoded.shape) # torch.Size([2, 512, 999]) ``` -------------------------------- ### Decoder: Transposed Convolution Audio Decoder for MossFormer2 Source: https://context7.com/alibabasglab/mossformer2/llms.txt The Decoder reconstructs audio waveforms from the separated feature representations using transposed 1D convolution. It converts the masked latent representations back to time-domain audio signals, completing the speech separation process. ```python import torch from model.utils.one_path_flash_fsmn import Decoder # Decoder initialization would typically follow, similar to the Encoder. # Example placeholder: # decoder = Decoder(...) # reconstructed_audio = decoder(processed_features) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.