config.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. import os
  15. from urllib.parse import urlparse
  16. import ruamel.yaml
  17. from ...base import BaseConfig
  18. from ....utils.misc import abspath, convert_and_remove_types
  19. class BaseTSConfig(BaseConfig):
  20. """Base TS Config"""
  21. def update(self, dict_like_obj: list):
  22. """update self
  23. Args:
  24. dict_like_obj (dict): dict of pairs(key0.key1.idx.key2=value).
  25. """
  26. from paddlets.utils.config import merge_config_dicts
  27. dict_ = merge_config_dicts(dict_like_obj, self.dict)
  28. self.reset_from_dict(dict_)
  29. def load(self, config_file_path: str):
  30. """load config from yaml file
  31. Args:
  32. config_file_path (str): the path of yaml file.
  33. Raises:
  34. TypeError: the content of yaml file `config_file_path` error.
  35. """
  36. from paddlets.utils.config import parse_from_yaml
  37. dict_ = parse_from_yaml(config_file_path)
  38. if not isinstance(dict_, dict):
  39. raise TypeError
  40. self.reset_from_dict(dict_)
  41. def dump(self, config_file_path: str):
  42. """dump self to yaml file
  43. Args:
  44. config_file_path (str): the path to save self as yaml file.
  45. """
  46. yaml = ruamel.yaml.YAML()
  47. with open(config_file_path, "w", encoding="utf-8") as f:
  48. dict_to_dump = self.dict
  49. dict_to_dump = convert_and_remove_types(dict_to_dump)
  50. yaml.dump(dict_to_dump, f)
  51. def update_epochs(self, epochs: int):
  52. """update epochs setting
  53. Args:
  54. epochs (int): the epochs number value to set
  55. """
  56. self.update({"epoch": epochs})
  57. def update_weights(self, weight_path: str):
  58. """update weight path
  59. Args:
  60. weight_path (str): the local path of weight file to set.
  61. """
  62. self["weights"] = abspath(weight_path)
  63. def update_learning_rate(self, learning_rate: float):
  64. """update learning rate
  65. Args:
  66. learning_rate (float): the learning rate value to set.
  67. Raises:
  68. RuntimeError: Not able to update learning rate, because no LR scheduler config was found.
  69. """
  70. if "learning_rate" not in self.model["model_cfg"]["optimizer_params"]:
  71. raise RuntimeError(
  72. "Not able to update learning rate, because no LR scheduler config was found."
  73. )
  74. self.model["model_cfg"]["optimizer_params"]["learning_rate"] = float(
  75. learning_rate
  76. )
  77. def update_batch_size(self, batch_size: int, mode: str = "train"):
  78. """update batch size setting
  79. Args:
  80. batch_size (int): the batch size number to set.
  81. mode (str, optional): the mode that to be set batch size, must be one of 'train', 'eval', 'test'.
  82. Defaults to 'train'.
  83. Raises:
  84. ValueError: `mode` error. `train` is supported only.
  85. """
  86. if mode == "train":
  87. self.set_val("batch_size", batch_size)
  88. else:
  89. raise ValueError(
  90. f"Setting `batch_size` in {repr(mode)} mode is not supported."
  91. )
  92. def update_pretrained_weights(self, weight_path: str):
  93. """update pretrained weight path
  94. Args:
  95. weight_path (str): the local path or url of pretrained weight file to set.
  96. Raises:
  97. RuntimeError: "Not able to update pretrained weight path, because no model config was found.
  98. TypeError: `weight_path` error. `str` and `None` are supported only.
  99. """
  100. if "model" not in self:
  101. raise RuntimeError(
  102. "Not able to update pretrained weight path, because no model config was found."
  103. )
  104. if isinstance(weight_path, str):
  105. if urlparse(weight_path).scheme == "":
  106. # If `weight_path` is a string but not URL (with scheme present),
  107. # it will be recognized as a local file path.
  108. weight_path = abspath(weight_path)
  109. else:
  110. if weight_path is not None:
  111. raise TypeError("`weight_path` must be string or None.")
  112. self.model["pretrain"] = weight_path
  113. def update_log_ranks(self, device):
  114. """update log ranks
  115. Args:
  116. device (str): the running device to set
  117. """
  118. # PaddleTS does not support multi-device training currently.
  119. pass
  120. def update_print_mem_info(self, print_mem_info: bool):
  121. """setting print memory info"""
  122. assert isinstance(print_mem_info, bool), "print_mem_info should be a bool"
  123. self.update({"print_mem_info": print_mem_info})
  124. def update_log_interval(self, log_interval: int):
  125. """update log interval(steps)
  126. Args:
  127. log_interval (int): the log interval value to set.
  128. """
  129. self.update({"log_interval": log_interval})
  130. def update_dataset(self, dataset_dir: str, dataset_type: str = None):
  131. """update dataset settings"""
  132. raise NotImplementedError
  133. def update_save_dir(self, save_dir: str):
  134. """update save directory
  135. Args:
  136. save_dir (str): the path to save outputs.
  137. """
  138. self["output_dir"] = abspath(save_dir)
  139. def get_epochs_iters(self) -> int:
  140. """get epochs
  141. Returns:
  142. int: the epochs value, i.e., `Global.epochs` in config.
  143. """
  144. if "epoch" in self:
  145. return self.epoch
  146. else:
  147. # Default iters
  148. return 1000
  149. def get_learning_rate(self) -> float:
  150. """get learning rate
  151. Returns:
  152. float: the learning rate value, i.e., `Optimizer.lr.learning_rate` in config.
  153. """
  154. if "learning_rate" not in self.model["model_cfg"]["optimizer_params"]:
  155. # Default lr
  156. return 0.0001
  157. else:
  158. return self.model["model_cfg"]["optimizer_params"]["learning_rate"]
  159. def get_batch_size(self, mode="train") -> int:
  160. """get batch size
  161. Args:
  162. mode (str, optional): the mode that to be get batch size value, must be one of 'train', 'eval', 'test'.
  163. Defaults to 'train'.
  164. Raises:
  165. ValueError: `mode` error. `train` is supported only.
  166. Returns:
  167. int: the batch size value of `mode`, i.e., `DataLoader.{mode}.sampler.batch_size` in config.
  168. """
  169. if mode == "train":
  170. if "batch_size" in self:
  171. return self.batch_size
  172. else:
  173. # Default batch size
  174. return 16
  175. else:
  176. raise ValueError(
  177. f"Getting `batch_size` in {repr(mode)} mode is not supported."
  178. )