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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. ##############test googlenet example on cifar10#################
  17. python eval.py
  18. """
  19. import argparse
  20. import mindspore.nn as nn
  21. from mindspore import context
  22. from mindspore.nn.optim.momentum import Momentum
  23. from mindspore.train.model import Model
  24. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  25. from src.config import cifar_cfg, imagenet_cfg
  26. from src.dataset import create_dataset_cifar10, create_dataset_imagenet
  27. from src.googlenet import GoogleNet
  28. parser = argparse.ArgumentParser(description='googlenet')
  29. parser.add_argument('--dataset_name', type=str, default='cifar10', choices=['imagenet', 'cifar10'],
  30. help='dataset name.')
  31. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  32. args_opt = parser.parse_args()
  33. if __name__ == '__main__':
  34. if args_opt.dataset_name == 'cifar10':
  35. cfg = cifar_cfg
  36. dataset = create_dataset_cifar10(cfg.data_path, 1, False)
  37. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean', is_grad=False)
  38. net = GoogleNet(num_classes=cfg.num_classes)
  39. opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, cfg.momentum,
  40. weight_decay=cfg.weight_decay)
  41. model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
  42. elif args_opt.dataset_name == "imagenet":
  43. cfg = imagenet_cfg
  44. dataset = create_dataset_imagenet(cfg.val_data_path, 1, False)
  45. if not cfg.use_label_smooth:
  46. cfg.label_smooth_factor = 0.0
  47. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean",
  48. smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes)
  49. net = GoogleNet(num_classes=cfg.num_classes)
  50. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  51. else:
  52. raise ValueError("dataset is not support.")
  53. device_target = cfg.device_target
  54. context.set_context(mode=context.GRAPH_MODE, device_target=cfg.device_target)
  55. if device_target == "Ascend":
  56. context.set_context(device_id=cfg.device_id)
  57. if args_opt.checkpoint_path is not None:
  58. param_dict = load_checkpoint(args_opt.checkpoint_path)
  59. print("load checkpoint from [{}].".format(args_opt.checkpoint_path))
  60. else:
  61. param_dict = load_checkpoint(cfg.checkpoint_path)
  62. print("load checkpoint from [{}].".format(cfg.checkpoint_path))
  63. load_param_into_net(net, param_dict)
  64. net.set_train(False)
  65. acc = model.eval(dataset)
  66. print("accuracy: ", acc)