runner.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 ..cls import ClsRunner
  17. from ...base.utils.subprocess import CompletedProcess
  18. class ShiTuRecRunner(ClsRunner):
  19. """ShiTuRec Runner"""
  20. pass
  21. def _extract_eval_metrics(stdout: str) -> dict:
  22. """extract evaluation metrics from training log
  23. Args:
  24. stdout (str): the training log
  25. Returns:
  26. dict: the training metric
  27. """
  28. import re
  29. _DP = r"[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
  30. patterns = [
  31. r"\[Eval\]\[Epoch 0\]\[Avg\].*top1: (_dp), top5: (_dp)".replace("_dp", _DP),
  32. r"\[Eval\]\[Epoch 0\]\[Avg\].*recall1: (_dp), recall5: (_dp), mAP: (_dp)".replace(
  33. "_dp", _DP
  34. ),
  35. ]
  36. keys = [["val.top1", "val.top5"], ["recall1", "recall5", "mAP"]]
  37. metric_dict = dict()
  38. for pattern, key in zip(patterns, keys):
  39. pattern = re.compile(pattern)
  40. for line in stdout.splitlines():
  41. match = pattern.search(line)
  42. if match:
  43. for k, v in zip(key, map(float, match.groups())):
  44. metric_dict[k] = v
  45. return metric_dict