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.2 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.mobilenetV2 import MobileNetV2Backbone, MobileNetV2Head, mobilenet_v2
  24. from src.args import eval_parse_args
  25. from src.models import load_ckpt
  26. from src.utils import switch_precision, set_context
  27. if __name__ == '__main__':
  28. args_opt = eval_parse_args()
  29. config = set_config(args_opt)
  30. backbone_net = MobileNetV2Backbone(platform=args_opt.platform)
  31. head_net = MobileNetV2Head(input_channel=backbone_net.out_channels, num_classes=config.num_classes)
  32. net = mobilenet_v2(backbone_net, head_net)
  33. #load the trained checkpoint file to the net for evaluation
  34. if args_opt.head_ckpt:
  35. load_ckpt(backbone_net, args_opt.pretrain_ckpt)
  36. load_ckpt(head_net, args_opt.head_ckpt)
  37. else:
  38. load_ckpt(net, args_opt.pretrain_ckpt)
  39. set_context(config)
  40. switch_precision(net, mstype.float16, config)
  41. dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, config=config)
  42. step_size = dataset.get_dataset_size()
  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, dataset_sink_mode=False)
  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}")