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

5 years ago
5 years ago
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. """eval mobilenet_v1."""
  16. import os
  17. import argparse
  18. from mindspore import context
  19. from mindspore.common import set_seed
  20. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  21. from mindspore.train.model import Model
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. from src.CrossEntropySmooth import CrossEntropySmooth
  24. from src.mobilenet_v1 import mobilenet_v1 as mobilenet
  25. parser = argparse.ArgumentParser(description='Image classification')
  26. parser.add_argument('--dataset', type=str, default=None, help='Dataset, either cifar10 or imagenet2012')
  27. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  28. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  29. parser.add_argument('--device_target', type=str, default='Ascend', help='Device target')
  30. args_opt = parser.parse_args()
  31. set_seed(1)
  32. if args_opt.dataset == 'cifar10':
  33. from src.config import config1 as config
  34. from src.dataset import create_dataset1 as create_dataset
  35. else:
  36. from src.config import config2 as config
  37. from src.dataset import create_dataset2 as create_dataset
  38. if __name__ == '__main__':
  39. target = args_opt.device_target
  40. # init context
  41. context.set_context(mode=context.GRAPH_MODE, device_target=target, save_graphs=False)
  42. if target == "Ascend":
  43. device_id = int(os.getenv('DEVICE_ID'))
  44. context.set_context(device_id=device_id)
  45. # create dataset
  46. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=config.batch_size,
  47. target=target)
  48. step_size = dataset.get_dataset_size()
  49. # define net
  50. net = mobilenet(class_num=config.class_num)
  51. # load checkpoint
  52. param_dict = load_checkpoint(args_opt.checkpoint_path)
  53. load_param_into_net(net, param_dict)
  54. net.set_train(False)
  55. # define loss, model
  56. if args_opt.dataset == "imagenet2012":
  57. if not config.use_label_smooth:
  58. config.label_smooth_factor = 0.0
  59. loss = CrossEntropySmooth(sparse=True, reduction='mean',
  60. smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  61. else:
  62. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  63. # define model
  64. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  65. # eval model
  66. res = model.eval(dataset)
  67. print("result:", res, "ckpt=", args_opt.checkpoint_path)