trainer.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 pathlib import Path
  16. import paddle
  17. from ..base import BaseTrainer, BaseTrainDeamon
  18. from ...utils.config import AttrDict
  19. from .support_models import SUPPORT_MODELS
  20. class TableRecTrainer(BaseTrainer):
  21. """ Table Recognition Model Trainer """
  22. support_models = SUPPORT_MODELS
  23. def build_deamon(self, config: AttrDict) -> "TableRecTrainDeamon":
  24. """build deamon thread for saving training outputs timely
  25. Args:
  26. config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.
  27. Returns:
  28. TableRecTrainDeamon: the training deamon thread object for saving training outputs timely.
  29. """
  30. return TableRecTrainDeamon(config)
  31. def update_config(self):
  32. """update training config
  33. """
  34. if self.train_config.log_interval:
  35. self.pdx_config.update_log_interval(self.train_config.log_interval)
  36. if self.train_config.eval_interval:
  37. self.pdx_config._update_eval_interval_by_epoch(
  38. self.train_config.eval_interval)
  39. if self.train_config.save_interval:
  40. self.pdx_config.update_save_interval(
  41. self.train_config.save_interval)
  42. self.pdx_config.update_dataset(self.global_config.dataset_dir,
  43. "PubTabTableRecDataset")
  44. if self.train_config.pretrain_weight_path:
  45. self.pdx_config.update_pretrained_weights(
  46. self.train_config.pretrain_weight_path)
  47. if self.train_config.batch_size is not None:
  48. self.pdx_config.update_batch_size(self.train_config.batch_size)
  49. if self.train_config.learning_rate is not None:
  50. self.pdx_config.update_learning_rate(
  51. self.train_config.learning_rate)
  52. if self.train_config.epochs_iters is not None:
  53. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  54. if self.train_config.resume_path is not None and self.train_config.resume_path != "":
  55. self.pdx_config._update_checkpoints(self.train_config.resume_path)
  56. if self.global_config.output is not None:
  57. self.pdx_config._update_output_dir(self.global_config.output)
  58. def get_train_kwargs(self) -> dict:
  59. """get key-value arguments of model training function
  60. Returns:
  61. dict: the arguments of training function.
  62. """
  63. return {"device": self.get_device()}
  64. class TableRecTrainDeamon(BaseTrainDeamon):
  65. """ TableRecTrainDeamon """
  66. def __init__(self, *args, **kwargs):
  67. super().__init__(*args, **kwargs)
  68. def get_the_pdparams_suffix(self):
  69. """ get the suffix of pdparams file """
  70. return "pdparams"
  71. def get_the_pdema_suffix(self):
  72. """ get the suffix of pdema file """
  73. return "pdema"
  74. def get_the_pdopt_suffix(self):
  75. """ get the suffix of pdopt file """
  76. return "pdopt"
  77. def get_the_pdstates_suffix(self):
  78. """ get the suffix of pdstates file """
  79. return "states"
  80. def get_ith_ckp_prefix(self, epoch_id):
  81. """ get the prefix of the epoch_id checkpoint file """
  82. return f"iter_epoch_{epoch_id}"
  83. def get_best_ckp_prefix(self):
  84. """ get the prefix of the best checkpoint file """
  85. return "best_accuracy"
  86. def get_score(self, pdstates_path):
  87. """ get the score by pdstates file """
  88. if not Path(pdstates_path).exists():
  89. return 0
  90. return paddle.load(pdstates_path)['best_model_dict']['acc']
  91. def get_epoch_id_by_pdparams_prefix(self, pdparams_prefix):
  92. """ get the epoch_id by pdparams file """
  93. return int(pdparams_prefix.split(".")[0].split("_")[-1])