kie_metric.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. # The code is refer from: https://github.com/open-mmlab/mmocr/blob/main/mmocr/core/evaluation/kie_metric.py
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import numpy as np
  19. import paddle
  20. __all__ = ['KIEMetric']
  21. class KIEMetric(object):
  22. def __init__(self, main_indicator='hmean', **kwargs):
  23. self.main_indicator = main_indicator
  24. self.reset()
  25. self.node = []
  26. self.gt = []
  27. def __call__(self, preds, batch, **kwargs):
  28. nodes, _ = preds
  29. gts, tag = batch[4].squeeze(0), batch[5].tolist()[0]
  30. gts = gts[:tag[0], :1].reshape([-1])
  31. self.node.append(nodes.numpy())
  32. self.gt.append(gts)
  33. # result = self.compute_f1_score(nodes, gts)
  34. # self.results.append(result)
  35. def compute_f1_score(self, preds, gts):
  36. ignores = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25]
  37. C = preds.shape[1]
  38. classes = np.array(sorted(set(range(C)) - set(ignores)))
  39. hist = np.bincount(
  40. (gts * C).astype('int64') + preds.argmax(1), minlength=C
  41. **2).reshape([C, C]).astype('float32')
  42. diag = np.diag(hist)
  43. recalls = diag / hist.sum(1).clip(min=1)
  44. precisions = diag / hist.sum(0).clip(min=1)
  45. f1 = 2 * recalls * precisions / (recalls + precisions).clip(min=1e-8)
  46. return f1[classes]
  47. def combine_results(self, results):
  48. node = np.concatenate(self.node, 0)
  49. gts = np.concatenate(self.gt, 0)
  50. results = self.compute_f1_score(node, gts)
  51. data = {'hmean': results.mean()}
  52. return data
  53. def get_metric(self):
  54. metrics = self.combine_results(self.results)
  55. self.reset()
  56. return metrics
  57. def reset(self):
  58. self.results = [] # clear results
  59. self.node = []
  60. self.gt = []