engine.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 .modules.base import build_dataset_checker, build_trainer, build_evaluater, build_exportor, build_predictor
  16. from .utils.result_saver import try_except_decorator
  17. from .utils import config
  18. from .utils.errors import raise_unsupported_api_error
  19. class Engine(object):
  20. """ Engine """
  21. def __init__(self):
  22. args = config.parse_args()
  23. self.config = config.get_config(
  24. args.config, overrides=args.override, show=False)
  25. self.mode = self.config.Global.mode
  26. self.output = self.config.Global.output
  27. @try_except_decorator
  28. def run(self):
  29. """ the main function """
  30. if self.config.Global.mode == "check_dataset":
  31. dataset_checker = build_dataset_checker(self.config)
  32. return dataset_checker.check()
  33. elif self.config.Global.mode == "train":
  34. trainer = build_trainer(self.config)
  35. trainer.train()
  36. elif self.config.Global.mode == "evaluate":
  37. evaluator = build_evaluater(self.config)
  38. return evaluator.evaluate()
  39. elif self.config.Global.mode == "export":
  40. exportor = build_exportor(self.config)
  41. return exportor.export()
  42. elif self.config.Global.mode == "predict":
  43. predictor = build_predictor(self.config)
  44. return predictor.predict()
  45. else:
  46. raise_unsupported_api_error(f"{self.config.Global.mode}",
  47. self.__class__)