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 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2020 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. """cnnctc train"""
  16. import argparse
  17. import ast
  18. import mindspore
  19. from mindspore import context
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  21. from mindspore.dataset import GeneratorDataset
  22. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig
  23. from mindspore.train.model import Model
  24. from mindspore.communication.management import init
  25. from mindspore.common import set_seed
  26. from src.config import Config_CNNCTC
  27. from src.callback import LossCallBack
  28. from src.dataset import ST_MJ_Generator_batch_fixed_length, ST_MJ_Generator_batch_fixed_length_para
  29. from src.cnn_ctc import CNNCTC_Model, ctc_loss, WithLossCell
  30. set_seed(1)
  31. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False,
  32. save_graphs_path=".", enable_auto_mixed_precision=False)
  33. def dataset_creator(run_distribute):
  34. if run_distribute:
  35. st_dataset = ST_MJ_Generator_batch_fixed_length_para()
  36. else:
  37. st_dataset = ST_MJ_Generator_batch_fixed_length()
  38. ds = GeneratorDataset(st_dataset,
  39. ['img', 'label_indices', 'text', 'sequence_length'],
  40. num_parallel_workers=8)
  41. return ds
  42. def train(args_opt, config):
  43. if args_opt.run_distribute:
  44. init()
  45. context.set_auto_parallel_context(parallel_mode="data_parallel")
  46. ds = dataset_creator(args_opt.run_distribute)
  47. net = CNNCTC_Model(config.NUM_CLASS, config.HIDDEN_SIZE, config.FINAL_FEATURE_WIDTH)
  48. net.set_train(True)
  49. if config.CKPT_PATH != '':
  50. param_dict = load_checkpoint(config.CKPT_PATH)
  51. load_param_into_net(net, param_dict)
  52. print('parameters loaded!')
  53. else:
  54. print('train from scratch...')
  55. criterion = ctc_loss()
  56. opt = mindspore.nn.RMSProp(params=net.trainable_params(), centered=True, learning_rate=config.LR_PARA,
  57. momentum=config.MOMENTUM, loss_scale=config.LOSS_SCALE)
  58. net = WithLossCell(net, criterion)
  59. loss_scale_manager = mindspore.train.loss_scale_manager.FixedLossScaleManager(config.LOSS_SCALE, False)
  60. model = Model(net, optimizer=opt, loss_scale_manager=loss_scale_manager, amp_level="O2")
  61. callback = LossCallBack()
  62. config_ck = CheckpointConfig(save_checkpoint_steps=config.SAVE_CKPT_PER_N_STEP,
  63. keep_checkpoint_max=config.KEEP_CKPT_MAX_NUM)
  64. ckpoint_cb = ModelCheckpoint(prefix="CNNCTC", config=config_ck, directory=config.SAVE_PATH)
  65. if args_opt.run_distribute:
  66. if args_opt.device_id == 0:
  67. model.train(config.TRAIN_EPOCHS, ds, callbacks=[callback, ckpoint_cb], dataset_sink_mode=False)
  68. else:
  69. model.train(config.TRAIN_EPOCHS, ds, callbacks=[callback], dataset_sink_mode=False)
  70. else:
  71. model.train(config.TRAIN_EPOCHS, ds, callbacks=[callback, ckpoint_cb], dataset_sink_mode=False)
  72. if __name__ == '__main__':
  73. parser = argparse.ArgumentParser(description='CNNCTC arg')
  74. parser.add_argument('--device_id', type=int, default=0, help="Device id, default is 0.")
  75. parser.add_argument("--ckpt_path", type=str, default="", help="Pretrain file path.")
  76. parser.add_argument("--run_distribute", type=ast.literal_eval, default=False,
  77. help="Run distribute, default is false.")
  78. args_cfg = parser.parse_args()
  79. cfg = Config_CNNCTC()
  80. if args_cfg.ckpt_path != "":
  81. cfg.CKPT_PATH = args_cfg.ckpt_path
  82. train(args_cfg, cfg)