predictor.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 os
  15. import numpy as np
  16. from ....utils import logging
  17. from ...base.predictor.transforms import image_common
  18. from ...base import BasePredictor
  19. from .keys import TableRecKeys as K
  20. from . import transforms as T
  21. from ..support_models import SUPPORT_MODELS
  22. class TableRecPredictor(BasePredictor):
  23. """ TableRecPredictor """
  24. support_models = SUPPORT_MODELS
  25. def __init__(self,
  26. model_dir,
  27. kernel_option,
  28. pre_transforms=None,
  29. post_transforms=None,
  30. table_max_len=488):
  31. super().__init__(
  32. model_dir=model_dir,
  33. kernel_option=kernel_option,
  34. pre_transforms=pre_transforms,
  35. post_transforms=post_transforms)
  36. self.table_max_len = table_max_len
  37. @classmethod
  38. def get_input_keys(cls):
  39. """ get input keys """
  40. return [[K.IMAGE, K.ORI_IM_SIZE], [K.IM_PATH, K.ORI_IM_SIZE]]
  41. @classmethod
  42. def get_output_keys(cls):
  43. """ get output keys """
  44. return [K.STRUCTURE_PROB, K.LOC_PROB, K.SHAPE_LIST]
  45. def _run(self, batch_input):
  46. """ run """
  47. images = [data[K.IMAGE] for data in batch_input]
  48. input_ = np.stack(images, axis=0)
  49. if input_.ndim == 3:
  50. input_ = input_[:, np.newaxis]
  51. input_ = input_.astype(dtype=np.float32, copy=False)
  52. outputs = self._predictor.predict([input_])
  53. struc_probs = outputs[1]
  54. bbox_probs = outputs[0]
  55. for data in batch_input:
  56. data[K.SHAPE_LIST] = [data[K.ORI_IM_SIZE]]
  57. # In-place update
  58. pred = batch_input
  59. for dict_, struc_prob, bbox_prob in zip(pred, struc_probs, bbox_probs):
  60. dict_[K.STRUCTURE_PROB] = struc_prob[np.newaxis, :]
  61. dict_[K.LOC_PROB] = bbox_prob[np.newaxis, :]
  62. return pred
  63. def _get_pre_transforms_for_data(self, data):
  64. """ _get_pre_transforms_for_data """
  65. if K.IMAGE not in data and K.IM_PATH not in data:
  66. raise KeyError(
  67. f"Key {repr(K.IMAGE)} or {repr(K.IM_PATH)} is required, but not found."
  68. )
  69. pre_transforms = []
  70. if K.IMAGE not in data:
  71. pre_transforms.append(image_common.ReadImage())
  72. pre_transforms.append(
  73. image_common.ResizeByLong(target_long_edge=self.table_max_len))
  74. pre_transforms.append(
  75. image_common.Normalize(
  76. mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]))
  77. pre_transforms.append(
  78. image_common.Pad(target_size=self.table_max_len, val=0.0))
  79. pre_transforms.append(image_common.ToCHWImage())
  80. return pre_transforms
  81. def _get_post_transforms_for_data(self, data):
  82. """ get postprocess transforms """
  83. post_transforms = [T.TableLabelDecode()]
  84. if data.get('cli_flag', False):
  85. output_dir = data.get("output_dir", "./")
  86. post_transforms.append(T.SaveTableResults(output_dir))
  87. return post_transforms