trainer.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 MLClsTrainer(BaseTrainer):
  19. """Image Classification 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(self.train_config.eval_interval)
  34. if self.train_config.save_interval:
  35. self.pdx_config.update_save_interval(self.train_config.save_interval)
  36. self.pdx_config.update_dataset(self.global_config.dataset_dir, "MLClsDataset")
  37. if self.train_config.num_classes is not None:
  38. self.pdx_config.update_num_classes(self.train_config.num_classes)
  39. if (
  40. self.train_config.pretrain_weight_path
  41. and self.train_config.pretrain_weight_path != ""
  42. ):
  43. self.pdx_config.update_pretrained_weights(
  44. self.train_config.pretrain_weight_path
  45. )
  46. label_dict_path = Path(self.global_config.dataset_dir).joinpath("label.txt")
  47. if label_dict_path.exists():
  48. self.dump_label_dict(label_dict_path)
  49. if self.train_config.batch_size is not None:
  50. self.pdx_config.update_batch_size(self.train_config.batch_size)
  51. if self.train_config.learning_rate is not None:
  52. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  53. if self.train_config.epochs_iters is not None:
  54. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  55. if self.train_config.warmup_steps is not None:
  56. self.pdx_config.update_warmup_epochs(self.train_config.warmup_steps)
  57. if self.global_config.output is not None:
  58. self.pdx_config._update_output_dir(self.global_config.output)
  59. def get_train_kwargs(self) -> dict:
  60. """get key-value arguments of model training function
  61. Returns:
  62. dict: the arguments of training function.
  63. """
  64. train_args = {"device": self.get_device()}
  65. if (
  66. self.train_config.resume_path is not None
  67. and self.train_config.resume_path != ""
  68. ):
  69. train_args["resume_path"] = self.train_config.resume_path
  70. train_args["dy2st"] = self.train_config.get("dy2st", False)
  71. return train_args