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.

train.py 6.3 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. """train_criteo."""
  16. import os
  17. from os.path import join
  18. import json
  19. import argparse
  20. from warnings import warn
  21. from hparams import hparams, hparams_debug_string
  22. from mindspore import context, Tensor
  23. from mindspore.context import ParallelMode
  24. from mindspore.communication.management import init, get_rank, get_group_size
  25. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
  26. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  27. from mindspore.nn.optim import Adam
  28. from mindspore.nn import TrainOneStepCell
  29. from mindspore.train import Model
  30. from src.lr_generator import get_lr
  31. from src.dataset import get_data_loaders
  32. from src.loss import NetWithLossClass
  33. from src.callback import Monitor
  34. from wavenet_vocoder import WaveNet
  35. from wavenet_vocoder.util import is_mulaw_quantize, is_scalar_input
  36. parser = argparse.ArgumentParser(description='TTS training')
  37. parser.add_argument('--data_path', type=str, required=True, default='',
  38. help='Directory contains preprocessed features.')
  39. parser.add_argument('--preset', type=str, required=True, default='', help='Path of preset parameters (json).')
  40. parser.add_argument('--checkpoint_dir', type=str, default='./checkpoints_test',
  41. help='Directory where to save model checkpoints [default: checkpoints].')
  42. parser.add_argument('--checkpoint', type=str, default='', help='Restore model from checkpoint path if given.')
  43. parser.add_argument('--speaker_id', type=str, default='',
  44. help=' Use specific speaker of data in case for multi-speaker datasets.')
  45. parser.add_argument('--is_distributed', action="store_true", default=False, help='Distributed training')
  46. args = parser.parse_args()
  47. if __name__ == '__main__':
  48. if args.is_distributed:
  49. init('nccl')
  50. rank_id = get_rank()
  51. group_size = get_group_size()
  52. context.set_context(mode=context.GRAPH_MODE, device_target="GPU", save_graphs=False)
  53. context.reset_auto_parallel_context()
  54. context.set_auto_parallel_context(device_num=get_group_size(), parallel_mode=ParallelMode.DATA_PARALLEL,
  55. gradients_mean=True)
  56. else:
  57. context.set_context(mode=context.GRAPH_MODE, device_target="GPU", save_graphs=False)
  58. rank_id = 0
  59. group_size = 1
  60. speaker_id = int(args.speaker_id) if args.speaker_id != '' else None
  61. if args.preset is not None:
  62. with open(args.preset) as f:
  63. hparams.parse_json(f.read())
  64. assert hparams.name == "wavenet_vocoder"
  65. print(hparams_debug_string())
  66. fs = hparams.sample_rate
  67. os.makedirs(args.checkpoint_dir, exist_ok=True)
  68. output_json_path = join(args.checkpoint_dir, "hparams.json")
  69. with open(output_json_path, "w") as f:
  70. json.dump(hparams.values(), f, indent=2)
  71. data_loaders = get_data_loaders(args.data_path, args.speaker_id, hparams=hparams, rank_id=rank_id,
  72. group_size=group_size)
  73. step_size_per_epoch = data_loaders.get_dataset_size()
  74. if is_mulaw_quantize(hparams.input_type):
  75. if hparams.out_channels != hparams.quantize_channels:
  76. raise RuntimeError(
  77. "out_channels must equal to quantize_chennels if input_type is 'mulaw-quantize'")
  78. if hparams.upsample_conditional_features and hparams.cin_channels < 0:
  79. s = "Upsample conv layers were specified while local conditioning disabled. "
  80. s += "Notice that upsample conv layers will never be used."
  81. warn(s)
  82. upsample_params = hparams.upsample_params
  83. upsample_params["cin_channels"] = hparams.cin_channels
  84. upsample_params["cin_pad"] = hparams.cin_pad
  85. model = WaveNet(
  86. out_channels=hparams.out_channels,
  87. layers=hparams.layers,
  88. stacks=hparams.stacks,
  89. residual_channels=hparams.residual_channels,
  90. gate_channels=hparams.gate_channels,
  91. skip_out_channels=hparams.skip_out_channels,
  92. cin_channels=hparams.cin_channels,
  93. gin_channels=hparams.gin_channels,
  94. n_speakers=hparams.n_speakers,
  95. dropout=hparams.dropout,
  96. kernel_size=hparams.kernel_size,
  97. cin_pad=hparams.cin_pad,
  98. upsample_conditional_features=hparams.upsample_conditional_features,
  99. upsample_params=upsample_params,
  100. scalar_input=is_scalar_input(hparams.input_type),
  101. output_distribution=hparams.output_distribution,
  102. )
  103. loss_net = NetWithLossClass(model, hparams)
  104. lr = get_lr(hparams.optimizer_params["lr"], hparams.nepochs, step_size_per_epoch)
  105. lr = Tensor(lr)
  106. if args.checkpoint != '':
  107. param_dict = load_checkpoint(args.pre_trained_model_path)
  108. load_param_into_net(model, param_dict)
  109. print('Successfully loading the pre-trained model')
  110. weights = model.trainable_params()
  111. optimizer = Adam(weights, learning_rate=lr, loss_scale=1024.)
  112. train_net = TrainOneStepCell(loss_net, optimizer)
  113. model = Model(train_net)
  114. lr_cb = Monitor(lr)
  115. callback_list = [lr_cb]
  116. if args.is_distributed:
  117. ckpt_path = os.path.join(args.checkpoint_dir, 'ckpt_' + str(get_rank()) + '/')
  118. else:
  119. ckpt_path = args.checkpoint_dir
  120. config_ck = CheckpointConfig(save_checkpoint_steps=step_size_per_epoch, keep_checkpoint_max=10)
  121. ckpt_cb = ModelCheckpoint(prefix='wavenet', directory=ckpt_path, config=config_ck)
  122. callback_list.append(ckpt_cb)
  123. model.train(hparams.nepochs, data_loaders, callbacks=callback_list)