eval.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. __dir__ = os.path.dirname(os.path.abspath(__file__))
  20. sys.path.append(__dir__)
  21. sys.path.append(os.path.abspath(os.path.join(__dir__, '..')))
  22. from ppocr.data import build_dataloader
  23. from ppocr.modeling.architectures import build_model
  24. from ppocr.postprocess import build_post_process
  25. from ppocr.metrics import build_metric
  26. from ppocr.utils.save_load import init_model
  27. from ppocr.utils.utility import print_dict
  28. import tools.program as program
  29. def main():
  30. global_config = config['Global']
  31. # build dataloader
  32. valid_dataloader = build_dataloader(config, 'Eval', device, logger)
  33. # build post process
  34. post_process_class = build_post_process(config['PostProcess'],
  35. global_config)
  36. # build model
  37. # for rec algorithm
  38. if hasattr(post_process_class, 'character'):
  39. config['Architecture']["Head"]['out_channels'] = len(
  40. getattr(post_process_class, 'character'))
  41. model = build_model(config['Architecture'])
  42. use_srn = config['Architecture']['algorithm'] == "SRN"
  43. best_model_dict = init_model(config, model, logger)
  44. if len(best_model_dict):
  45. logger.info('metric in ckpt ***************')
  46. for k, v in best_model_dict.items():
  47. logger.info('{}:{}'.format(k, v))
  48. # build metric
  49. eval_class = build_metric(config['Metric'])
  50. # start eval
  51. metirc = program.eval(model, valid_dataloader, post_process_class,
  52. eval_class, use_srn)
  53. logger.info('metric eval ***************')
  54. for k, v in metirc.items():
  55. logger.info('{}:{}'.format(k, v))
  56. if __name__ == '__main__':
  57. config, device, logger, vdl_writer = program.preprocess()
  58. main()