trainer.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. import shutil
  16. from pathlib import Path
  17. import lazy_paddle as paddle
  18. from ..base import BaseTrainer
  19. from ...utils.config import AttrDict
  20. from .model_list import MODELS
  21. from ..formula_recognition.model_list import MODELS as MODELS_LaTeX
  22. MODELS = MODELS + MODELS_LaTeX
  23. class TextRecTrainer(BaseTrainer):
  24. """Text Recognition Model Trainer"""
  25. entities = MODELS
  26. def dump_label_dict(self, src_label_dict_path: str):
  27. """dump label dict config
  28. Args:
  29. src_label_dict_path (str): path to label dict file to be saved.
  30. """
  31. dst_label_dict_path = Path(self.global_config.output).joinpath("label_dict.txt")
  32. shutil.copyfile(src_label_dict_path, dst_label_dict_path)
  33. def update_config(self):
  34. """update training config"""
  35. if self.train_config.log_interval:
  36. self.pdx_config.update_log_interval(self.train_config.log_interval)
  37. if self.train_config.eval_interval:
  38. self.pdx_config._update_eval_interval_by_epoch(
  39. self.train_config.eval_interval
  40. )
  41. if self.train_config.save_interval:
  42. self.pdx_config.update_save_interval(self.train_config.save_interval)
  43. if self.global_config["model"] == "LaTeX_OCR_rec":
  44. self.pdx_config.update_dataset(
  45. self.global_config.dataset_dir, "LaTeXOCRDataSet"
  46. )
  47. else:
  48. self.pdx_config.update_dataset(
  49. self.global_config.dataset_dir, "MSTextRecDataset"
  50. )
  51. label_dict_path = Path(self.global_config.dataset_dir).joinpath("dict.txt")
  52. if label_dict_path.exists():
  53. self.pdx_config.update_label_dict_path(label_dict_path)
  54. self.dump_label_dict(label_dict_path)
  55. if self.train_config.pretrain_weight_path:
  56. self.pdx_config.update_pretrained_weights(
  57. self.train_config.pretrain_weight_path
  58. )
  59. if self.global_config["model"] == "LaTeX_OCR_rec":
  60. if (
  61. self.train_config.batch_size_train is not None
  62. and self.train_config.batch_size_val
  63. ):
  64. self.pdx_config.update_batch_size_pair(
  65. self.train_config.batch_size_train, self.train_config.batch_size_val
  66. )
  67. else:
  68. if self.train_config.batch_size is not None:
  69. self.pdx_config.update_batch_size(self.train_config.batch_size)
  70. if self.train_config.learning_rate is not None:
  71. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  72. if self.train_config.epochs_iters is not None:
  73. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  74. if (
  75. self.train_config.resume_path is not None
  76. and self.train_config.resume_path != ""
  77. ):
  78. self.pdx_config._update_checkpoints(self.train_config.resume_path)
  79. if self.global_config.output is not None:
  80. self.pdx_config._update_output_dir(self.global_config.output)
  81. def get_train_kwargs(self) -> dict:
  82. """get key-value arguments of model training function
  83. Returns:
  84. dict: the arguments of training function.
  85. """
  86. return {
  87. "device": self.get_device(),
  88. "dy2st": self.train_config.get("dy2st", False),
  89. }