trainer.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  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 shutil
  15. from pathlib import Path
  16. from ..base import BaseTrainer
  17. from .model_list import MODELS
  18. class FormulaRecTrainer(BaseTrainer):
  19. """Text Recognition Model Trainer"""
  20. entities = MODELS
  21. def dump_label_dict(self, src_label_dict_path: str):
  22. """dump label dict config
  23. Args:
  24. src_label_dict_path (str): path to label dict file to be saved.
  25. """
  26. dst_label_dict_path = Path(self.global_config.output).joinpath("label_dict.txt")
  27. shutil.copyfile(src_label_dict_path, dst_label_dict_path)
  28. def update_config(self):
  29. """update training config"""
  30. if self.train_config.log_interval:
  31. self.pdx_config.update_log_interval(self.train_config.log_interval)
  32. if self.train_config.eval_interval:
  33. self.pdx_config._update_eval_interval_by_epoch(
  34. self.train_config.eval_interval
  35. )
  36. if self.train_config.save_interval:
  37. self.pdx_config.update_save_interval(self.train_config.save_interval)
  38. if self.global_config["model"] == "LaTeX_OCR_rec":
  39. self.pdx_config.update_dataset(
  40. self.global_config.dataset_dir, "LaTeXOCRDataSet"
  41. )
  42. elif self.global_config["model"] in (
  43. "UniMERNet",
  44. "PP-FormulaNet-L",
  45. "PP-FormulaNet-S",
  46. "PP-FormulaNet_plus-L",
  47. "PP-FormulaNet_plus-M",
  48. "PP-FormulaNet_plus-S",
  49. ):
  50. self.pdx_config.update_dataset(
  51. self.global_config.dataset_dir, "SimpleDataSet"
  52. )
  53. label_dict_path = Path(self.global_config.dataset_dir).joinpath("dict.txt")
  54. if label_dict_path.exists():
  55. self.pdx_config.update_label_dict_path(label_dict_path)
  56. self.dump_label_dict(label_dict_path)
  57. if self.train_config.pretrain_weight_path:
  58. self.pdx_config.update_pretrained_weights(
  59. self.train_config.pretrain_weight_path
  60. )
  61. if self.train_config.batch_size is not None:
  62. if self.global_config["model"] == "LaTeX_OCR_rec":
  63. self.pdx_config.update_batch_size_pair(
  64. self.train_config.batch_size, mode="train"
  65. )
  66. else:
  67. self.pdx_config.update_batch_size(
  68. self.train_config.batch_size, mode="train"
  69. )
  70. if self.eval_config.batch_size is not None:
  71. if self.global_config["model"] == "LaTeX_OCR_rec":
  72. self.pdx_config.update_batch_size_pair(
  73. self.eval_config.batch_size, mode="eval"
  74. )
  75. else:
  76. self.pdx_config.update_batch_size(
  77. self.eval_config.batch_size, mode="eval"
  78. )
  79. if self.train_config.learning_rate is not None:
  80. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  81. if self.train_config.get("delimiter", None) is not None:
  82. self.pdx_config.update_delimiter(self.train_config.delimiter, mode="train")
  83. if self.eval_config.get("delimiter", None) is not None:
  84. self.pdx_config.update_delimiter(self.eval_config.delimiter, mode="eval")
  85. if self.train_config.epochs_iters is not None:
  86. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  87. if (
  88. self.train_config.resume_path is not None
  89. and self.train_config.resume_path != ""
  90. ):
  91. self.pdx_config._update_checkpoints(self.train_config.resume_path)
  92. if self.global_config.output is not None:
  93. self.pdx_config._update_output_dir(self.global_config.output)
  94. def get_train_kwargs(self) -> dict:
  95. """get key-value arguments of model training function
  96. Returns:
  97. dict: the arguments of training function.
  98. """
  99. return {
  100. "device": self.get_device(),
  101. "dy2st": self.train_config.get("dy2st", False),
  102. "amp": self.train_config.get("amp", "OFF"), # amp support 'O1', 'O2', 'OFF'
  103. }