arg_parser.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import argparse
  15. from ast import literal_eval
  16. def parse_hifigan_args(parent, add_help=False):
  17. """
  18. Parse model specific commandline arguments.
  19. """
  20. parser = argparse.ArgumentParser(parents=[parent], add_help=add_help,
  21. allow_abbrev=False)
  22. hfg = parser.add_argument_group('HiFi-GAN generator parameters')
  23. hfg.add_argument('--upsample_rates', default=[8, 8, 2, 2],
  24. type=literal_eval_arg,
  25. help='Upsample rates')
  26. hfg.add_argument('--upsample_kernel_sizes', default=[16, 16, 4, 4],
  27. type=literal_eval_arg,
  28. help='Upsample kernel sizes')
  29. hfg.add_argument('--upsample_initial_channel', default=512, type=int,
  30. help='Upsample initial channel')
  31. hfg.add_argument('--resblock', default='1', type=str,
  32. help='Resblock module version')
  33. hfg.add_argument('--resblock_kernel_sizes', default=[3, 7, 11],
  34. type=literal_eval_arg,
  35. help='Resblock kernel sizes')
  36. hfg.add_argument('--resblock_dilation_sizes', type=literal_eval_arg,
  37. default=[[1, 3, 5], [1, 3, 5], [1, 3, 5]],
  38. help='Resblock dilation sizes'),
  39. hfg = parser.add_argument_group('HiFi-GAN discriminator parameters')
  40. hfg.add_argument('--mpd_periods', default=[2, 3, 5, 7, 11],
  41. type=literal_eval_arg,
  42. help='Periods of MultiPeriodDiscriminator')
  43. hfg.add_argument('--concat_fwd', action='store_true',
  44. help='Faster Discriminators (requires more GPU memory)')
  45. hfg.add_argument('--hifigan-config', type=str, default=None, required=False,
  46. help='Path to a HiFi-GAN config .json'
  47. ' (if provided, overrides model architecture flags)')
  48. return parser
  49. def literal_eval_arg(val):
  50. try:
  51. return literal_eval(val)
  52. except SyntaxError as e: # Argparse does not handle SyntaxError
  53. raise ValueError(str(e)) from e