trainer.py 3.7 KB

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