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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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.
  17. """
  18. from mindspore import nn
  19. from mindspore.train.model import Model
  20. from mindspore.common import dtype as mstype
  21. from src.dataset import create_dataset
  22. from src.config import set_config
  23. from src.args import eval_parse_args
  24. from src.models import define_net, load_ckpt
  25. from src.utils import switch_precision, set_context
  26. if __name__ == '__main__':
  27. args_opt = eval_parse_args()
  28. config = set_config(args_opt)
  29. backbone_net, head_net, net = define_net(config, args_opt.is_training)
  30. #load the trained checkpoint file to the net for evaluation
  31. if args_opt.head_ckpt:
  32. load_ckpt(backbone_net, args_opt.pretrain_ckpt)
  33. load_ckpt(head_net, args_opt.head_ckpt)
  34. else:
  35. load_ckpt(net, args_opt.pretrain_ckpt)
  36. set_context(config)
  37. switch_precision(net, mstype.float16, config)
  38. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, config=config)
  39. step_size = dataset.get_dataset_size()
  40. if step_size == 0:
  41. raise ValueError("The step_size of dataset is zero. Check if the images count of train dataset is more \
  42. than batch_size in config.py")
  43. net.set_train(False)
  44. loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  45. model = Model(net, loss_fn=loss, metrics={'acc'})
  46. res = model.eval(dataset)
  47. print(f"result:{res}\npretrain_ckpt={args_opt.pretrain_ckpt}")
  48. if args_opt.head_ckpt:
  49. print(f"head_ckpt={args_opt.head_ckpt}")