trainer.py 2.7 KB

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