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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. """evaluate imagenet"""
  16. import argparse
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore.train.model import Model
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  21. from src.config import nasnet_a_mobile_config_gpu as cfg
  22. from src.dataset import create_dataset
  23. from src.nasnet_a_mobile import NASNetAMobile
  24. from src.loss import CrossEntropy_Val
  25. if __name__ == '__main__':
  26. parser = argparse.ArgumentParser(description='image classification evaluation')
  27. parser.add_argument('--checkpoint', type=str, default='', help='checkpoint of nasnet_a_mobile (Default: None)')
  28. parser.add_argument('--dataset_path', type=str, default='', help='Dataset path')
  29. parser.add_argument('--platform', type=str, default='GPU', choices=('Ascend', 'GPU'), help='run platform')
  30. args_opt = parser.parse_args()
  31. if args_opt.platform != 'GPU':
  32. raise ValueError("Only supported GPU training.")
  33. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.platform)
  34. net = NASNetAMobile(num_classes=cfg.num_classes, is_training=False)
  35. ckpt = load_checkpoint(args_opt.checkpoint)
  36. load_param_into_net(net, ckpt)
  37. net.set_train(False)
  38. dataset = create_dataset(args_opt.dataset_path, cfg, False)
  39. loss = CrossEntropy_Val(smooth_factor=0.1, num_classes=cfg.num_classes)
  40. eval_metrics = {'Loss': nn.Loss(),
  41. 'Top1-Acc': nn.Top1CategoricalAccuracy(),
  42. 'Top5-Acc': nn.Top5CategoricalAccuracy()}
  43. model = Model(net, loss, optimizer=None, metrics=eval_metrics)
  44. metrics = model.eval(dataset)
  45. print("metric: ", metrics)