trainer.py 2.6 KB

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