engine.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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. from .model import _ModelBasedConfig
  15. from .utils.config import get_config, parse_args
  16. from .utils.errors import raise_unsupported_api_error
  17. from .utils.flags import INFER_BENCHMARK
  18. from .utils.lazy_loader import disable_pir_bydefault
  19. from .utils.result_saver import try_except_decorator
  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. if INFER_BENCHMARK:
  45. continue
  46. res.print()
  47. if self._output:
  48. res.save_all(save_path=self._output)
  49. else:
  50. raise_unsupported_api_error(f"{self._mode}", self.__class__)