predictor.py 3.1 KB

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