model.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. from ....utils import logging
  16. from ...base.utils.arg import CLIArgument
  17. from ...base.utils.subprocess import CompletedProcess
  18. from ....utils.misc import abspath
  19. from ..text_rec.model import TextRecModel
  20. class TableRecModel(TextRecModel):
  21. """Table Recognition Model"""
  22. METRICS = ["acc"]
  23. def predict(
  24. self,
  25. weight_path: str,
  26. input_path: str,
  27. device: str = "gpu",
  28. save_dir: str = None,
  29. **kwargs
  30. ) -> CompletedProcess:
  31. """predict using specified weight
  32. Args:
  33. weight_path (str): the path of model weight file used to predict.
  34. input_path (str): the path of image file to be predicted.
  35. device (str, optional): the running device. Defaults to 'gpu'.
  36. save_dir (str, optional): the directory path to save predict output. Defaults to None.
  37. Returns:
  38. CompletedProcess: the result of predicting subprocess execution.
  39. """
  40. config = self.config.copy()
  41. weight_path = abspath(weight_path)
  42. config.update_pretrained_weights(weight_path)
  43. input_path = abspath(input_path)
  44. config._update_infer_img(input_path)
  45. # TODO: Handle `device`
  46. logging.warning("`device` will not be used.")
  47. if save_dir is not None:
  48. save_dir = abspath(save_dir)
  49. else:
  50. save_dir = abspath(config.get_predict_save_dir())
  51. config._update_save_res_path(save_dir)
  52. self._assert_empty_kwargs(kwargs)
  53. with self._create_new_config_file() as config_path:
  54. config.dump(config_path)
  55. return self.runner.predict(config_path, [], device)
  56. def infer(
  57. self,
  58. model_dir: str,
  59. input_path: str,
  60. device: str = "gpu",
  61. save_dir: str = None,
  62. **kwargs
  63. ) -> CompletedProcess:
  64. """predict image using infernece model
  65. Args:
  66. model_dir (str): the directory path of inference model files that would use to predict.
  67. input_path (str): the path of image that would be predict.
  68. device (str, optional): the running device. Defaults to 'gpu'.
  69. save_dir (str, optional): the directory path to save output. Defaults to None.
  70. Returns:
  71. CompletedProcess: the result of infering subprocess execution.
  72. """
  73. config = self.config.copy()
  74. cli_args = []
  75. model_dir = abspath(model_dir)
  76. cli_args.append(CLIArgument("--table_model_dir", model_dir))
  77. input_path = abspath(input_path)
  78. cli_args.append(CLIArgument("--image_dir", input_path))
  79. device_type, _ = self.runner.parse_device(device)
  80. cli_args.append(CLIArgument("--use_gpu", str(device_type == "gpu")))
  81. if save_dir is not None:
  82. save_dir = abspath(save_dir)
  83. else:
  84. # `save_dir` is None
  85. save_dir = abspath(os.path.join("output", "infer"))
  86. cli_args.append(CLIArgument("--output", save_dir))
  87. dict_path = kwargs.pop("dict_path", None)
  88. if dict_path is not None:
  89. dict_path = abspath(dict_path)
  90. else:
  91. dict_path = config.get_label_dict_path()
  92. cli_args.append(CLIArgument("--table_char_dict_path", dict_path))
  93. model_type = config._get_model_type()
  94. cli_args.append(CLIArgument("--table_algorithm", model_type))
  95. infer_shape = config._get_infer_shape()
  96. if infer_shape is not None:
  97. cli_args.append(CLIArgument("--table_max_len", infer_shape))
  98. self._assert_empty_kwargs(kwargs)
  99. with self._create_new_config_file() as config_path:
  100. config.dump(config_path)
  101. return self.runner.infer(config_path, cli_args, device)