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

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