trainer.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. from pathlib import Path
  15. from ..base import BaseTrainer
  16. from ...utils.config import AttrDict
  17. from ...utils import logging
  18. from .model_list import MODELS
  19. class DetTrainer(BaseTrainer):
  20. """Object Detection Model Trainer"""
  21. entities = MODELS
  22. def _update_dataset(self):
  23. """update dataset settings"""
  24. metric = self.pdx_config.metric if "metric" in self.pdx_config else "COCO"
  25. data_fields = (
  26. self.pdx_config.TrainDataset["data_fields"]
  27. if "data_fields" in self.pdx_config.TrainDataset
  28. else None
  29. )
  30. self.pdx_config.update_dataset(
  31. self.global_config.dataset_dir,
  32. "COCODetDataset",
  33. data_fields=data_fields,
  34. metric=metric,
  35. )
  36. def update_config(self):
  37. """update training config"""
  38. if self.train_config.log_interval:
  39. self.pdx_config.update_log_interval(self.train_config.log_interval)
  40. if self.train_config.eval_interval:
  41. self.pdx_config.update_eval_interval(self.train_config.eval_interval)
  42. self._update_dataset()
  43. if self.train_config.num_classes is not None:
  44. self.pdx_config.update_num_class(self.train_config.num_classes)
  45. if (
  46. self.train_config.pretrain_weight_path
  47. and self.train_config.pretrain_weight_path != ""
  48. ):
  49. self.pdx_config.update_pretrained_weights(
  50. self.train_config.pretrain_weight_path
  51. )
  52. if self.train_config.batch_size is not None:
  53. self.pdx_config.update_batch_size(self.train_config.batch_size)
  54. if self.train_config.learning_rate is not None:
  55. self.pdx_config.update_learning_rate(self.train_config.learning_rate)
  56. if self.train_config.epochs_iters is not None:
  57. self.pdx_config.update_epochs(self.train_config.epochs_iters)
  58. epochs_iters = self.train_config.epochs_iters
  59. else:
  60. epochs_iters = self.pdx_config.get_epochs_iters()
  61. if self.train_config.warmup_steps is not None:
  62. self.pdx_config.update_warmup_steps(self.train_config.warmup_steps)
  63. if self.global_config.output is not None:
  64. self.pdx_config.update_save_dir(self.global_config.output)
  65. if "PicoDet" in self.global_config.model:
  66. assigner_epochs = max(int(epochs_iters / 10), 1)
  67. try:
  68. self.pdx_config.update_static_assigner_epochs(assigner_epochs)
  69. except Exception:
  70. logging.info(
  71. f"The model({self.global_config.model}) don't support to update_static_assigner_epochs!"
  72. )
  73. def get_train_kwargs(self) -> dict:
  74. """get key-value arguments of model training function
  75. Returns:
  76. dict: the arguments of training function.
  77. """
  78. train_args = {"device": self.get_device()}
  79. if (
  80. self.train_config.resume_path is not None
  81. and self.train_config.resume_path != ""
  82. ):
  83. train_args["resume_path"] = self.train_config.resume_path
  84. train_args["dy2st"] = self.train_config.get("dy2st", False)
  85. return train_args