trainer.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 json
  15. import shutil
  16. from pathlib import Path
  17. from ..base import BaseTrainer
  18. from .model_list import MODELS
  19. from ...utils.config import AttrDict
  20. class MLClsTrainer(BaseTrainer):
  21. """Image Classification Model Trainer"""
  22. entities = MODELS
  23. def dump_label_dict(self, src_label_dict_path: str):
  24. """dump label dict config
  25. Args:
  26. src_label_dict_path (str): path to label dict file to be saved.
  27. """
  28. dst_label_dict_path = Path(self.global_config.output).joinpath("label_dict.txt")
  29. shutil.copyfile(src_label_dict_path, dst_label_dict_path)
  30. def update_config(self):
  31. """update training config"""
  32. if self.train_config.log_interval:
  33. self.pdx_config.update_log_interval(self.train_config.log_interval)
  34. if self.train_config.eval_interval:
  35. self.pdx_config.update_eval_interval(self.train_config.eval_interval)
  36. if self.train_config.save_interval:
  37. self.pdx_config.update_save_interval(self.train_config.save_interval)
  38. self.pdx_config.update_dataset(self.global_config.dataset_dir, "MLClsDataset")
  39. if self.train_config.num_classes is not None:
  40. self.pdx_config.update_num_classes(self.train_config.num_classes)
  41. if (
  42. self.train_config.pretrain_weight_path
  43. and self.train_config.pretrain_weight_path != ""
  44. ):
  45. self.pdx_config.update_pretrained_weights(
  46. self.train_config.pretrain_weight_path
  47. )
  48. label_dict_path = Path(self.global_config.dataset_dir).joinpath("label.txt")
  49. if label_dict_path.exists():
  50. self.dump_label_dict(label_dict_path)
  51. if self.train_config.batch_size is not None:
  52. self.pdx_config.update_batch_size(self.train_config.batch_size)
  53. if self.train_config.learning_rate is not None:
  54. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  55. if self.train_config.epochs_iters is not None:
  56. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  57. if self.train_config.warmup_steps is not None:
  58. self.pdx_config.update_warmup_epochs(self.train_config.warmup_steps)
  59. if self.global_config.output is not None:
  60. self.pdx_config._update_output_dir(self.global_config.output)
  61. def get_train_kwargs(self) -> dict:
  62. """get key-value arguments of model training function
  63. Returns:
  64. dict: the arguments of training function.
  65. """
  66. train_args = {"device": self.get_device()}
  67. if (
  68. self.train_config.resume_path is not None
  69. and self.train_config.resume_path != ""
  70. ):
  71. train_args["resume_path"] = self.train_config.resume_path
  72. train_args["dy2st"] = self.train_config.get("dy2st", False)
  73. return train_args