runner.py 7.4 KB

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