model.py 4.3 KB

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