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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 Xception."""
  16. import argparse
  17. from mindspore import context, nn
  18. from mindspore.train.model import Model
  19. from mindspore.common import set_seed
  20. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  21. from src.Xception import xception
  22. from src.config import config_gpu, config_ascend
  23. from src.dataset import create_dataset
  24. from src.loss import CrossEntropySmooth
  25. set_seed(1)
  26. if __name__ == '__main__':
  27. parser = argparse.ArgumentParser(description='Image classification')
  28. parser.add_argument('--device_target', type=str, default='GPU', help='Device target')
  29. parser.add_argument('--device_id', type=int, default=0, help='Device id')
  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. args_opt = parser.parse_args()
  33. if args_opt.device_target == "Ascend":
  34. config = config_ascend
  35. elif args_opt.device_target == "GPU":
  36. config = config_gpu
  37. else:
  38. raise ValueError("Unsupported device_target.")
  39. context.set_context(device_id=args_opt.device_id)
  40. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target, save_graphs=False)
  41. # create dataset
  42. dataset = create_dataset(args_opt.dataset_path, do_train=False, batch_size=config.batch_size, device_num=1, rank=0)
  43. step_size = dataset.get_dataset_size()
  44. # define net
  45. net = xception(class_num=config.class_num)
  46. # load checkpoint
  47. param_dict = load_checkpoint(args_opt.checkpoint_path)
  48. load_param_into_net(net, param_dict)
  49. net.set_train(False)
  50. # define loss, model
  51. loss = CrossEntropySmooth(smooth_factor=config.label_smooth_factor, num_classes=config.class_num)
  52. # define model
  53. eval_metrics = {'Loss': nn.Loss(),
  54. 'Top_1_Acc': nn.Top1CategoricalAccuracy(),
  55. 'Top_5_Acc': nn.Top5CategoricalAccuracy()}
  56. model = Model(net, loss_fn=loss, metrics=eval_metrics)
  57. # eval model
  58. res = model.eval(dataset, dataset_sink_mode=True)
  59. print("result:", res, "ckpt=", args_opt.checkpoint_path)