Spaces:
Sleeping
Sleeping
File size: 9,306 Bytes
c42fe7e |
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
import os
import pathlib
import re
import sys
from typing import List
import click
import torch
root_dir = pathlib.Path(__file__).resolve().parent.parent
os.environ['PYTHONPATH'] = str(root_dir)
sys.path.insert(0, str(root_dir))
from utils.hparams import set_hparams, hparams
def check_pytorch_version():
# A past version required PyTorch 1.13.x, but I am overriding this requirement and hoping it works anyway
print(f'| WARNING: Using PyTorch {torch.__version__}, export.py expects 1.13.x')
def find_exp(exp):
if not (root_dir / 'checkpoints' / exp).exists():
for subdir in (root_dir / 'checkpoints').iterdir():
if not subdir.is_dir():
continue
if subdir.name.startswith(exp):
print(f'| match ckpt by prefix: {subdir.name}')
exp = subdir.name
break
else:
raise click.BadParameter(
f'There are no matching exp starting with \'{exp}\' in \'checkpoints\' folder. '
'Please specify \'--exp\' as the folder name or prefix.'
)
else:
print(f'| found ckpt by name: {exp}')
return exp
def parse_spk_settings(export_spk, freeze_spk):
if export_spk is None:
export_spk = []
else:
export_spk = list(export_spk)
from utils.infer_utils import parse_commandline_spk_mix
spk_name_pattern = r'[0-9A-Za-z_-]+'
export_spk_mix = []
for spk in export_spk:
assert '=' in spk or '|' not in spk, \
'You must specify an alias with \'NAME=\' for each speaker mix.'
if '=' in spk:
alias, mix = spk.split('=', maxsplit=1)
assert re.fullmatch(spk_name_pattern, alias) is not None, f'Invalid alias \'{alias}\' for speaker mix.'
export_spk_mix.append((alias, parse_commandline_spk_mix(mix)))
else:
export_spk_mix.append((spk, {spk: 1.0}))
freeze_spk_mix = None
if freeze_spk is not None:
assert '=' in freeze_spk or '|' not in freeze_spk, \
'You must specify an alias with \'NAME=\' for each speaker mix.'
if '=' in freeze_spk:
alias, mix = freeze_spk.split('=', maxsplit=1)
assert re.fullmatch(spk_name_pattern, alias) is not None, f'Invalid alias \'{alias}\' for speaker mix.'
freeze_spk_mix = (alias, parse_commandline_spk_mix(mix))
else:
freeze_spk_mix = (freeze_spk, {freeze_spk: 1.0})
return export_spk_mix, freeze_spk_mix
@click.group()
def main():
pass
@main.command(help='Export DiffSinger acoustic model to ONNX format.')
@click.option(
'--exp', type=click.STRING,
required=True, metavar='EXP', callback=lambda ctx, param, value: find_exp(value),
help='Choose an experiment to export.'
)
@click.option(
'--ckpt', type=click.IntRange(min=0),
required=False, metavar='STEPS',
help='Checkpoint training steps.'
)
@click.option(
'--out', type=click.Path(
dir_okay=True, file_okay=False,
path_type=pathlib.Path, resolve_path=True
),
required=False,
help='Output directory for the artifacts.'
)
@click.option(
'--freeze_gender', type=click.FloatRange(min=-1, max=1),
help='(for random pitch shifting) Freeze gender value into the model.'
)
@click.option(
'--freeze_velocity', is_flag=True,
help='(for random time stretching) Freeze default velocity value into the model.'
)
@click.option(
'--export_spk', type=click.STRING,
required=False, multiple=True,
help='(for multi-speaker models) Export one or more speaker or speaker mixture keys.'
)
@click.option(
'--freeze_spk', type=click.STRING,
required=False,
help='(for multi-speaker models) Freeze one speaker or speaker mixture into the model.'
)
def acoustic(
exp: str,
ckpt: int = None,
out: pathlib.Path = None,
freeze_gender: float = 0.,
freeze_velocity: bool = False,
export_spk: List[str] = None,
freeze_spk: str = None
):
# Validate arguments
if export_spk and freeze_spk:
print('--export_spk is exclusive to --freeze_spk.')
exit(-1)
if out is None:
out = root_dir / 'artifacts' / exp
export_spk_mix, freeze_spk_mix = parse_spk_settings(export_spk, freeze_spk)
# Load configurations
sys.argv = [
sys.argv[0],
'--exp_name',
exp,
'--infer'
]
set_hparams()
# Export artifacts
from deployment.exporters import DiffSingerAcousticExporter
print(f'| Exporter: {DiffSingerAcousticExporter}')
exporter = DiffSingerAcousticExporter(
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'),
cache_dir=root_dir / 'deployment' / 'cache',
ckpt_steps=ckpt,
freeze_gender=freeze_gender,
freeze_velocity=freeze_velocity,
export_spk=export_spk_mix,
freeze_spk=freeze_spk_mix
)
try:
exporter.export(out)
except KeyboardInterrupt:
exit(-1)
@main.command(help='Export DiffSinger variance model to ONNX format.')
@click.option(
'--exp', type=click.STRING,
required=True, metavar='EXP', callback=lambda ctx, param, value: find_exp(value),
help='Choose an experiment to export.'
)
@click.option(
'--ckpt', type=click.IntRange(min=0),
required=False, metavar='STEPS',
help='Checkpoint training steps.'
)
@click.option(
'--out', type=click.Path(
dir_okay=True, file_okay=False,
path_type=pathlib.Path, resolve_path=True
),
required=False,
help='Output directory for the artifacts.'
)
@click.option(
'--freeze_glide', is_flag=True,
help='Freeze default glide embedding into the model.'
)
@click.option(
'--freeze_expr', is_flag=True,
help='Freeze default pitch expressiveness factor into the model.'
)
@click.option(
'--export_spk', type=click.STRING,
required=False, multiple=True,
help='(for multi-speaker models) Export one or more speaker or speaker mixture keys.'
)
@click.option(
'--freeze_spk', type=click.STRING,
required=False,
help='(for multi-speaker models) Freeze one speaker or speaker mixture into the model.'
)
def variance(
exp: str,
ckpt: int = None,
out: str = None,
freeze_glide: bool = False,
freeze_expr: bool = False,
export_spk: List[str] = None,
freeze_spk: str = None
):
# Validate arguments
if export_spk and freeze_spk:
print('--export_spk is exclusive to --freeze_spk.')
exit(-1)
if out is None:
out = root_dir / 'artifacts' / exp
export_spk_mix, freeze_spk_mix = parse_spk_settings(export_spk, freeze_spk)
# Load configurations
sys.argv = [
sys.argv[0],
'--exp_name',
exp,
'--infer'
]
set_hparams()
from deployment.exporters import DiffSingerVarianceExporter
print(f'| Exporter: {DiffSingerVarianceExporter}')
exporter = DiffSingerVarianceExporter(
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'),
cache_dir=root_dir / 'deployment' / 'cache',
ckpt_steps=ckpt,
freeze_glide=freeze_glide,
freeze_expr=freeze_expr,
export_spk=export_spk_mix,
freeze_spk=freeze_spk_mix
)
try:
exporter.export(out)
except KeyboardInterrupt:
exit(-1)
@main.command(help='Export NSF-HiFiGAN vocoder model to ONNX format.')
@click.option(
'--config', type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True,
path_type=pathlib.Path, resolve_path=True
),
required=True,
help='Specify a configuration file for the vocoder.'
)
@click.option(
'--ckpt', type=click.Path(
exists=True, file_okay=True, dir_okay=False, readable=True,
path_type=pathlib.Path, resolve_path=True
),
required=False,
help='Specify a model path of the vocoder checkpoint.'
)
@click.option(
'--out', type=click.Path(
dir_okay=True, file_okay=False,
path_type=pathlib.Path, resolve_path=True
),
required=False,
help='Output directory for the artifacts.'
)
@click.option(
'--name', type=click.STRING,
required=False, default='nsf_hifigan', show_default=False,
help='Specify filename (without suffix) of the target model file.'
)
def nsf_hifigan(
config: pathlib.Path,
ckpt: pathlib.Path = None,
out: pathlib.Path = None,
name: str = None
):
# Check arguments
if out is None:
out = root_dir / 'artifacts' / 'nsf_hifigan'
# Load configurations
set_hparams(config.as_posix())
if ckpt is None:
model_path = pathlib.Path(hparams['vocoder_ckpt']).resolve()
else:
model_path = ckpt
# Export artifacts
from deployment.exporters import NSFHiFiGANExporter
print(f'| Exporter: {NSFHiFiGANExporter}')
exporter = NSFHiFiGANExporter(
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'),
cache_dir=root_dir / 'deployment' / 'cache',
model_path=model_path,
model_name=name
)
try:
exporter.export(out)
except KeyboardInterrupt:
exit(-1)
if __name__ == '__main__':
check_pytorch_version()
main()
|