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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. """evaluate_imagenet"""
  16. import argparse
  17. import os
  18. import mindspore.nn as nn
  19. from mindspore import context
  20. from mindspore.train.model import Model
  21. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  22. from src.config import config_gpu, config_ascend, config_cpu
  23. from src.dataset import create_dataset_imagenet, create_dataset_cifar10
  24. from src.inception_v3 import InceptionV3
  25. from src.loss import CrossEntropy_Val
  26. CFG_DICT = {
  27. "Ascend": config_ascend,
  28. "GPU": config_gpu,
  29. "CPU": config_cpu,
  30. }
  31. DS_DICT = {
  32. "imagenet": create_dataset_imagenet,
  33. "cifar10": create_dataset_cifar10,
  34. }
  35. if __name__ == '__main__':
  36. parser = argparse.ArgumentParser(description='image classification evaluation')
  37. parser.add_argument('--checkpoint', type=str, default='', help='checkpoint of inception-v3 (Default: None)')
  38. parser.add_argument('--dataset_path', type=str, default='', help='Dataset path')
  39. parser.add_argument('--platform', type=str, default='GPU', choices=('Ascend', 'GPU', 'CPU'), help='run platform')
  40. args_opt = parser.parse_args()
  41. if args_opt.platform == 'Ascend':
  42. device_id = int(os.getenv('DEVICE_ID'))
  43. context.set_context(device_id=device_id)
  44. cfg = CFG_DICT[args_opt.platform]
  45. create_dataset = DS_DICT[cfg.ds_type]
  46. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.platform)
  47. net = InceptionV3(num_classes=cfg.num_classes, is_training=False)
  48. ckpt = load_checkpoint(args_opt.checkpoint)
  49. load_param_into_net(net, ckpt)
  50. net.set_train(False)
  51. cfg.rank = 0
  52. cfg.group_size = 1
  53. dataset = create_dataset(args_opt.dataset_path, False, cfg)
  54. loss = CrossEntropy_Val(smooth_factor=0.1, num_classes=cfg.num_classes)
  55. eval_metrics = {'Loss': nn.Loss(),
  56. 'Top1-Acc': nn.Top1CategoricalAccuracy(),
  57. 'Top5-Acc': nn.Top5CategoricalAccuracy()}
  58. model = Model(net, loss, optimizer=None, metrics=eval_metrics)
  59. metrics = model.eval(dataset, dataset_sink_mode=cfg.ds_sink_mode)
  60. print("metric: ", metrics)