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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from dataset import create_dataset
  21. from config import config
  22. from mindspore import context
  23. from mindspore.model_zoo.resnet import resnet50
  24. from mindspore.train.model import Model
  25. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  26. from crossentropy import CrossEntropy
  27. parser = argparse.ArgumentParser(description='Image classification')
  28. parser.add_argument('--run_distribute', type=bool, default=False, help='Run distribute')
  29. parser.add_argument('--device_num', type=int, default=1, help='Device num.')
  30. parser.add_argument('--do_train', type=bool, default=False, help='Do train or not.')
  31. parser.add_argument('--do_eval', type=bool, default=True, help='Do eval or not.')
  32. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  33. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  34. parser.add_argument('--device_target', type=str, default='Ascend', help='Device target')
  35. args_opt = parser.parse_args()
  36. target = args_opt.device_target
  37. context.set_context(mode=context.GRAPH_MODE, device_target=target, save_graphs=False)
  38. if target == "Ascend":
  39. device_id = int(os.getenv('DEVICE_ID'))
  40. context.set_context(device_id=device_id)
  41. if __name__ == '__main__':
  42. net = resnet50(class_num=config.class_num)
  43. if not config.use_label_smooth:
  44. config.label_smooth_factor = 0.0
  45. loss = CrossEntropy(smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  46. if args_opt.do_eval:
  47. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=config.batch_size,
  48. target=target)
  49. step_size = dataset.get_dataset_size()
  50. if args_opt.checkpoint_path:
  51. param_dict = load_checkpoint(args_opt.checkpoint_path)
  52. load_param_into_net(net, param_dict)
  53. net.set_train(False)
  54. model = Model(net, loss_fn=loss, metrics={'acc'})
  55. res = model.eval(dataset)
  56. print("result:", res, "ckpt=", args_opt.checkpoint_path)