base_seg_config.py 4.5 KB

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