engine.py 1.7 KB

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