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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. eval.
  17. """
  18. import os
  19. import argparse
  20. from mindspore import context
  21. from mindspore import nn
  22. from mindspore.train.model import Model
  23. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  24. from mindspore.common import dtype as mstype
  25. from src.dataset import create_dataset
  26. from src.config import config_ascend, config_gpu
  27. from src.ghostnet import ghostnet_1x, ghostnet_nose_1x
  28. from src.ghostnet600 import ghostnet_600m
  29. parser = argparse.ArgumentParser(description='Image classification')
  30. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  31. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  32. parser.add_argument('--platform', type=str, default=None, help='run platform')
  33. parser.add_argument('--model', type=str, default=None, help='ghostnet')
  34. args_opt = parser.parse_args()
  35. if __name__ == '__main__':
  36. config_platform = None
  37. if args_opt.platform == "Ascend":
  38. config_platform = config_ascend
  39. device_id = int(os.getenv('DEVICE_ID'))
  40. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
  41. device_id=device_id, save_graphs=False)
  42. elif args_opt.platform == "GPU":
  43. config_platform = config_gpu
  44. context.set_context(mode=context.GRAPH_MODE,
  45. device_target="GPU", save_graphs=False)
  46. else:
  47. raise ValueError("Unsupport platform.")
  48. loss = nn.SoftmaxCrossEntropyWithLogits(
  49. is_grad=False, sparse=True, reduction='mean')
  50. if args_opt.model == 'ghostnet':
  51. net = ghostnet_1x(num_classes=config_platform.num_classes)
  52. elif args_opt.model == 'ghostnet_nose':
  53. net = ghostnet_nose_1x(num_classes=config_platform.num_classes)
  54. elif args_opt.model == 'ghostnet-600':
  55. net = ghostnet_600m(num_classes=config_platform.num_classes)
  56. if args_opt.platform == "Ascend":
  57. net.to_float(mstype.float16)
  58. for _, cell in net.cells_and_names():
  59. if isinstance(cell, nn.Dense):
  60. cell.to_float(mstype.float32)
  61. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  62. do_train=False,
  63. config=config_platform,
  64. platform=args_opt.platform,
  65. batch_size=config_platform.batch_size,
  66. model=args_opt.model)
  67. step_size = dataset.get_dataset_size()
  68. if args_opt.checkpoint_path:
  69. param_dict = load_checkpoint(args_opt.checkpoint_path)
  70. load_param_into_net(net, param_dict)
  71. net.set_train(False)
  72. model = Model(net, loss_fn=loss, metrics={'acc'})
  73. res = model.eval(dataset)
  74. print("result:", res, "ckpt=", args_opt.checkpoint_path)