trainer.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 os
  15. from ..base import BaseTrainer
  16. from .model_list import MODELS
  17. class UadTrainer(BaseTrainer):
  18. """Uad Model Trainer"""
  19. entities = MODELS
  20. def update_config(self):
  21. """update training config"""
  22. self.pdx_config.update_dataset(self.global_config.dataset_dir, "SegDataset")
  23. if self.train_config.num_classes is not None:
  24. self.pdx_config.update_num_classes(self.train_config.num_classes)
  25. if (
  26. self.train_config.pretrain_weight_path
  27. and self.train_config.pretrain_weight_path != ""
  28. ):
  29. self.pdx_config.update_pretrained_weights(
  30. self.train_config.pretrain_weight_path, is_backbone=True
  31. )
  32. def get_train_kwargs(self) -> dict:
  33. """get key-value arguments of model training function
  34. Returns:
  35. dict: the arguments of training function.
  36. """
  37. train_args = {"device": self.get_device()}
  38. # XXX:
  39. os.environ.pop("FLAGS_npu_jit_compile", None)
  40. if self.train_config.batch_size is not None:
  41. train_args["batch_size"] = self.train_config.batch_size
  42. if self.train_config.learning_rate is not None:
  43. train_args["learning_rate"] = self.train_config.learning_rate
  44. if self.train_config.epochs_iters is not None:
  45. train_args["epochs_iters"] = self.train_config.epochs_iters
  46. if (
  47. self.train_config.resume_path is not None
  48. and self.train_config.resume_path != ""
  49. ):
  50. train_args["resume_path"] = self.train_config.resume_path
  51. if self.global_config.output is not None:
  52. train_args["save_dir"] = self.global_config.output
  53. if self.train_config.log_interval:
  54. train_args["log_iters"] = self.train_config.log_interval
  55. if self.train_config.eval_interval:
  56. train_args["do_eval"] = True
  57. train_args["save_interval"] = self.train_config.eval_interval
  58. train_args["dy2st"] = self.train_config.get("dy2st", False)
  59. return train_args