predictor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. output_dir,
  29. pre_transforms=None,
  30. post_transforms=None,
  31. table_max_len=488):
  32. super().__init__(
  33. model_dir=model_dir,
  34. kernel_option=kernel_option,
  35. output_dir=output_dir,
  36. pre_transforms=pre_transforms,
  37. post_transforms=post_transforms)
  38. self.table_max_len = table_max_len
  39. @classmethod
  40. def get_input_keys(cls):
  41. """ get input keys """
  42. return [[K.IMAGE, K.ORI_IM_SIZE], [K.IM_PATH, K.ORI_IM_SIZE]]
  43. @classmethod
  44. def get_output_keys(cls):
  45. """ get output keys """
  46. return [K.STRUCTURE_PROB, K.LOC_PROB, K.SHAPE_LIST]
  47. def _run(self, batch_input):
  48. """ run """
  49. images = [data[K.IMAGE] for data in batch_input]
  50. input_ = np.stack(images, axis=0)
  51. if input_.ndim == 3:
  52. input_ = input_[:, np.newaxis]
  53. input_ = input_.astype(dtype=np.float32, copy=False)
  54. outputs = self._predictor.predict([input_])
  55. struc_probs = outputs[1]
  56. bbox_probs = outputs[0]
  57. for data in batch_input:
  58. data[K.SHAPE_LIST] = [data[K.ORI_IM_SIZE]]
  59. # In-place update
  60. pred = batch_input
  61. for dict_, struc_prob, bbox_prob in zip(pred, struc_probs, bbox_probs):
  62. dict_[K.STRUCTURE_PROB] = struc_prob[np.newaxis, :]
  63. dict_[K.LOC_PROB] = bbox_prob[np.newaxis, :]
  64. return pred
  65. def _get_pre_transforms_from_config(self):
  66. """ _get_pre_transforms_from_config """
  67. return [
  68. image_common.ReadImage(),
  69. image_common.ResizeByLong(target_long_edge=self.table_max_len),
  70. image_common.Normalize(
  71. mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
  72. image_common.Pad(target_size=self.table_max_len, val=0.0),
  73. image_common.ToCHWImage()
  74. ]
  75. def _get_post_transforms_from_config(self):
  76. """ get postprocess transforms """
  77. return [T.TableLabelDecode(), T.SaveTableResults(self.output_dir)]