trainer.py 2.6 KB

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