trainer.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 ..base import BaseTrainer
  15. from .model_list import MODELS
  16. class TableRecTrainer(BaseTrainer):
  17. """Table Recognition Model Trainer"""
  18. entities = MODELS
  19. def update_config(self):
  20. """update training config"""
  21. if self.train_config.log_interval:
  22. self.pdx_config.update_log_interval(self.train_config.log_interval)
  23. if self.train_config.eval_interval:
  24. self.pdx_config._update_eval_interval_by_epoch(
  25. self.train_config.eval_interval
  26. )
  27. if self.train_config.save_interval:
  28. self.pdx_config.update_save_interval(self.train_config.save_interval)
  29. self.pdx_config.update_dataset(
  30. self.global_config.dataset_dir, "PubTabTableRecDataset"
  31. )
  32. if self.train_config.pretrain_weight_path:
  33. self.pdx_config.update_pretrained_weights(
  34. self.train_config.pretrain_weight_path
  35. )
  36. if self.train_config.batch_size is not None:
  37. self.pdx_config.update_batch_size(self.train_config.batch_size)
  38. if self.train_config.learning_rate is not None:
  39. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  40. if self.train_config.epochs_iters is not None:
  41. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  42. if (
  43. self.train_config.resume_path is not None
  44. and self.train_config.resume_path != ""
  45. ):
  46. self.pdx_config._update_checkpoints(self.train_config.resume_path)
  47. if self.global_config.output is not None:
  48. self.pdx_config._update_output_dir(self.global_config.output)
  49. def get_train_kwargs(self) -> dict:
  50. """get key-value arguments of model training function
  51. Returns:
  52. dict: the arguments of training function.
  53. """
  54. return {
  55. "device": self.get_device(),
  56. "dy2st": self.train_config.get("dy2st", False),
  57. "amp": self.train_config.get("amp", "OFF"), # amp support 'O1', 'O2', 'OFF'
  58. }