predictor.py 3.2 KB

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