trainer.py 3.9 KB

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