runner.py 4.8 KB

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