vqa_token_re_layoutlm_postprocess.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. result = []
  38. used_tail_id = []
  39. for relation in pred_relation:
  40. if relation['tail_id'] in used_tail_id:
  41. continue
  42. used_tail_id.append(relation['tail_id'])
  43. ocr_info_head = ser_result[entity_idx_dict[relation['head_id']]]
  44. ocr_info_tail = ser_result[entity_idx_dict[relation['tail_id']]]
  45. result.append((ocr_info_head, ocr_info_tail))
  46. results.append(result)
  47. return results
  48. def decode_pred(self, pred_relations):
  49. pred_relations_new = []
  50. for pred_relation in pred_relations:
  51. pred_relation_new = []
  52. pred_relation = pred_relation[1:pred_relation[0, 0, 0] + 1]
  53. for relation in pred_relation:
  54. relation_new = dict()
  55. relation_new['head_id'] = relation[0, 0]
  56. relation_new['head'] = tuple(relation[1])
  57. relation_new['head_type'] = relation[2, 0]
  58. relation_new['tail_id'] = relation[3, 0]
  59. relation_new['tail'] = tuple(relation[4])
  60. relation_new['tail_type'] = relation[5, 0]
  61. relation_new['type'] = relation[6, 0]
  62. pred_relation_new.append(relation_new)
  63. pred_relations_new.append(pred_relation_new)
  64. return pred_relations_new
  65. class DistillationRePostProcess(VQAReTokenLayoutLMPostProcess):
  66. """
  67. DistillationRePostProcess
  68. """
  69. def __init__(self, model_name=["Student"], key=None, **kwargs):
  70. super().__init__(**kwargs)
  71. if not isinstance(model_name, list):
  72. model_name = [model_name]
  73. self.model_name = model_name
  74. self.key = key
  75. def __call__(self, preds, *args, **kwargs):
  76. output = dict()
  77. for name in self.model_name:
  78. pred = preds[name]
  79. if self.key is not None:
  80. pred = pred[self.key]
  81. output[name] = super().__call__(pred, *args, **kwargs)
  82. return output