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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 argparse
  19. from mindspore import context
  20. from mindspore import nn
  21. from mindspore.train.model import Model
  22. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  23. from src.dataset import create_dataset
  24. from src.config import config_gpu
  25. from src.mobilenetV3 import mobilenet_v3_large
  26. parser = argparse.ArgumentParser(description='Image classification')
  27. parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
  28. parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
  29. parser.add_argument('--device_target', type=str, default="GPU", help='run device_target')
  30. args_opt = parser.parse_args()
  31. if __name__ == '__main__':
  32. config = None
  33. if args_opt.device_target == "GPU":
  34. config = config_gpu
  35. context.set_context(mode=context.GRAPH_MODE,
  36. device_target="GPU", save_graphs=False)
  37. else:
  38. raise ValueError("Unsupported device_target.")
  39. loss = nn.SoftmaxCrossEntropyWithLogits(
  40. is_grad=False, sparse=True, reduction='mean')
  41. net = mobilenet_v3_large(num_classes=config.num_classes)
  42. dataset = create_dataset(dataset_path=args_opt.dataset_path,
  43. do_train=False,
  44. config=config,
  45. device_target=args_opt.device_target,
  46. batch_size=config.batch_size)
  47. step_size = dataset.get_dataset_size()
  48. if args_opt.checkpoint_path:
  49. param_dict = load_checkpoint(args_opt.checkpoint_path)
  50. load_param_into_net(net, param_dict)
  51. net.set_train(False)
  52. model = Model(net, loss_fn=loss, metrics={'acc'})
  53. res = model.eval(dataset)
  54. print("result:", res, "ckpt=", args_opt.checkpoint_path)