You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

export.py 4.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """export mindir."""
  16. import json
  17. from os.path import join
  18. import argparse
  19. from warnings import warn
  20. import numpy as np
  21. from hparams import hparams, hparams_debug_string
  22. from mindspore import context, Tensor
  23. from mindspore.train.serialization import load_checkpoint, load_param_into_net, export
  24. from wavenet_vocoder import WaveNet
  25. from wavenet_vocoder.util import is_mulaw_quantize, is_scalar_input
  26. from src.loss import PredictNet
  27. parser = argparse.ArgumentParser(description='TTS training')
  28. parser.add_argument('--preset', type=str, default='', help='Path of preset parameters (json).')
  29. parser.add_argument('--checkpoint_dir', type=str, default='./checkpoints_test',
  30. help='Directory where to save model checkpoints [default: checkpoints].')
  31. parser.add_argument('--speaker_id', type=str, default='',
  32. help=' Use specific speaker of data in case for multi-speaker datasets.')
  33. parser.add_argument('--pretrain_ckpt', type=str, default='', help='Pretrained checkpoint path')
  34. parser.add_argument('--platform', type=str, default='GPU', choices=('GPU', 'CPU'),
  35. help='run platform, support GPU and CPU. Default: GPU')
  36. args = parser.parse_args()
  37. if __name__ == '__main__':
  38. context.set_context(mode=context.GRAPH_MODE, device_target=args.platform, save_graphs=False)
  39. speaker_id = int(args.speaker_id) if args.speaker_id != '' else None
  40. if args.preset is not None:
  41. with open(args.preset) as f:
  42. hparams.parse_json(f.read())
  43. assert hparams.name == "wavenet_vocoder"
  44. print(hparams_debug_string())
  45. fs = hparams.sample_rate
  46. output_json_path = join(args.checkpoint_dir, "hparams.json")
  47. with open(output_json_path, "w") as f:
  48. json.dump(hparams.values(), f, indent=2)
  49. if is_mulaw_quantize(hparams.input_type):
  50. if hparams.out_channels != hparams.quantize_channels:
  51. raise RuntimeError(
  52. "out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
  53. if hparams.upsample_conditional_features and hparams.cin_channels < 0:
  54. s = "Upsample conv layers were specified while local conditioning disabled. "
  55. s += "Notice that upsample conv layers will never be used."
  56. warn(s)
  57. upsample_params = hparams.upsample_params
  58. upsample_params["cin_channels"] = hparams.cin_channels
  59. upsample_params["cin_pad"] = hparams.cin_pad
  60. model = WaveNet(
  61. out_channels=hparams.out_channels,
  62. layers=hparams.layers,
  63. stacks=hparams.stacks,
  64. residual_channels=hparams.residual_channels,
  65. gate_channels=hparams.gate_channels,
  66. skip_out_channels=hparams.skip_out_channels,
  67. cin_channels=hparams.cin_channels,
  68. gin_channels=hparams.gin_channels,
  69. n_speakers=hparams.n_speakers,
  70. dropout=hparams.dropout,
  71. kernel_size=hparams.kernel_size,
  72. cin_pad=hparams.cin_pad,
  73. upsample_conditional_features=hparams.upsample_conditional_features,
  74. upsample_params=upsample_params,
  75. scalar_input=is_scalar_input(hparams.input_type),
  76. output_distribution=hparams.output_distribution,
  77. )
  78. Net = PredictNet(model)
  79. Net.set_train(False)
  80. param_dict = load_checkpoint(args.pretrain_ckpt)
  81. load_param_into_net(model, param_dict)
  82. print('Successfully loading the pre-trained model')
  83. if is_mulaw_quantize(hparams.input_type):
  84. x = np.array(np.random.random((2, 256, 10240)), dtype=np.float32)
  85. else:
  86. x = np.array(np.random.random((2, 1, 10240)), dtype=np.float32)
  87. c = np.array(np.random.random((2, 80, 44)), dtype=np.float32)
  88. g = np.array([0, 0], dtype=np.int64)
  89. export(Net, Tensor(x), Tensor(c), Tensor(g), file_name="WaveNet", file_format='MINDIR')