trainer.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = self.pdx_config.TrainDataset['data_fields'] if 'data_fields' in self.pdx_config.TrainDataset else None
  26. self.pdx_config.update_dataset(
  27. self.global_config.dataset_dir, "COCODetDataset",
  28. data_fields=data_fields,
  29. metric=metric,
  30. )
  31. def update_config(self):
  32. """update training config"""
  33. if self.train_config.log_interval:
  34. self.pdx_config.update_log_interval(self.train_config.log_interval)
  35. if self.train_config.eval_interval:
  36. self.pdx_config.update_eval_interval(self.train_config.eval_interval)
  37. self._update_dataset()
  38. if self.train_config.num_classes is not None:
  39. self.pdx_config.update_num_class(self.train_config.num_classes)
  40. if (
  41. self.train_config.pretrain_weight_path
  42. and self.train_config.pretrain_weight_path != ""
  43. ):
  44. self.pdx_config.update_pretrained_weights(
  45. self.train_config.pretrain_weight_path
  46. )
  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(self.train_config.learning_rate)
  51. if self.train_config.epochs_iters is not None:
  52. self.pdx_config.update_epochs(self.train_config.epochs_iters)
  53. epochs_iters = self.train_config.epochs_iters
  54. else:
  55. epochs_iters = self.pdx_config.get_epochs_iters()
  56. if self.global_config.output is not None:
  57. self.pdx_config.update_save_dir(self.global_config.output)
  58. if "PicoDet" in self.global_config.model:
  59. assigner_epochs = max(int(epochs_iters / 10), 1)
  60. try:
  61. self.pdx_config.update_static_assigner_epochs(assigner_epochs)
  62. except Exception:
  63. logging.info(
  64. f"The model({self.global_config.model}) don't support to update_static_assigner_epochs!"
  65. )
  66. def get_train_kwargs(self) -> dict:
  67. """get key-value arguments of model training function
  68. Returns:
  69. dict: the arguments of training function.
  70. """
  71. train_args = {"device": self.get_device()}
  72. if (
  73. self.train_config.resume_path is not None
  74. and self.train_config.resume_path != ""
  75. ):
  76. train_args["resume_path"] = self.train_config.resume_path
  77. train_args["dy2st"] = self.train_config.get("dy2st", False)
  78. return train_args