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.

eval.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. eval.
  17. """
  18. import os
  19. import argparse
  20. import random
  21. import numpy as np
  22. from dataset import create_dataset
  23. from config import config
  24. from mindspore import context
  25. from mindspore.model_zoo.resnet import resnet101
  26. from mindspore.parallel._auto_parallel_context import auto_parallel_context
  27. from mindspore.train.model import Model, ParallelMode
  28. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  29. import mindspore.dataset.engine as de
  30. from mindspore.communication.management import init
  31. from crossentropy import CrossEntropy
  32. random.seed(1)
  33. np.random.seed(1)
  34. de.config.set_seed(1)
  35. parser = argparse.ArgumentParser(description='Image classification')
  36. parser.add_argument('--run_distribute', type=bool, default=False, help='Run distribute')
  37. parser.add_argument('--device_num', type=int, default=1, help='Device num.')
  38. parser.add_argument('--do_train', type=bool, default=False, help='Do train or not.')
  39. parser.add_argument('--do_eval', type=bool, default=True, help='Do eval or not.')
  40. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  41. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  42. args_opt = parser.parse_args()
  43. device_id = int(os.getenv('DEVICE_ID'))
  44. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False, device_id=device_id)
  45. context.set_context(enable_task_sink=True)
  46. context.set_context(enable_loop_sink=True)
  47. context.set_context(enable_mem_reuse=True)
  48. if __name__ == '__main__':
  49. if args_opt.do_eval:
  50. context.set_context(enable_hccl=False)
  51. else:
  52. if args_opt.run_distribute:
  53. context.set_context(enable_hccl=True)
  54. context.set_auto_parallel_context(device_num=args_opt.device_num, parallel_mode=ParallelMode.DATA_PARALLEL,
  55. mirror_mean=True, parameter_broadcast=True)
  56. auto_parallel_context().set_all_reduce_fusion_split_indices([180, 313])
  57. init()
  58. else:
  59. context.set_context(enable_hccl=False)
  60. epoch_size = config.epoch_size
  61. net = resnet101(class_num=config.class_num)
  62. if not config.label_smooth:
  63. config.label_smooth_factor = 0.0
  64. loss = CrossEntropy(smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  65. if args_opt.do_eval:
  66. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=config.batch_size)
  67. step_size = dataset.get_dataset_size()
  68. if args_opt.checkpoint_path:
  69. param_dict = load_checkpoint(args_opt.checkpoint_path)
  70. load_param_into_net(net, param_dict)
  71. net.set_train(False)
  72. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  73. res = model.eval(dataset)
  74. print("result:", res, "ckpt=", args_opt.checkpoint_path)