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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 squeezenet."""
  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. parser = argparse.ArgumentParser(description='Image classification')
  25. parser.add_argument('--net', type=str, default='squeezenet', choices=['squeezenet', 'squeezenet_residual'],
  26. help='Model.')
  27. parser.add_argument('--dataset', type=str, default='cifar10', choices=['cifar10', 'imagenet'], help='Dataset.')
  28. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  29. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  30. parser.add_argument('--device_target', type=str, default='Ascend', help='Device target')
  31. args_opt = parser.parse_args()
  32. set_seed(1)
  33. if args_opt.net == "squeezenet":
  34. from src.squeezenet import SqueezeNet as squeezenet
  35. if args_opt.dataset == "cifar10":
  36. from src.config import config1 as config
  37. from src.dataset import create_dataset_cifar as create_dataset
  38. else:
  39. from src.config import config2 as config
  40. from src.dataset import create_dataset_imagenet as create_dataset
  41. else:
  42. from src.squeezenet import SqueezeNet_Residual as squeezenet
  43. if args_opt.dataset == "cifar10":
  44. from src.config import config3 as config
  45. from src.dataset import create_dataset_cifar as create_dataset
  46. else:
  47. from src.config import config4 as config
  48. from src.dataset import create_dataset_imagenet as create_dataset
  49. if __name__ == '__main__':
  50. target = args_opt.device_target
  51. # init context
  52. device_id = os.getenv('DEVICE_ID')
  53. device_id = int(device_id) if device_id else 0
  54. context.set_context(mode=context.GRAPH_MODE,
  55. device_target=target,
  56. device_id=device_id)
  57. # create dataset
  58. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  59. do_train=False,
  60. batch_size=config.batch_size,
  61. target=target)
  62. step_size = dataset.get_dataset_size()
  63. # define net
  64. net = squeezenet(num_classes=config.class_num)
  65. # load checkpoint
  66. param_dict = load_checkpoint(args_opt.checkpoint_path)
  67. load_param_into_net(net, param_dict)
  68. net.set_train(False)
  69. # define loss
  70. if args_opt.dataset == "imagenet":
  71. if not config.use_label_smooth:
  72. config.label_smooth_factor = 0.0
  73. loss = CrossEntropySmooth(sparse=True,
  74. reduction='mean',
  75. smooth_factor=config.label_smooth_factor,
  76. num_classes=config.class_num)
  77. else:
  78. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  79. # define model
  80. model = Model(net,
  81. loss_fn=loss,
  82. metrics={'top_1_accuracy', 'top_5_accuracy'})
  83. # eval model
  84. res = model.eval(dataset)
  85. print("result:", res, "ckpt=", args_opt.checkpoint_path)