trainer.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. from pathlib import Path
  16. from ...utils.misc import abspath
  17. from ..image_classification import ClsTrainer
  18. from .model_list import MODELS
  19. class FaceRecTrainer(ClsTrainer):
  20. """Face Recognition Model Trainer"""
  21. entities = MODELS
  22. def update_config(self):
  23. """update training config"""
  24. if self.train_config.log_interval:
  25. self.pdx_config.update_log_interval(self.train_config.log_interval)
  26. if self.train_config.eval_interval:
  27. self.pdx_config.update_eval_interval(self.train_config.eval_interval)
  28. if self.train_config.save_interval:
  29. self.pdx_config.update_save_interval(self.train_config.save_interval)
  30. self.update_dataset_cfg()
  31. if self.train_config.num_classes is not None:
  32. self.pdx_config.update_num_classes(self.train_config.num_classes)
  33. if self.train_config.pretrain_weight_path != "":
  34. self.pdx_config.update_pretrained_weights(
  35. self.train_config.pretrain_weight_path
  36. )
  37. label_dict_path = Path(self.global_config.dataset_dir).joinpath("label.txt")
  38. if label_dict_path.exists():
  39. self.dump_label_dict(label_dict_path)
  40. if self.train_config.batch_size is not None:
  41. self.pdx_config.update_batch_size(self.train_config.batch_size)
  42. if self.train_config.learning_rate is not None:
  43. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  44. if self.train_config.epochs_iters is not None:
  45. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  46. if self.train_config.warmup_steps is not None:
  47. self.pdx_config.update_warmup_epochs(self.train_config.warmup_steps)
  48. if self.global_config.output is not None:
  49. self.pdx_config._update_output_dir(self.global_config.output)
  50. def update_dataset_cfg(self):
  51. train_dataset_dir = abspath(
  52. os.path.join(self.global_config.dataset_dir, "train")
  53. )
  54. val_dataset_dir = abspath(os.path.join(self.global_config.dataset_dir, "val"))
  55. train_list_path = abspath(os.path.join(train_dataset_dir, "label.txt"))
  56. val_list_path = abspath(os.path.join(val_dataset_dir, "pair_label.txt"))
  57. ds_cfg = [
  58. f"DataLoader.Train.dataset.name=ClsDataset",
  59. f"DataLoader.Train.dataset.image_root={train_dataset_dir}",
  60. f"DataLoader.Train.dataset.cls_label_path={train_list_path}",
  61. f"DataLoader.Eval.dataset.name=FaceEvalDataset",
  62. f"DataLoader.Eval.dataset.dataset_root={val_dataset_dir}",
  63. f"DataLoader.Eval.dataset.pair_label_path={val_list_path}",
  64. ]
  65. self.pdx_config.update(ds_cfg)