### Install HS-TasNet Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Install the HS-TasNet library using pip. This is the primary method for setting up the project. ```bash pip install HS-TasNet ``` -------------------------------- ### Start Training with Trainer Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Initiates the training process using the configured `Trainer` instance. This call starts the training loop. ```python trainer() ``` -------------------------------- ### Install Wandb for Experiment Tracking Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Installs the Weights & Biases library for experiment monitoring and logging, and logs in to the service. ```bash pip install wandb && wandb login ``` -------------------------------- ### Install uv for Package Management Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Installs `uv`, a fast Python package installer and resolver, which is recommended for managing project dependencies. ```bash pip install uv ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Installs the necessary dependencies for running tests, including optional test-specific packages, using `uv pip install`. ```bash uv pip install '.[test]' --system ``` -------------------------------- ### Install Training Dependencies Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Installs necessary dependencies for training by executing a shell script. This ensures all required packages are available. ```bash sh scripts/install.sh ``` -------------------------------- ### Run Training with Wandb Integration Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Starts the training process with Weights & Biases integration enabled using the `--use-wandb` flag. This allows for online monitoring of training metrics. ```bash uv run train.py --use-wandb ``` -------------------------------- ### Run Pytest Tests Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Executes the project's test suite using the `pytest` framework. Ensure all test dependencies are installed first. ```bash pytest tests ``` -------------------------------- ### Clear Folders for Fresh Training Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Appends the `--clear-folders` flag to the training command to remove previous checkpoints and evaluation results, ensuring a clean start for training. ```bash uv run train.py --use-wandb --clear-folders ``` -------------------------------- ### Initialize HSTasNet Model Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Configure and instantiate the model with various parameters for architecture size, audio channels, and processing settings. ```python import torch from hs_tasnet import HSTasNet # Initialize the default model (stereo, standard size) model = HSTasNet( dim=500, # Hidden dimension (default: 500) small=False, # Use small variant with fewer layers stereo=True, # Process stereo audio num_basis=1500, # Number of basis functions for waveform branch segment_len=1024, # Segment length for processing overlap_len=512, # Overlap length between segments n_fft=1024, # FFT size for spectrogram branch sample_rate=44100, # Audio sample rate num_sources=4, # Number of output sources (drums, bass, vocals, other) torch_compile=False, # Enable torch.compile for faster inference use_gru=False, # Use GRU instead of LSTM spec_branch_use_phase=True, # Include phase information in spectrogram branch norm_before_mask_estimate=True # Apply RMSNorm before mask estimation ) # Small model variant for faster inference model_small = HSTasNet(small=True, stereo=True) # Mono model model_mono = HSTasNet(stereo=False) print(f"Model parameters: {model.num_parameters}") ``` -------------------------------- ### Basic Audio Separation with HSTasNet Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Demonstrates how to initialize the HSTasNet model and perform audio separation on a random stereo audio tensor. Ensure torch is imported. ```python import torch from hs_tasnet import HSTasNet model = HSTasNet() audio = torch.randn(1, 2, 204800) # ~5 seconds of stereo separated_audios, _ = model(audio) assert separated_audios.shape == (1, 4, 2, 204800) # second dimension is the separated tracks ``` -------------------------------- ### Run Training Script Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Executes the main training script using `uv`. This command initiates the training process on a subset of MusDB, and the loss should decrease. ```bash uv run train.py ``` -------------------------------- ### Configure Distributed Training and Experiment Tracking Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Integrate Weights & Biases for tracking and use Accelerate for distributed training configurations. ```python from hs_tasnet import HSTasNet, Trainer model = HSTasNet() trainer = Trainer( model, dataset=None, concat_musdb_dataset=True, batch_size=16, max_steps=100000, # Experiment tracking with wandb use_wandb=True, experiment_project='HS-TasNet', experiment_run_name='stereo-full-musdb', experiment_hparams={'dim': 500, 'small': False}, # Accelerate settings for distributed training accelerate_kwargs={}, grad_accum_every=4 ) # Clear previous checkpoints and results trainer.clear_folders() # Start training (run `accelerate config` first for multi-GPU) trainer() ``` -------------------------------- ### Initialize and Configure Trainer Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Sets up the `Trainer` for HS-TasNet, allowing for dataset configuration, batch size, and training steps. `cpu=True` can be used for CPU-only training. ```python from hs_tasnet import HSTasNet, Trainer model = HSTasNet() # trainer trainer = Trainer( model, dataset = None, # add your in-house Dataset concat_musdb_dataset = True, # concat the musdb dataset automatically batch_size = 2, max_steps = 2, cpu = True, ) ``` -------------------------------- ### Initialize and Load Model from Checkpoint Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md A convenient method to initialize and load an HS-TasNet model directly from a checkpoint file, without needing to manually save hyperparameters. ```python model = HSTasNet.init_and_load_from('./checkpoints/path.to.desired.ckpt.pt') ``` -------------------------------- ### Stream Real-Time Audio with Sounddevice Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Process live audio streams or initialize a stateful transform function for continuous audio processing. ```python import torch from hs_tasnet import HSTasNet model = HSTasNet(small=True, stereo=True) model.eval() # Stream from microphone for 10 seconds, output only vocals and drums model.sounddevice_stream( return_reduced_sources=[0, 2], # [drums, vocals] indices duration_seconds=10, print_latency=True # Print processing latency for each chunk ) # Alternative: Get a stateful transform function for custom audio processing transform_fn = model.init_stateful_transform_fn( return_reduced_sources=[0, 2], device='cuda', # Process on GPU auto_convert_to_stereo=True, # Convert mono input to stereo print_latency=True ) # Process audio chunks (512 samples each, matching overlap_len) import numpy as np chunk = np.random.randn(2, 512).astype(np.float32) # (stereo, samples) output1 = transform_fn(chunk) # Returns numpy array output2 = transform_fn(chunk) # Maintains state between calls output3 = transform_fn(chunk) # Each output: (2, 512) numpy array ``` -------------------------------- ### Save and Load Model Checkpoints Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Save trained models including configuration and load them for inference or further training. ```python import torch from hs_tasnet import HSTasNet # Create and train a model model = HSTasNet(dim=512, small=True, stereo=True) # Save model with config model.save('./checkpoints/model.pt', overwrite=True) # Load weights into existing model model.load('./checkpoints/model.pt', strict=True) # Initialize model from checkpoint (automatically restores hyperparameters) restored_model = HSTasNet.init_and_load_from('./checkpoints/model.pt') # No need to specify dim, small, stereo, etc. - loaded from checkpoint ``` -------------------------------- ### Load Model from Checkpoint Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Loads a pre-trained HS-TasNet model from a specified checkpoint file path. This allows resuming training or performing inference with saved weights. ```python model.load('./checkpoints/path.to.desired.ckpt.pt') model.sounddevice_stream(...) ``` -------------------------------- ### Train with MusDB18 Dataset Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Configure the trainer to use the MusDB18 dataset, either via automatic download or a local path. ```python import musdb from hs_tasnet import HSTasNet, Trainer, MusDB18HQ model = HSTasNet(stereo=True) # Option 1: Use MusDB sample dataset (auto-download) trainer = Trainer( model, dataset=None, concat_musdb_dataset=True, # Automatically add MusDB use_full_musdb_dataset=False, # Use sample version batch_size=16, max_steps=50000, random_split_dataset_for_eval_frac=0.05 # Split 5% for validation ) # Option 2: Use full MusDB18-HQ from local path trainer = Trainer( model, dataset=None, concat_musdb_dataset=True, use_full_musdb_dataset=True, full_musdb_dataset_root='./data/musdb18hq', batch_size=16 ) # Option 3: Use MusDB18HQ dataset wrapper directly dataset = MusDB18HQ( dataset_path='./data/musdb18hq', sep_filenames=('drums', 'bass', 'vocals', 'other'), max_audio_length_seconds=10 # Random crop to 10 seconds ) trainer = Trainer(model, dataset=dataset, batch_size=16) trainer() ``` -------------------------------- ### Save Spectrogram Visualizations Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Generate and save spectrogram images for debugging and analysis. ```python import torch from hs_tasnet import HSTasNet model = HSTasNet() ``` -------------------------------- ### Perform Forward Pass and Source Separation Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Execute inference on audio tensors or perform training steps with target supervision and variable-length masking. ```python import torch from hs_tasnet import HSTasNet model = HSTasNet(stereo=True) # Inference: Separate audio into 4 sources audio = torch.randn(1, 2, 204800) # (batch, stereo_channels, samples) ~5 seconds at 44.1kHz separated_audios, hiddens = model(audio) # separated_audios shape: (batch=1, sources=4, stereo=2, samples=204800) drums = separated_audios[:, 0] # (1, 2, 204800) bass = separated_audios[:, 1] # (1, 2, 204800) vocals = separated_audios[:, 2] # (1, 2, 204800) other = separated_audios[:, 3] # (1, 2, 204800) # Return only specific sources summed together (e.g., vocals + drums) vocals_and_drums, _ = model(audio, return_reduced_sources=[0, 2]) # vocals_and_drums shape: (1, 2, 204800) # Training mode with targets and loss computation targets = torch.randn(1, 4, 2, 204800) # (batch, sources, stereo, samples) loss = model(audio, targets=targets) loss.backward() # Variable length audio with masking batch_audio = torch.randn(4, 2, 204800) audio_lens = torch.tensor([204800, 180000, 150000, 100000]) loss = model(batch_audio, targets=torch.randn(4, 4, 2, 204800), audio_lens=audio_lens) ``` -------------------------------- ### Inferencing with EMA Model Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Utilizes the exponentially moving average (EMA) model from the trainer for inference, which often provides more stable results. The `sounddevice_stream` method is called on this EMA model. ```python trainer.ema_model.sounddevice_stream(...) ``` -------------------------------- ### Process Audio Files with HS-TasNet Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Use the process_audio_file method to separate audio sources from files, supporting automatic resampling and format conversion. ```python from hs_tasnet import HSTasNet model = HSTasNet() model.eval() # Process an audio file and save separated output model.process_audio_file( input_file='./input_song.mp3', return_reduced_sources=[2], # Extract vocals only output_file='./vocals_only.mp3', auto_convert_to_stereo=True, overwrite=True ) # Extract instrumental (everything except vocals) model.process_audio_file( input_file='./input_song.wav', return_reduced_sources=[0, 1, 3], # drums + bass + other output_file='./instrumental.mp3' ) ``` -------------------------------- ### Real-time Inference with sounddevice_stream Source: https://github.com/lucidrains/hs-tasnet/blob/main/README.md Performs real-time audio separation using `sounddevice_stream`. Specify `duration_seconds` and optionally `return_reduced_sources` to control the output. ```python model.sounddevice_stream( duration_seconds = 2, return_reduced_sources = [0, 2] ) ``` -------------------------------- ### Save Spectrogram Visualization Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Generates and saves a spectrogram image from an audio tensor. Supports both mono and stereo inputs. ```python audio = torch.randn(44100 * 5) # 5 seconds mono audio model.save_spectrogram_figure( save_path='./spectrogram.png', audio=audio, figsize=(10, 4), overwrite=True ) ``` ```python # Works with stereo (averages channels) stereo_audio = torch.randn(2, 44100 * 5) model.save_spectrogram_figure('./stereo_spec.png', stereo_audio, overwrite=True) ``` -------------------------------- ### Train Models with Trainer Class Source: https://context7.com/lucidrains/hs-tasnet/llms.txt The Trainer class manages the training pipeline, including dataset handling, EMA, and learning rate scheduling. ```python import torch from torch.utils.data import Dataset from hs_tasnet import HSTasNet, Trainer # Custom dataset returning (audio, targets) pairs class MusicDataset(Dataset): def __len__(self): return 1000 def __getitem__(self, idx): audio = torch.randn(2, 44100 * 5) # 5 seconds stereo audio targets = torch.randn(4, 2, 44100 * 5) # 4 separated sources return audio, targets model = HSTasNet(stereo=True) trainer = Trainer( model, dataset=MusicDataset(), batch_size=16, max_epochs=20, max_steps=50000, learning_rate=3e-4, checkpoint_every=1, checkpoint_folder='./checkpoints', eval_results_folder='./eval-results', use_ema=True, ema_decay=0.995, cpu=False, # Use GPU decay_lr_if_not_improved_steps=3, early_stop_if_not_improved_steps=10, augment_gain=True, # Apply gain augmentation augment_remix=True, # Apply remix augmentation augment_remix_frac=0.5 ) # Start training trainer() # Access EMA model for inference trainer.ema_model.sounddevice_stream( duration_seconds=5, return_reduced_sources=[2] ) ``` -------------------------------- ### Export Separated Audio Tensor Source: https://context7.com/lucidrains/hs-tasnet/llms.txt Saves a processed audio tensor to a file, useful for exporting specific source tracks like vocals. ```python # Save separated audio to file separated, _ = model(stereo_audio.unsqueeze(0)) vocals = separated[0, 2] # Get vocals track model.save_tensor_to_file('./vocals.mp3', vocals, overwrite=True, verbose=True) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.