runner.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. from ...base import BaseRunner
  15. from ...base.utils.arg import gather_opts_args
  16. from ...base.utils.subprocess import CompletedProcess
  17. from ....utils.errors import raise_unsupported_api_error
  18. class TSCLSRunner(BaseRunner):
  19. """ TS Classify Runner """
  20. def train(self,
  21. config_path: str,
  22. cli_args: list,
  23. device: str,
  24. ips: str,
  25. save_dir: str,
  26. do_eval=True) -> CompletedProcess:
  27. """train model
  28. Args:
  29. config_path (str): the config file path used to train.
  30. cli_args (list): the additional parameters.
  31. device (str): the training device.
  32. ips (str): the ip addresses of nodes when using distribution.
  33. save_dir (str): the directory path to save training output.
  34. do_eval (bool, optional): whether or not to evaluate model during training. Defaults to True.
  35. Returns:
  36. CompletedProcess: the result of training subprocess execution.
  37. """
  38. args, env = self.distributed(device, ips, log_dir=save_dir)
  39. cli_args = self._gather_opts_args(cli_args)
  40. cmd = [*args, 'tools/train.py', '--config', config_path, *cli_args]
  41. return self.run_cmd(
  42. cmd,
  43. env=env,
  44. switch_wdir=True,
  45. echo=True,
  46. silent=False,
  47. capture_output=True,
  48. log_path=self._get_train_log_path(save_dir))
  49. def evaluate(self, config_path: str, cli_args: list, device: str,
  50. ips: str) -> CompletedProcess:
  51. """run model evaluating
  52. Args:
  53. config_path (str): the config file path used to evaluate.
  54. cli_args (list): the additional parameters.
  55. device (str): the evaluating device.
  56. ips (str): the ip addresses of nodes when using distribution.
  57. Returns:
  58. CompletedProcess: the result of evaluating subprocess execution.
  59. """
  60. args, env = self.distributed(device, ips)
  61. cli_args = self._gather_opts_args(cli_args)
  62. cmd = [*args, 'tools/val.py', '--config', config_path, *cli_args]
  63. cp = self.run_cmd(
  64. cmd,
  65. env=env,
  66. switch_wdir=True,
  67. echo=True,
  68. silent=False,
  69. capture_output=True)
  70. if cp.returncode == 0:
  71. metric_dict = _extract_eval_metrics(cp.stderr)
  72. cp.metrics = metric_dict
  73. return cp
  74. def predict(self, config_path: str, cli_args: list,
  75. device: str) -> CompletedProcess:
  76. """run predicting using dynamic mode
  77. Args:
  78. config_path (str): the config file path used to predict.
  79. cli_args (list): the additional parameters.
  80. device (str): unused.
  81. Returns:
  82. CompletedProcess: the result of predicting subprocess execution.
  83. """
  84. # `device` unused
  85. cli_args = self._gather_opts_args(cli_args)
  86. cmd = [
  87. self.python, 'tools/predict.py', '--config', config_path, *cli_args
  88. ]
  89. return self.run_cmd(cmd, switch_wdir=True, echo=True, silent=False)
  90. def export(self, config_path, cli_args, device):
  91. """export
  92. """
  93. raise_unsupported_api_error('export', self.__class__)
  94. def infer(self, config_path, cli_args, device):
  95. """infer
  96. """
  97. raise_unsupported_api_error('infer', self.__class__)
  98. def compression(self, config_path, train_cli_args, export_cli_args, device,
  99. train_save_dir):
  100. """compression
  101. """
  102. raise_unsupported_api_error('compression', self.__class__)
  103. def _gather_opts_args(self, args):
  104. # Since `--opts` in PaddleSeg does not use `action='append'`
  105. # We collect and arrange all opts args here
  106. # e.g.: python tools/train.py --config xxx --opts a=1 c=3 --opts b=2
  107. # => python tools/train.py --config xxx c=3 --opts a=1 b=2
  108. return gather_opts_args(args, '--opts')
  109. def _extract_eval_metrics(stdout):
  110. """extract evaluation metrics from training log
  111. Args:
  112. stdout (str): the training log
  113. Returns:
  114. dict: the training metric
  115. """
  116. import re
  117. pattern = r'\'acc\':\s+(\d+\.\d+),+[\s|\n]+\'f1\':\s+(\d+\.\d+)'
  118. keys = ['acc', 'f1']
  119. metric_dict = dict()
  120. pattern = re.compile(pattern)
  121. lines = stdout.splitlines()
  122. for line in lines:
  123. match = pattern.search(line)
  124. if match:
  125. for k, v in zip(keys, map(float, match.groups())):
  126. metric_dict[k] = v
  127. return metric_dict