engine.py 2.0 KB

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