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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 tinydarknet example on cifar10#################
  17. python eval.py
  18. """
  19. import argparse
  20. from mindspore import context
  21. from mindspore.train.model import Model
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. from mindspore.common import set_seed
  24. from src.config import imagenet_cfg
  25. from src.dataset import create_dataset_imagenet
  26. from src.tinydarknet import TinyDarkNet
  27. from src.CrossEntropySmooth import CrossEntropySmooth
  28. set_seed(1)
  29. parser = argparse.ArgumentParser(description='tinydarknet')
  30. parser.add_argument('--dataset_name', type=str, default='imagenet', choices=['imagenet', 'cifar10'],
  31. help='dataset name.')
  32. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  33. args_opt = parser.parse_args()
  34. if __name__ == '__main__':
  35. if args_opt.dataset_name == "imagenet":
  36. cfg = imagenet_cfg
  37. dataset = create_dataset_imagenet(cfg.val_data_path, 1, False)
  38. if not cfg.use_label_smooth:
  39. cfg.label_smooth_factor = 0.0
  40. loss = CrossEntropySmooth(sparse=True, reduction="mean",
  41. smooth_factor=cfg.label_smooth_factor, num_classes=cfg.num_classes)
  42. net = TinyDarkNet(num_classes=cfg.num_classes)
  43. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  44. else:
  45. raise ValueError("dataset is not support.")
  46. device_target = cfg.device_target
  47. context.set_context(mode=context.GRAPH_MODE, device_target=cfg.device_target)
  48. if device_target == "Ascend":
  49. context.set_context(device_id=cfg.device_id)
  50. if args_opt.checkpoint_path is not None:
  51. param_dict = load_checkpoint(args_opt.checkpoint_path)
  52. print("load checkpoint from [{}].".format(args_opt.checkpoint_path))
  53. else:
  54. param_dict = load_checkpoint(cfg.checkpoint_path)
  55. print("load checkpoint from [{}].".format(cfg.checkpoint_path))
  56. load_param_into_net(net, param_dict)
  57. net.set_train(False)
  58. acc = model.eval(dataset)
  59. print("accuracy: ", acc)