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

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