trainer_ml.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import paddle
  17. from pathlib import Path
  18. from ..base import BaseTrainer, BaseTrainDeamon
  19. from .trainer import ClsTrainer, ClsTrainDeamon
  20. from .model_list import ML_MODELS
  21. from ...utils.config import AttrDict
  22. class MLClsTrainer(ClsTrainer, BaseTrainer):
  23. """ Multi Label Image Classification Model Trainer """
  24. entities = ML_MODELS
  25. def update_config(self):
  26. """update training config
  27. """
  28. if self.train_config.log_interval:
  29. self.pdx_config.update_log_interval(self.train_config.log_interval)
  30. if self.train_config.eval_interval:
  31. self.pdx_config.update_eval_interval(
  32. self.train_config.eval_interval)
  33. if self.train_config.save_interval:
  34. self.pdx_config.update_save_interval(
  35. self.train_config.save_interval)
  36. self.pdx_config.update_dataset(self.global_config.dataset_dir,
  37. "MLClsDataset")
  38. if self.train_config.num_classes is not None:
  39. self.pdx_config.update_num_classes(self.train_config.num_classes)
  40. if self.train_config.pretrain_weight_path and self.train_config.pretrain_weight_path != "":
  41. self.pdx_config.update_pretrained_weights(
  42. self.train_config.pretrain_weight_path)
  43. label_dict_path = Path(self.global_config.dataset_dir).joinpath(
  44. "label.txt")
  45. if label_dict_path.exists():
  46. self.dump_label_dict(label_dict_path)
  47. if self.train_config.batch_size is not None:
  48. self.pdx_config.update_batch_size(self.train_config.batch_size)
  49. if self.train_config.learning_rate is not None:
  50. self.pdx_config.update_learning_rate(
  51. self.train_config.learning_rate)
  52. if self.train_config.epochs_iters is not None:
  53. self.pdx_config._update_epochs(self.train_config.epochs_iters)
  54. if self.train_config.warmup_steps is not None:
  55. self.pdx_config.update_warmup_epochs(self.train_config.warmup_steps)
  56. if self.global_config.output is not None:
  57. self.pdx_config._update_output_dir(self.global_config.output)