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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. """Evaluation for SSD"""
  16. import os
  17. import argparse
  18. from mindspore import context, Tensor
  19. from mindspore.train.serialization import load_checkpoint, load_param_into_net
  20. from src.ssd import SSD300, SsdInferWithDecoder, ssd_mobilenet_v2, ssd_mobilenet_v1_fpn, ssd_resnet50_fpn, ssd_vgg16
  21. from src.dataset import create_ssd_dataset, create_mindrecord
  22. from src.config import config
  23. from src.eval_utils import apply_eval
  24. from src.box_utils import default_boxes
  25. def ssd_eval(dataset_path, ckpt_path, anno_json):
  26. """SSD evaluation."""
  27. batch_size = 1
  28. ds = create_ssd_dataset(dataset_path, batch_size=batch_size, repeat_num=1,
  29. is_training=False, use_multiprocessing=False)
  30. if config.model == "ssd300":
  31. net = SSD300(ssd_mobilenet_v2(), config, is_training=False)
  32. elif config.model == "ssd_vgg16":
  33. net = ssd_vgg16(config=config)
  34. elif config.model == "ssd_mobilenet_v1_fpn":
  35. net = ssd_mobilenet_v1_fpn(config=config)
  36. elif config.model == "ssd_resnet50_fpn":
  37. net = ssd_resnet50_fpn(config=config)
  38. else:
  39. raise ValueError(f'config.model: {config.model} is not supported')
  40. net = SsdInferWithDecoder(net, Tensor(default_boxes), config)
  41. print("Load Checkpoint!")
  42. param_dict = load_checkpoint(ckpt_path)
  43. net.init_parameters_data()
  44. load_param_into_net(net, param_dict)
  45. net.set_train(False)
  46. total = ds.get_dataset_size() * batch_size
  47. print("\n========================================\n")
  48. print("total images num: ", total)
  49. print("Processing, please wait a moment.")
  50. eval_param_dict = {"net": net, "dataset": ds, "anno_json": anno_json}
  51. mAP = apply_eval(eval_param_dict)
  52. print("\n========================================\n")
  53. print(f"mAP: {mAP}")
  54. def get_eval_args():
  55. parser = argparse.ArgumentParser(description='SSD evaluation')
  56. parser.add_argument("--device_id", type=int, default=0, help="Device id, default is 0.")
  57. parser.add_argument("--dataset", type=str, default="coco", help="Dataset, default is coco.")
  58. parser.add_argument("--checkpoint_path", type=str, required=True, help="Checkpoint file path.")
  59. parser.add_argument("--run_platform", type=str, default="Ascend", choices=("Ascend", "GPU", "CPU"),
  60. help="run platform, support Ascend ,GPU and CPU.")
  61. return parser.parse_args()
  62. if __name__ == '__main__':
  63. args_opt = get_eval_args()
  64. if args_opt.dataset == "coco":
  65. json_path = os.path.join(config.coco_root, config.instances_set.format(config.val_data_type))
  66. elif args_opt.dataset == "voc":
  67. json_path = os.path.join(config.voc_root, config.voc_json)
  68. else:
  69. raise ValueError('SSD eval only support dataset mode is coco and voc!')
  70. context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.run_platform, device_id=args_opt.device_id)
  71. mindrecord_file = create_mindrecord(args_opt.dataset, "ssd_eval.mindrecord", False)
  72. print("Start Eval!")
  73. ssd_eval(mindrecord_file, args_opt.checkpoint_path, json_path)