trainer.py 3.8 KB

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