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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. """Training process"""
  16. import os
  17. from mindspore import nn
  18. from mindspore import context
  19. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
  20. from mindspore.train import Model
  21. from mindspore.nn.metrics import Accuracy
  22. from src.moxing_adapter import moxing_wrapper
  23. from src.config import config
  24. from src.dataset import create_lenet_dataset
  25. from src.foo import LeNet5
  26. @moxing_wrapper()
  27. def train_lenet5():
  28. """
  29. Train lenet5
  30. """
  31. config.ckpt_path = config.output_path
  32. context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
  33. ds_train = create_lenet_dataset(os.path.join(config.data_path, "train"), config.batch_size, num_parallel_workers=1)
  34. if ds_train.get_dataset_size() == 0:
  35. raise ValueError("Please check dataset size > 0 and batch_size <= dataset size")
  36. network = LeNet5(config.num_classes)
  37. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  38. net_opt = nn.Momentum(network.trainable_params(), config.lr, config.momentum)
  39. time_cb = TimeMonitor(data_size=ds_train.get_dataset_size())
  40. config_ck = CheckpointConfig(save_checkpoint_steps=config.save_checkpoint_steps,
  41. keep_checkpoint_max=config.keep_checkpoint_max)
  42. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_lenet",
  43. directory=None if config.ckpt_path == "" else config.ckpt_path,
  44. config=config_ck)
  45. if config.device_target != "Ascend":
  46. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  47. else:
  48. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()}, amp_level="O2")
  49. print("============== Starting Training ==============")
  50. model.train(config.epoch_size, ds_train, callbacks=[time_cb, ckpoint_cb, LossMonitor()])
  51. if __name__ == '__main__':
  52. train_lenet5()