trainer.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 VideoClsTrainer(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(
  39. self.global_config.dataset_dir, "VideoClsDataset"
  40. )
  41. if self.train_config.num_classes is not None:
  42. self.pdx_config.update_num_classes(self.train_config.num_classes)
  43. if self.train_config.pretrain_weight_path != "":
  44. self.pdx_config.update_pretrained_weights(
  45. self.train_config.pretrain_weight_path
  46. )
  47. label_dict_path = Path(self.global_config.dataset_dir).joinpath("label.txt")
  48. if label_dict_path.exists():
  49. self.dump_label_dict(label_dict_path)
  50. if self.train_config.batch_size is not None:
  51. self.pdx_config.update_batch_size(
  52. self.train_config.batch_size, mode="train"
  53. )
  54. if self.eval_config.batch_size is not None:
  55. self.pdx_config.update_batch_size(self.eval_config.batch_size, mode="eval")
  56. if self.train_config.learning_rate is not None:
  57. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  58. if self.train_config.epochs_iters is not None:
  59. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  60. if self.train_config.warmup_steps is not None:
  61. self.pdx_config.update_warmup_epochs(self.train_config.warmup_steps)
  62. if self.global_config.output is not None:
  63. self.pdx_config._update_output_dir(self.global_config.output)
  64. def get_train_kwargs(self) -> dict:
  65. """get key-value arguments of model training function
  66. Returns:
  67. dict: the arguments of training function.
  68. """
  69. train_args = {"device": self.get_device()}
  70. if (
  71. self.train_config.resume_path is not None
  72. and self.train_config.resume_path != ""
  73. ):
  74. train_args["resume_path"] = self.train_config.resume_path
  75. train_args["dy2st"] = self.train_config.get("dy2st", False)
  76. return train_args