vqa_token_re_layoutlm_postprocess.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import paddle
  15. class VQAReTokenLayoutLMPostProcess(object):
  16. """Convert between text-label and text-index"""
  17. def __init__(self, **kwargs):
  18. super(VQAReTokenLayoutLMPostProcess, self).__init__()
  19. def __call__(self, preds, label=None, *args, **kwargs):
  20. pred_relations = preds["pred_relations"]
  21. if isinstance(preds["pred_relations"], paddle.Tensor):
  22. pred_relations = pred_relations.numpy()
  23. pred_relations = self.decode_pred(pred_relations)
  24. if label is not None:
  25. return self._metric(pred_relations, label)
  26. else:
  27. return self._infer(pred_relations, *args, **kwargs)
  28. def _metric(self, pred_relations, label):
  29. return pred_relations, label[-1], label[-2]
  30. def _infer(self, pred_relations, *args, **kwargs):
  31. ser_results = kwargs["ser_results"]
  32. entity_idx_dict_batch = kwargs["entity_idx_dict_batch"]
  33. # merge relations and ocr info
  34. results = []
  35. for pred_relation, ser_result, entity_idx_dict in zip(
  36. pred_relations, ser_results, entity_idx_dict_batch
  37. ):
  38. result = []
  39. used_tail_id = []
  40. for relation in pred_relation:
  41. if relation["tail_id"] in used_tail_id:
  42. continue
  43. used_tail_id.append(relation["tail_id"])
  44. ocr_info_head = ser_result[entity_idx_dict[relation["head_id"]]]
  45. ocr_info_tail = ser_result[entity_idx_dict[relation["tail_id"]]]
  46. result.append((ocr_info_head, ocr_info_tail))
  47. results.append(result)
  48. return results
  49. def decode_pred(self, pred_relations):
  50. pred_relations_new = []
  51. for pred_relation in pred_relations:
  52. pred_relation_new = []
  53. pred_relation = pred_relation[1 : pred_relation[0, 0, 0] + 1]
  54. for relation in pred_relation:
  55. relation_new = dict()
  56. relation_new["head_id"] = relation[0, 0]
  57. relation_new["head"] = tuple(relation[1])
  58. relation_new["head_type"] = relation[2, 0]
  59. relation_new["tail_id"] = relation[3, 0]
  60. relation_new["tail"] = tuple(relation[4])
  61. relation_new["tail_type"] = relation[5, 0]
  62. relation_new["type"] = relation[6, 0]
  63. pred_relation_new.append(relation_new)
  64. pred_relations_new.append(pred_relation_new)
  65. return pred_relations_new
  66. class DistillationRePostProcess(VQAReTokenLayoutLMPostProcess):
  67. """
  68. DistillationRePostProcess
  69. """
  70. def __init__(self, model_name=["Student"], key=None, **kwargs):
  71. super().__init__(**kwargs)
  72. if not isinstance(model_name, list):
  73. model_name = [model_name]
  74. self.model_name = model_name
  75. self.key = key
  76. def __call__(self, preds, *args, **kwargs):
  77. output = dict()
  78. for name in self.model_name:
  79. pred = preds[name]
  80. if self.key is not None:
  81. pred = pred[self.key]
  82. output[name] = super().__call__(pred, *args, **kwargs)
  83. return output