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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 lenet example ########################
  17. eval lenet according to model file:
  18. python eval.py --data_path /YourDataPath --ckpt_path Your.ckpt
  19. """
  20. import os
  21. import ast
  22. import argparse
  23. import mindspore.nn as nn
  24. from mindspore import context
  25. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  26. from mindspore.train import Model
  27. from mindspore.nn.metrics import Accuracy
  28. from src.dataset import create_dataset
  29. from src.config import mnist_cfg as cfg
  30. from src.lenet import LeNet5
  31. if __name__ == "__main__":
  32. parser = argparse.ArgumentParser(description='MindSpore Lenet Example')
  33. parser.add_argument('--device_target', type=str, default="Ascend", choices=['Ascend', 'GPU', 'CPU'],
  34. help='device where the code will be implemented (default: Ascend)')
  35. parser.add_argument('--data_path', type=str, default="./Data",
  36. help='path where the dataset is saved')
  37. parser.add_argument('--ckpt_path', type=str, default="", help='if mode is test, must provide\
  38. path where the trained ckpt file')
  39. parser.add_argument('--dataset_sink_mode', type=ast.literal_eval,
  40. default=False, help='dataset_sink_mode is False or True')
  41. args = parser.parse_args()
  42. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
  43. network = LeNet5(cfg.num_classes)
  44. net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
  45. repeat_size = cfg.epoch_size
  46. net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
  47. model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
  48. print("============== Starting Testing ==============")
  49. param_dict = load_checkpoint(args.ckpt_path)
  50. load_param_into_net(network, param_dict)
  51. ds_eval = create_dataset(os.path.join(args.data_path, "test"),
  52. cfg.batch_size,
  53. 1)
  54. acc = model.eval(ds_eval, dataset_sink_mode=args.dataset_sink_mode)
  55. print("============== {} ==============".format(acc))