base_seg_config.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 urllib.parse import urlparse
  15. import yaml
  16. from paddleseg.utils import NoAliasDumper
  17. from paddleseg.cvlibs.config import parse_from_yaml, merge_config_dicts
  18. from ..base import BaseConfig
  19. from ...utils.misc import abspath
  20. class BaseSegConfig(BaseConfig):
  21. """BaseSegConfig"""
  22. def update(self, dict_like_obj):
  23. """update"""
  24. dict_ = merge_config_dicts(dict_like_obj, self.dict)
  25. self.reset_from_dict(dict_)
  26. def load(self, config_path):
  27. """load"""
  28. dict_ = parse_from_yaml(config_path)
  29. if not isinstance(dict_, dict):
  30. raise TypeError
  31. self.reset_from_dict(dict_)
  32. def dump(self, config_path):
  33. """dump"""
  34. with open(config_path, "w", encoding="utf-8") as f:
  35. yaml.dump(self.dict, f, Dumper=NoAliasDumper)
  36. def update_learning_rate(self, learning_rate):
  37. """update_learning_rate"""
  38. if "lr_scheduler" not in self:
  39. raise RuntimeError(
  40. "Not able to update learning rate, because no LR scheduler config was found."
  41. )
  42. self.lr_scheduler["learning_rate"] = learning_rate
  43. def update_batch_size(self, batch_size, mode="train"):
  44. """update_batch_size"""
  45. if mode == "train":
  46. self.set_val("batch_size", batch_size)
  47. else:
  48. raise ValueError(
  49. f"Setting `batch_size` in {repr(mode)} mode is not supported."
  50. )
  51. def update_log_ranks(self, device):
  52. """update log ranks
  53. Args:
  54. device (str): the running device to set
  55. """
  56. log_ranks = device.split(":")[1]
  57. self.set_val("log_ranks", log_ranks)
  58. def update_print_mem_info(self, print_mem_info: bool):
  59. """setting print memory info"""
  60. assert isinstance(print_mem_info, bool), "print_mem_info should be a bool"
  61. self.set_val("print_mem_info", print_mem_info)
  62. def update_shuffle(self, shuffle: bool):
  63. """setting print memory info"""
  64. assert isinstance(shuffle, bool), "shuffle should be a bool"
  65. self.set_val("shuffle", shuffle)
  66. def update_pretrained_weights(self, weight_path, is_backbone=False):
  67. """update_pretrained_weights"""
  68. if "model" not in self:
  69. raise RuntimeError(
  70. "Not able to update pretrained weight path, because no model config was found."
  71. )
  72. if isinstance(weight_path, str):
  73. if urlparse(weight_path).scheme == "":
  74. # If `weight_path` is a string but not URL (with scheme present),
  75. # it will be recognized as a local file path.
  76. weight_path = abspath(weight_path)
  77. else:
  78. if weight_path is not None:
  79. raise TypeError("`weight_path` must be string or None.")
  80. if is_backbone:
  81. if "backbone" not in self.model:
  82. raise RuntimeError(
  83. "Not able to update pretrained weight path of backbone, because no backbone config was found."
  84. )
  85. self.model["backbone"]["pretrained"] = weight_path
  86. else:
  87. self.model["pretrained"] = weight_path
  88. def update_dy2st(self, dy2st):
  89. """update_dy2st"""
  90. self.set_val("to_static_training", dy2st)
  91. def update_dataset(self, dataset_dir, dataset_type=None):
  92. """update_dataset"""
  93. raise NotImplementedError
  94. def get_epochs_iters(self):
  95. """get_epochs_iters"""
  96. raise NotImplementedError
  97. def get_learning_rate(self):
  98. """get_learning_rate"""
  99. raise NotImplementedError
  100. def get_batch_size(self, mode="train"):
  101. """get_batch_size"""
  102. raise NotImplementedError
  103. def get_qat_epochs_iters(self):
  104. """get_qat_epochs_iters"""
  105. return self.get_epochs_iters() // 2
  106. def get_qat_learning_rate(self):
  107. """get_qat_learning_rate"""
  108. return self.get_learning_rate() / 2