e2e_metric.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2021 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. __all__ = ['E2EMetric']
  18. from ppocr.utils.e2e_metric.Deteval import get_socre_A, get_socre_B, combine_results
  19. from ppocr.utils.e2e_utils.extract_textpoint_slow import get_dict
  20. class E2EMetric(object):
  21. def __init__(self,
  22. mode,
  23. gt_mat_dir,
  24. character_dict_path,
  25. main_indicator='f_score_e2e',
  26. **kwargs):
  27. self.mode = mode
  28. self.gt_mat_dir = gt_mat_dir
  29. self.label_list = get_dict(character_dict_path)
  30. self.max_index = len(self.label_list)
  31. self.main_indicator = main_indicator
  32. self.reset()
  33. def __call__(self, preds, batch, **kwargs):
  34. if self.mode == 'A':
  35. gt_polyons_batch = batch[2]
  36. temp_gt_strs_batch = batch[3][0]
  37. ignore_tags_batch = batch[4]
  38. gt_strs_batch = []
  39. for temp_list in temp_gt_strs_batch:
  40. t = ""
  41. for index in temp_list:
  42. if index < self.max_index:
  43. t += self.label_list[index]
  44. gt_strs_batch.append(t)
  45. for pred, gt_polyons, gt_strs, ignore_tags in zip(
  46. [preds], gt_polyons_batch, [gt_strs_batch], ignore_tags_batch):
  47. # prepare gt
  48. gt_info_list = [{
  49. 'points': gt_polyon,
  50. 'text': gt_str,
  51. 'ignore': ignore_tag
  52. } for gt_polyon, gt_str, ignore_tag in
  53. zip(gt_polyons, gt_strs, ignore_tags)]
  54. # prepare det
  55. e2e_info_list = [{
  56. 'points': det_polyon,
  57. 'texts': pred_str
  58. } for det_polyon, pred_str in
  59. zip(pred['points'], pred['texts'])]
  60. result = get_socre_A(gt_info_list, e2e_info_list)
  61. self.results.append(result)
  62. else:
  63. img_id = batch[5][0]
  64. e2e_info_list = [{
  65. 'points': det_polyon,
  66. 'texts': pred_str
  67. } for det_polyon, pred_str in zip(preds['points'], preds['texts'])]
  68. result = get_socre_B(self.gt_mat_dir, img_id, e2e_info_list)
  69. self.results.append(result)
  70. def get_metric(self):
  71. metrics = combine_results(self.results)
  72. self.reset()
  73. return metrics
  74. def reset(self):
  75. self.results = [] # clear results