engine.py 1.9 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. from .utils.lazy_loader import disable_pir_bydefault
  16. from .utils.result_saver import try_except_decorator
  17. from .utils.config import parse_args, get_config
  18. from .utils.errors import raise_unsupported_api_error
  19. from .model import _ModelBasedConfig
  20. class Engine(object):
  21. """Engine"""
  22. def __init__(self):
  23. args = parse_args()
  24. config = get_config(args.config, overrides=args.override, show=False)
  25. self._mode = config.Global.mode
  26. self._output = config.Global.output
  27. self._model = _ModelBasedConfig(config)
  28. @try_except_decorator
  29. def run(self):
  30. """the main function"""
  31. if self._mode == "check_dataset":
  32. return self._model.check_dataset()
  33. elif self._mode == "train":
  34. disable_pir_bydefault()
  35. self._model.train()
  36. elif self._mode == "evaluate":
  37. disable_pir_bydefault()
  38. return self._model.evaluate()
  39. elif self._mode == "export":
  40. disable_pir_bydefault()
  41. return self._model.export()
  42. elif self._mode == "predict":
  43. for res in self._model.predict():
  44. res.print(json_format=False)
  45. if self._output:
  46. res.save_all(save_path=self._output)
  47. else:
  48. raise_unsupported_api_error(f"{self._mode}", self.__class__)