eval.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # coding: utf8
  2. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import paddlex as pdx
  17. import paddlex.utils.logging as logging
  18. from paddlex.seg import transforms
  19. def parse_args():
  20. parser = argparse.ArgumentParser(description='HumanSeg training')
  21. parser.add_argument(
  22. '--model_dir',
  23. dest='model_dir',
  24. help='Model path for evaluating',
  25. type=str,
  26. default='output/best_model')
  27. parser.add_argument(
  28. '--data_dir',
  29. dest='data_dir',
  30. help='The root directory of dataset',
  31. type=str)
  32. parser.add_argument(
  33. '--val_list',
  34. dest='val_list',
  35. help='Val list file of dataset',
  36. type=str,
  37. default=None)
  38. parser.add_argument(
  39. '--batch_size',
  40. dest='batch_size',
  41. help='Mini batch size',
  42. type=int,
  43. default=128)
  44. parser.add_argument(
  45. "--image_shape",
  46. dest="image_shape",
  47. help="The image shape for net inputs.",
  48. nargs=2,
  49. default=[192, 192],
  50. type=int)
  51. return parser.parse_args()
  52. def dict2str(dict_input):
  53. out = ''
  54. for k, v in dict_input.items():
  55. try:
  56. v = round(float(v), 6)
  57. except:
  58. pass
  59. out = out + '{}={}, '.format(k, v)
  60. return out.strip(', ')
  61. def evaluate(args):
  62. eval_transforms = transforms.Compose(
  63. [transforms.Resize(args.image_shape), transforms.Normalize()])
  64. eval_dataset = pdx.datasets.SegDataset(
  65. data_dir=args.data_dir,
  66. file_list=args.val_list,
  67. transforms=eval_transforms)
  68. model = pdx.load_model(args.model_dir)
  69. metrics = model.evaluate(eval_dataset, args.batch_size)
  70. logging.info('[EVAL] Finished, {} .'.format(dict2str(metrics)))
  71. if __name__ == '__main__':
  72. args = parse_args()
  73. evaluate(args)