runner.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. import os
  12. import tempfile
  13. from ...base import BaseRunner
  14. from ...base.utils.arg import CLIArgument, gather_opts_args
  15. from ...base.utils.subprocess import CompletedProcess
  16. class InstanceSegRunner(BaseRunner):
  17. """ InstanceSegRunner """
  18. def train(self,
  19. config_path: str,
  20. cli_args: list,
  21. device: str,
  22. ips: str,
  23. save_dir: str,
  24. do_eval=True) -> CompletedProcess:
  25. """train model
  26. Args:
  27. config_path (str): the config file path used to train.
  28. cli_args (list): the additional parameters.
  29. device (str): the training device.
  30. ips (str): the ip addresses of nodes when using distribution.
  31. save_dir (str): the directory path to save training output.
  32. do_eval (bool, optional): whether or not to evaluate model during training. Defaults to True.
  33. Returns:
  34. CompletedProcess: the result of training subprocess execution.
  35. """
  36. args, env = self.distributed(device, ips, log_dir=save_dir)
  37. cli_args = self._gather_opts_args(cli_args)
  38. cmd = [*args, 'tools/train.py']
  39. if do_eval:
  40. cmd.append('--eval')
  41. cmd.extend(['--config', config_path, *cli_args])
  42. return self.run_cmd(
  43. cmd,
  44. env=env,
  45. switch_wdir=True,
  46. echo=True,
  47. silent=False,
  48. capture_output=True,
  49. log_path=self._get_train_log_path(save_dir))
  50. def evaluate(self, config_path: str, cli_args: list, device: str,
  51. ips: str) -> CompletedProcess:
  52. """run model evaluating
  53. Args:
  54. config_path (str): the config file path used to evaluate.
  55. cli_args (list): the additional parameters.
  56. device (str): the evaluating device.
  57. ips (str): the ip addresses of nodes when using distribution.
  58. Returns:
  59. CompletedProcess: the result of evaluating subprocess execution.
  60. """
  61. args, env = self.distributed(device, ips)
  62. cli_args = self._gather_opts_args(cli_args)
  63. cmd = [*args, 'tools/eval.py', '--config', config_path, *cli_args]
  64. cp = self.run_cmd(
  65. cmd,
  66. env=env,
  67. switch_wdir=True,
  68. echo=True,
  69. silent=False,
  70. capture_output=True)
  71. if cp.returncode == 0:
  72. metric_dict = _extract_eval_metrics(cp.stdout)
  73. cp.metrics = metric_dict
  74. return cp
  75. def predict(self, config_path: str, cli_args: list,
  76. device: str) -> CompletedProcess:
  77. """run predicting using dynamic mode
  78. Args:
  79. config_path (str): the config file path used to predict.
  80. cli_args (list): the additional parameters.
  81. device (str): unused.
  82. Returns:
  83. CompletedProcess: the result of predicting subprocess execution.
  84. """
  85. # `device` unused
  86. cli_args = self._gather_opts_args(cli_args)
  87. cmd = [self.python, 'tools/infer.py', '-c', config_path, *cli_args]
  88. return self.run_cmd(cmd, switch_wdir=True, echo=True, silent=False)
  89. def export(self, config_path: str, cli_args: list,
  90. device: str) -> CompletedProcess:
  91. """run exporting
  92. Args:
  93. config_path (str): the path of config file used to export.
  94. cli_args (list): the additional parameters.
  95. device (str): unused.
  96. save_dir (str, optional): the directory path to save exporting output. Defaults to None.
  97. Returns:
  98. CompletedProcess: the result of exporting subprocess execution.
  99. """
  100. # `device` unused
  101. cli_args = self._gather_opts_args(cli_args)
  102. cmd = [
  103. self.python, 'tools/export_model.py', '--for_fd', '-c', config_path,
  104. *cli_args
  105. ]
  106. cp = self.run_cmd(cmd, switch_wdir=True, echo=True, silent=False)
  107. return cp
  108. def infer(self, cli_args: list, device: str) -> CompletedProcess:
  109. """run predicting using inference model
  110. Args:
  111. cli_args (list): the additional parameters.
  112. device (str): unused.
  113. Returns:
  114. CompletedProcess: the result of infering subprocess execution.
  115. """
  116. # `device` unused
  117. cmd = [
  118. self.python, 'deploy/python/infer.py', '--use_fd_format', *cli_args
  119. ]
  120. return self.run_cmd(cmd, switch_wdir=True, echo=True, silent=False)
  121. def compression(self,
  122. config_path: str,
  123. train_cli_args: list,
  124. export_cli_args: list,
  125. device: str,
  126. train_save_dir: str) -> CompletedProcess:
  127. """run compression model
  128. Args:
  129. config_path (str): the path of config file used to predict.
  130. train_cli_args (list): the additional training parameters.
  131. export_cli_args (list): the additional exporting parameters.
  132. device (str): the running device.
  133. train_save_dir (str): the directory path to save output.
  134. Returns:
  135. CompletedProcess: the result of compression subprocess execution.
  136. """
  137. args, env = self.distributed(device, log_dir=train_save_dir)
  138. train_cli_args = self._gather_opts_args(train_cli_args)
  139. cmd = [*args, 'tools/train.py', '-c', config_path, *train_cli_args]
  140. cp_train = self.run_cmd(
  141. cmd,
  142. env=env,
  143. switch_wdir=True,
  144. echo=True,
  145. silent=False,
  146. capture_output=True,
  147. log_path=self._get_train_log_path(train_save_dir))
  148. cps_weight_path = os.path.join(train_save_dir, 'model_final')
  149. export_cli_args.append(CLIArgument('-o', f"weights={cps_weight_path}"))
  150. export_cli_args = self._gather_opts_args(export_cli_args)
  151. cmd = [
  152. self.python, 'tools/export_model.py', '--for_fd', '-c', config_path,
  153. *export_cli_args
  154. ]
  155. cp_export = self.run_cmd(cmd, switch_wdir=True, echo=True, silent=False)
  156. return cp_train, cp_export
  157. def _gather_opts_args(self, args):
  158. """ _gather_opts_args """
  159. return gather_opts_args(args, '-o')
  160. def _extract_eval_metrics(stdout):
  161. """extract evaluation metrics from training log
  162. Args:
  163. stdout (str): the training log
  164. Returns:
  165. dict: the training metric
  166. """
  167. import re
  168. pattern = r'.*\(AP\)\s*@\[\s*IoU=0\.50:0\.95\s*\|\s*area=\s*all\s\|\smaxDets=\s*\d+\s\]\s*=\s*[0-1]?\.[0-9]{3}$'
  169. key = 'AP'
  170. metric_dict = dict()
  171. pattern = re.compile(pattern)
  172. # TODO: Use lazy version to make it more efficient
  173. lines = stdout.splitlines()
  174. metric_dict[key] = 0
  175. for line in lines:
  176. match = pattern.search(line)
  177. if match:
  178. metric_dict[key] = float(match.group(0)[-5:])
  179. return metric_dict