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 4.0 kB

4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2020-2021 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 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=None, help='Resnet Model, either resnet18, '
  26. 'resnet50 or resnet101')
  27. parser.add_argument('--dataset', type=str, default=None, help='Dataset, either cifar10 or imagenet2012')
  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', choices=("Ascend", "GPU", "CPU"),
  31. help="Device target, support Ascend, GPU and CPU.")
  32. args_opt = parser.parse_args()
  33. set_seed(1)
  34. if args_opt.net in ("resnet18", "resnet50"):
  35. if args_opt.net == "resnet18":
  36. from src.resnet import resnet18 as resnet
  37. if args_opt.net == "resnet50":
  38. from src.resnet import resnet50 as resnet
  39. if args_opt.dataset == "cifar10":
  40. from src.config import config1 as config
  41. from src.dataset import create_dataset1 as create_dataset
  42. else:
  43. from src.config import config2 as config
  44. from src.dataset import create_dataset2 as create_dataset
  45. elif args_opt.net == "resnet101":
  46. from src.resnet import resnet101 as resnet
  47. from src.config import config3 as config
  48. from src.dataset import create_dataset3 as create_dataset
  49. else:
  50. from src.resnet import se_resnet50 as resnet
  51. from src.config import config4 as config
  52. from src.dataset import create_dataset4 as create_dataset
  53. if __name__ == '__main__':
  54. target = args_opt.device_target
  55. # init context
  56. context.set_context(mode=context.GRAPH_MODE, device_target=target, save_graphs=False)
  57. if target == "Ascend":
  58. device_id = int(os.getenv('DEVICE_ID'))
  59. context.set_context(device_id=device_id)
  60. # create dataset
  61. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=config.batch_size,
  62. target=target)
  63. step_size = dataset.get_dataset_size()
  64. # define net
  65. net = resnet(class_num=config.class_num)
  66. # load checkpoint
  67. param_dict = load_checkpoint(args_opt.checkpoint_path)
  68. load_param_into_net(net, param_dict)
  69. net.set_train(False)
  70. # define loss, model
  71. if args_opt.dataset == "imagenet2012":
  72. if not config.use_label_smooth:
  73. config.label_smooth_factor = 0.0
  74. loss = CrossEntropySmooth(sparse=True, reduction='mean',
  75. smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  76. else:
  77. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  78. # define model
  79. model = Model(net, loss_fn=loss, metrics={'top_1_accuracy', 'top_5_accuracy'})
  80. # eval model
  81. res = model.eval(dataset)
  82. print("result:", res, "ckpt=", args_opt.checkpoint_path)