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.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. """
  16. ######################## train alexnet example ########################
  17. train alexnet and get network model files(.ckpt) :
  18. python train.py --data_path /YourDataPath
  19. """
  20. import argparse
  21. from config import alexnet_cfg as cfg
  22. from dataset import create_dataset
  23. import mindspore.nn as nn
  24. from mindspore import context
  25. from mindspore.train import Model
  26. from mindspore.nn.metrics import Accuracy
  27. from mindspore.model_zoo.alexnet import AlexNet
  28. from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor
  29. if __name__ == "__main__":
  30. parser = argparse.ArgumentParser(description='MindSpore AlexNet Example')
  31. parser.add_argument('--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU'],
  32. help='device where the code will be implemented (default: Ascend)')
  33. parser.add_argument('--data_path', type=str, default="./", help='path where the dataset is saved')
  34. parser.add_argument('--ckpt_path', type=str, default="./ckpt", help='if is test, must provide\
  35. path where the trained ckpt file')
  36. parser.add_argument('--dataset_sink_mode', type=bool, default=False, help='dataset_sink_mode is False or True')
  37. args = parser.parse_args()
  38. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, enable_mem_reuse=False)
  39. network = AlexNet(cfg.num_classes)
  40. loss = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True, reduction="mean")
  41. opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum)
  42. model = Model(network, loss, opt, metrics={"Accuracy": Accuracy()}) # test
  43. print("============== Starting Training ==============")
  44. ds_train = create_dataset(args.data_path,
  45. cfg.batch_size,
  46. cfg.epoch_size,
  47. "train")
  48. config_ck = CheckpointConfig(save_checkpoint_steps=cfg.save_checkpoint_steps,
  49. keep_checkpoint_max=cfg.keep_checkpoint_max)
  50. ckpoint_cb = ModelCheckpoint(prefix="checkpoint_alexnet", directory=args.ckpt_path, config=config_ck)
  51. model.train(cfg.epoch_size, ds_train, callbacks=[ckpoint_cb, LossMonitor()],
  52. dataset_sink_mode=args.dataset_sink_mode)