### example_xml_file function Source: https://nnsvs.github.io/_modules/nnsvs/util.html Retrieves the file path for an example XML file included in the package. ```python def example_xml_file(key="haruga_kita"): """Get the path to an included xml file. Args: key (str): key of the file Returns: str: path to an example xml file Raises: FileNotFoundError: if the file is not found """ return pkg_resources.resource_filename(__name__, join(EXAMPLE_DIR, f"{key}.xml")) ``` -------------------------------- ### example_xml_file(key='haruga_kita') Source: https://nnsvs.github.io/modules/generated/nnsvs.util.example_xml_file.html Returns the path to an included example XML file based on the provided key. ```APIDOC ## example_xml_file(key='haruga_kita') ### Description Get the path to an included xml file. ### Method Function ### Endpoint `nnsvs.util.example_xml_file(key='haruga_kita')` ### Parameters #### Path Parameters - **key** (str) - Required - key of the file ### Returns - **str** - path to an example xml file ### Raises - **FileNotFoundError** - if the file is not found ``` -------------------------------- ### Install NNSVS for development Source: https://nnsvs.github.io/_sources/installation.rst.txt Clones the repository and installs the package in editable mode with development, linting, and testing dependencies. ```bash git clone https://github.com/nnsvs/nnsvs.git && cd nnsvs pip install -e ".[dev,lint,test]" ``` -------------------------------- ### Start MLflow UI Source: https://nnsvs.github.io/_sources/optuna.rst.txt Launch the MLflow UI to visualize and track hyperparameter search results. ```bash mlflow ui --host 0.0.0.0 ``` -------------------------------- ### nnsvs.util.example_xml_file Source: https://nnsvs.github.io/modules/util.html Returns the path to an example XML file included with the package. ```APIDOC ## nnsvs.util.example_xml_file ### Description Get the path to an included xml file. ### Method N/A ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response #### Success Response - **path** (string) - Path to the xml file. #### Response Example N/A ``` -------------------------------- ### Verify HnSincNSF import from command line Source: https://nnsvs.github.io/_sources/train_vocoders.rst.txt Verify the installation from the command line by importing and printing HnSincNSF. Should throw no errors if the installation is done properly. ```bash python -c "from parallel_wavegan.models.nsf import HnSincNSF; print(HnSincNSF(1,1))" ``` -------------------------------- ### Install nnsvs if not already installed Source: https://nnsvs.github.io/notebooks/Demos.html Installs nnsvs from GitHub if the import fails. Uses %%capture to suppress output. ```python %%capture try: import nnsvs except ImportError: ! pip install git+https://github.com/nnsvs/nnsvs ``` -------------------------------- ### Specify a custom model in a new file Source: https://nnsvs.github.io/_sources/custom_models.rst.txt Configuration example for a model defined in a custom file, such as nnsvs/test.py. ```yaml netG: _target_: nnsvs.test.MyModel # the followings are arguments passed to your model's __init__ method in_dim: 331 hidden_dim: 32 out_dim: 1 ``` -------------------------------- ### Get note indices Source: https://nnsvs.github.io/_modules/nnsvs/io/hts.html Retrieves note start indices from HTS labels based on start times. ```python def get_note_indices(labels): """Get note start indices from HTS labels Args: labels (nnmnkwii.io.hts.HTSLabelFile): HTS labels Returns: list: Note start indices """ note_indices = [0] last_start_time = labels.start_times[0] for idx in range(1, len(labels)): if labels.start_times[idx] != last_start_time: note_indices.append(idx) last_start_time = labels.start_times[idx] else: pass return note_indices ``` -------------------------------- ### setup(config, device, collate_fn=collate_fn_default) Source: https://nnsvs.github.io/modules/generated/nnsvs.train_util.setup.html Sets up the training environment by creating the model, optimizer, learning rate scheduler, data loaders, tensorboard writer, logger, and scalers. ```APIDOC ## setup(config, device, collate_fn=collate_fn_default) ### Description Sets up the training environment by creating the model, optimizer, learning rate scheduler, data loaders, tensorboard writer, logger, and scalers. ### Method setup ### Parameters - **config** (dict) - Required - Configuration for training. - **device** (torch.device) - Required - Device to use for training. - **collate_fn** (callable, optional) - Optional - Collate function. Defaults to collate_fn_default. ### Returns - **tuple** - Tuple containing model, optimizer, learning rate scheduler, data loaders, tensorboard writer, logger, and scalers. ``` -------------------------------- ### get_note_indices(labels) Source: https://nnsvs.github.io/modules/generated/nnsvs.io.hts.get_note_indices.html Get note start indices from HTS labels. This function accepts an HTSLabelFile object and returns a list of note start indices. ```APIDOC ## get_note_indices(labels) ### Description Get note start indices from HTS labels. ### Parameters - **labels** (nnmnkwii.io.hts.HTSLabelFile) - Required - HTS labels ### Returns - **list** - Note start indices ``` -------------------------------- ### Setup training environment Source: https://nnsvs.github.io/_modules/nnsvs/train_util.html Initializes logging, random seeds, CUDA settings, mixed precision, model, and optimizer for training. ```python [docs] def setup(config, device, collate_fn=collate_fn_default): """Setup for training Args: config (dict): configuration for training device (torch.device): device to use for training collate_fn (callable, optional): collate function. Defaults to collate_fn_default. Returns: (tuple): tuple containing model, optimizer, learning rate scheduler, data loaders, tensorboard writer, logger, and scalers. """ if dist.is_initialized(): rank = dist.get_rank() logger = getLogger(config.verbose) if rank == 0 else getLogger(0) sys.stdout = open(os.devnull, "w") if rank != 0 else sys.stdout else: logger = getLogger(config.verbose) rank = 0 logger.info(OmegaConf.to_yaml(config)) logger.info(f"PyTorch version: {torch.__version__}") if torch.cuda.is_available(): from torch.backends import cudnn cudnn.benchmark = config.train.cudnn.benchmark cudnn.deterministic = config.train.cudnn.deterministic logger.info(f"cudnn.deterministic: {cudnn.deterministic}") logger.info(f"cudnn.benchmark: {cudnn.benchmark}") if torch.backends.cudnn.version() is not None: logger.info(f"cuDNN version: {torch.backends.cudnn.version()}") logger.info(f"Random seed: {config.seed}") init_seed(config.seed) if config.train.use_detect_anomaly: torch.autograd.set_detect_anomaly(True) logger.info("Set to use torch.autograd.detect_anomaly") if "use_amp" in config.train and config.train.use_amp: logger.info("Use mixed precision training") grad_scaler = GradScaler() else: grad_scaler = None # Model model = hydra.utils.instantiate(config.model.netG).to(device) logger.info(model) logger.info( "Number of trainable params: {:.3f} million".format( num_trainable_params(model) / 1000000.0 ) ) # Distributed training if dist.is_initialized(): device_id = rank % torch.cuda.device_count() model = DDP(model, device_ids=[device_id]) # Optimizer optimizer_class = getattr(optim, config.train.optim.optimizer.name) optimizer = optimizer_class( model.parameters(), **config.train.optim.optimizer.params ) # Scheduler lr_scheduler_class = getattr( optim.lr_scheduler, config.train.optim.lr_scheduler.name ) lr_scheduler = lr_scheduler_class( optimizer, **config.train.optim.lr_scheduler.params ) ``` -------------------------------- ### Install NNSVS on Google Colab Source: https://nnsvs.github.io/_sources/installation.rst.txt Checks for the existing installation and installs NNSVS from the GitHub repository if it is not found. ```python %%capture try: import nnsvs except ImportError: ! pip install git+https://github.com/nnsvs/nnsvs ``` -------------------------------- ### Install NNSVS development dependencies Source: https://nnsvs.github.io/_sources/optuna.rst.txt Install the required development dependencies for NNSVS. ```bash pip install -e ".[dev]" ``` -------------------------------- ### Run full recipe with CUDA_VISIBLE_DEVICES=0 Source: https://nnsvs.github.io/_sources/recipes.rst.txt Runs the entire recipe from stage 0 to stage 99 on GPU 0. Omit CUDA_VISIBLE_DEVICES if only one GPU is available. Stage -1 is reserved for optional data download and may require manual download after signing a contract. ```bash CUDA_VISIBLE_DEVICES=0 ./run.sh --stage 0 --stop-stage 99 ``` -------------------------------- ### Install hyperparameter optimization requirements Source: https://nnsvs.github.io/_sources/optuna.rst.txt Explicitly install the packages required for hyperparameter optimization. ```bash pip install mlflow optuna hydra-optuna-sweeper ``` -------------------------------- ### Pack models with custom models (stage 99) Source: https://nnsvs.github.io/_sources/recipes.rst.txt Run the model packaging step (stage 99) with custom model types. Specify the timelag, duration, and acoustic models as shown. ```bash CUDA_VISIBLE_DEVICES=0 ./run.sh --stage 99 --stop-stage 99 \ --timelag-model timelag_test \ --duration-model duration_test \ --acoustic-model acoustic_test ``` -------------------------------- ### Build documentation locally Source: https://nnsvs.github.io/_sources/devdocs.rst.txt Use sphinx-autobuild to generate and serve the documentation locally from the top of the nnsvs directory. ```bash sphinx-autobuild docs docs/_build/html/ ``` -------------------------------- ### Install NNSVS for inference Source: https://nnsvs.github.io/_sources/installation.rst.txt Installs the base NNSVS package for users who only need to run inference without training models. ```bash pip install nnsvs ``` -------------------------------- ### Verify HnSincNSF import in Jupyter Source: https://nnsvs.github.io/_sources/train_vocoders.rst.txt Verify the installation by importing HnSincNSF in a Jupyter notebook. Should throw no errors if the installation is done properly. ```python In [1]: from parallel_wavegan.models.nsf import HnSincNSF In [2]: HnSincNSF(1,1) ``` -------------------------------- ### Example of conf directory structure Source: https://nnsvs.github.io/recipes.html Shows the directory layout for model-specific configurations in a recipe, including subdirectories for duration, timelag, and acoustic models, each with data, model, and train configuration files. ```text conf ├── train │ ├── duration │ │ ├── data │ │ │ └── myconfig.yaml │ │ ├── model │ │ │ └── duration_mdn.yaml │ │ └── train │ │ └── myconfig.yaml │ └── timelag │ ├── data │ │ └── myconfig.yaml │ ├── model │ │ └── timelag_mdn.yaml │ └── train │ └── myconfig.yaml └── train_acoustic └── acoustic ├── data │ └── myconfig.yaml ├── model │ └── acoustic_resf0convlstm.yaml └── train └── myconfig.yaml ``` -------------------------------- ### Fix HTS label offset to zero Source: https://nnsvs.github.io/_modules/nnsvs/io/hts.html The fix_label_offset_to_zero function adjusts the start and end times of HTS labels so that the first label starts at zero. ```python def fix_label_offset_to_zero(labels): """Fix label offset to zero Args: labels (nnmnkwii.io.hts.HTSLabelFile): HTS labels Returns: nnmnkwii.io.hts.HTSLabelFile: HTS labels with fixed offset """ offset = labels.start_times[0] if offset > 0: labels.start_times = np.asarray(labels.start_times) - offset labels.end_times = np.asarray(labels.end_times) - offset return labels ``` -------------------------------- ### Pack models (stage 99) Source: https://nnsvs.github.io/_sources/recipes.rst.txt Run the model packaging step (stage 99) without custom models. This packs the trained models into a single directory. ```bash CUDA_VISIBLE_DEVICES=0 ./run.sh --stage 99 --stop-stage 99 ``` -------------------------------- ### Setup GAN Training Environment Source: https://nnsvs.github.io/_modules/nnsvs/train_util.html Configures the training environment for GAN models, including logging, device settings, mixed precision, and model instantiation for both generator and discriminator. ```python def setup_gan(config, device, collate_fn=collate_fn_default): """Setup for training GAN Args: config (dict): configuration for training device (torch.device): device to use for training collate_fn (callable, optional): collate function. Defaults to collate_fn_default. Returns: (tuple): tuple containing model, optimizer, learning rate scheduler, data loaders, tensorboard writer, logger, and scalers. """ if dist.is_initialized(): rank = dist.get_rank() logger = getLogger(config.verbose) if rank == 0 else getLogger(0) sys.stdout = open(os.devnull, "w") if rank != 0 else sys.stdout else: logger = getLogger(config.verbose) rank = 0 logger.info(OmegaConf.to_yaml(config)) logger.info(f"PyTorch version: {torch.__version__}") if torch.cuda.is_available(): from torch.backends import cudnn cudnn.benchmark = config.train.cudnn.benchmark cudnn.deterministic = config.train.cudnn.deterministic logger.info(f"cudnn.deterministic: {cudnn.deterministic}") logger.info(f"cudnn.benchmark: {cudnn.benchmark}") if torch.backends.cudnn.version() is not None: logger.info(f"cuDNN version: {torch.backends.cudnn.version()}") logger.info(f"Random seed: {config.seed}") init_seed(config.seed) if config.train.use_detect_anomaly: torch.autograd.set_detect_anomaly(True) logger.info("Set to use torch.autograd.detect_anomaly") if "use_amp" in config.train and config.train.use_amp: logger.info("Use mixed precision training") grad_scaler = GradScaler() else: grad_scaler = None # Model G netG = hydra.utils.instantiate(config.model.netG).to(device) logger.info(netG) logger.info( "[Generator] Number of trainable params: {:.3f} million".format( num_trainable_params(netG) / 1000000.0 ) ) if dist.is_initialized(): device_id = rank % torch.cuda.device_count() netG = DDP(netG, device_ids=[device_id]) # Optimizer and LR scheduler for G optG, schedulerG = _instantiate_optim(config.train.optim.netG, netG) # Model D netD = hydra.utils.instantiate(config.model.netD).to(device) logger.info(netD) logger.info( "[Discriminator] Number of trainable params: {:.3f} million".format( num_trainable_params(netD) / 1000000.0 ) ) if dist.is_initialized(): device_id = rank % torch.cuda.device_count() netD = DDP(netD, device_ids=[device_id]) # Optimizer and LR scheduler for D optD, schedulerD = _instantiate_optim(config.train.optim.netD, netD) # DataLoader data_loaders, samplers = get_data_loaders(config.data, collate_fn, logger) set_epochs_based_on_max_steps_( config.train, len(data_loaders["train_no_dev"]), logger ) # Resume _resume(logger, config.train.resume.netG, netG, optG, schedulerG) _resume(logger, config.train.resume.netD, netD, optD, schedulerD) if config.data_parallel: netG = nn.DataParallel(netG) netD = nn.DataParallel(netD) ``` -------------------------------- ### Install parallel_wavegan nnsvs branch Source: https://nnsvs.github.io/_sources/train_vocoders.rst.txt Install the nnsvs branch of the parallel_wavegan repository to enable neural vocoder support in NNSVS. This branch includes code for NSF. ```bash pip install git+https://github.com/r9y9/ParallelWaveGAN@nnsvs ``` -------------------------------- ### Install NNSVS in editable mode Source: https://nnsvs.github.io/_sources/devdocs.rst.txt Install the full requirements with editable mode enabled to make local changes available to the Python environment without manual re-installation. ```bash pip install -e ".[dev,lint,test,docs]" ``` -------------------------------- ### Run stage 7 to prepare post-filter features Source: https://nnsvs.github.io/_sources/train_postfilters.rst.txt Run stage 7 of the recipe to prepare input and output features for training post-filters. Requires a trained acoustic model; the acoustic model's checkpoint directory is specified with --acoustic-model. ```bash CUDA_VISIBLE_DEVICES=0 ./run.sh --stage 7 --stop-stage 7 \ --acoustic-model acoustic_test ``` -------------------------------- ### Initialize Training Components Source: https://nnsvs.github.io/_modules/nnsvs/train_util.html Initializes data loaders, model, optimizer, and logging infrastructure. Handles configuration for MLflow or Tensorboard and loads input/output scalers. ```python # DataLoader data_loaders, samplers = get_data_loaders(config.data, collate_fn, logger) set_epochs_based_on_max_steps_( config.train, len(data_loaders["train_no_dev"]), logger ) # Resume _resume(logger, config.train.resume, model, optimizer, lr_scheduler) if config.data_parallel: model = nn.DataParallel(model) # Mlflow if config.mlflow.enabled: mlflow.set_tracking_uri("file://" + get_original_cwd() + "/mlruns") mlflow.set_experiment(config.mlflow.experiment) # NOTE: disable tensorboard if mlflow is enabled writer = None logger.info("Using mlflow instead of tensorboard") else: # Tensorboard if rank == 0: writer = SummaryWriter(to_absolute_path(config.train.log_dir)) else: writer = None # Scalers if "in_scaler_path" in config.data and config.data.in_scaler_path is not None: in_scaler = joblib.load(to_absolute_path(config.data.in_scaler_path)) in_scaler = MinMaxScaler( in_scaler.min_, in_scaler.scale_, in_scaler.data_min_, in_scaler.data_max_ ) else: in_scaler = None if "out_scaler_path" in config.data and config.data.out_scaler_path is not None: out_scaler = joblib.load(to_absolute_path(config.data.out_scaler_path)) out_scaler = StandardScaler( out_scaler.mean_, out_scaler.var_, out_scaler.scale_ ) else: out_scaler = None return ( model, optimizer, lr_scheduler, grad_scaler, data_loaders, samplers, writer, logger, in_scaler, out_scaler, ) ``` -------------------------------- ### PredictionType.PROBABILISTIC usage Source: https://nnsvs.github.io/_modules/nnsvs/base.html Example usage for MDN-based models. ```python # training mdn_params = model(x) # inference mu, sigma = model.inference(x) ``` -------------------------------- ### PredictionType.DIFFUSION usage Source: https://nnsvs.github.io/_modules/nnsvs/base.html Example usage for diffusion model predictions. ```python # training noise, x_recon = model(x) # inference y = model.inference(x) ``` -------------------------------- ### List parallel_wavegan configurations Source: https://nnsvs.github.io/_sources/train_vocoders.rst.txt View the available model configuration files in the parallel_wavegan directory. ```bash $ tree conf/parallel_wavegan conf/parallel_wavegan ├── hn-sinc-nsf_sr24k_pwgD.yaml ├── hn-sinc-nsf_sr48k_hifiganD.yaml ├── hn-sinc-nsf_sr48k_pwgD.yaml └── hn-sinc-nsf_sr48k_pwgD_test.yaml ``` -------------------------------- ### PredictionType.DETERMINISTIC usage Source: https://nnsvs.github.io/_modules/nnsvs/base.html Example usage for non-MDN single-stream models. ```python # training y = model(x) # inference y = model.inference(x) ```