config.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 functools import lru_cache
  16. import yaml
  17. from typing import Union
  18. from paddleseg.utils import NoAliasDumper
  19. from ..base_seg_config import BaseSegConfig
  20. from ....utils.misc import abspath
  21. from ....utils import logging
  22. class SegConfig(BaseSegConfig):
  23. """Semantic Segmentation Config"""
  24. def update_dataset(self, dataset_path: str, dataset_type: str = None):
  25. """update dataset settings
  26. Args:
  27. dataset_path (str): the root path of dataset.
  28. dataset_type (str, optional): dataset type. Defaults to None.
  29. Raises:
  30. ValueError: the dataset_type error.
  31. """
  32. dataset_dir = abspath(dataset_path)
  33. if dataset_type is None:
  34. dataset_type = "SegDataset"
  35. if dataset_type == "SegDataset":
  36. # TODO: Prune extra keys
  37. ds_cfg = self._make_custom_dataset_config(dataset_dir)
  38. self.update(ds_cfg)
  39. elif dataset_type == "_dummy":
  40. # XXX: A special dataset type to tease PaddleSeg val dataset checkers
  41. self.update(
  42. {
  43. "val_dataset": {
  44. "type": "SegDataset",
  45. "dataset_root": dataset_dir,
  46. "val_path": os.path.join(dataset_dir, "val.txt"),
  47. "mode": "val",
  48. },
  49. }
  50. )
  51. else:
  52. raise ValueError(f"{repr(dataset_type)} is not supported.")
  53. def update_num_classes(self, num_classes: int):
  54. """update classes number
  55. Args:
  56. num_classes (int): the classes number value to set.
  57. """
  58. if "train_dataset" in self:
  59. self.train_dataset["num_classes"] = num_classes
  60. if "val_dataset" in self:
  61. self.val_dataset["num_classes"] = num_classes
  62. if "model" in self:
  63. self.model["num_classes"] = num_classes
  64. def update_train_crop_size(self, crop_size: Union[int, list]):
  65. """update the image cropping size of training preprocessing
  66. Args:
  67. crop_size (int | list): the size of image to be cropped.
  68. Raises:
  69. ValueError: the `crop_size` error.
  70. """
  71. # XXX: This method is highly coupled to PaddleSeg's internal functions
  72. if isinstance(crop_size, int):
  73. crop_size = [crop_size, crop_size]
  74. else:
  75. crop_size = list(crop_size)
  76. if len(crop_size) != 2:
  77. raise ValueError
  78. crop_size = [int(crop_size[0]), int(crop_size[1])]
  79. tf_cfg_list = self.train_dataset["transforms"]
  80. modified = False
  81. for tf_cfg in tf_cfg_list:
  82. if tf_cfg["type"] == "RandomPaddingCrop":
  83. tf_cfg["crop_size"] = crop_size
  84. modified = True
  85. if not modified:
  86. logging.warning(
  87. "Could not find configuration item of image cropping transformation operator. "
  88. "Therefore, the crop size was not updated."
  89. )
  90. def get_epochs_iters(self) -> int:
  91. """get epochs
  92. Returns:
  93. int: the epochs value, i.e., `Global.epochs` in config.
  94. """
  95. if "iters" in self:
  96. return self.iters
  97. else:
  98. # Default iters
  99. return 1000
  100. def get_learning_rate(self) -> float:
  101. """get learning rate
  102. Returns:
  103. float: the learning rate value, i.e., `Optimizer.lr.learning_rate` in config.
  104. """
  105. if "lr_scheduler" not in self or "learning_rate" not in self.lr_scheduler:
  106. # Default lr
  107. return 0.0001
  108. else:
  109. return self.lr_scheduler["learning_rate"]
  110. def get_batch_size(self, mode="train") -> int:
  111. """get batch size
  112. Args:
  113. mode (str, optional): the mode that to be get batch size value, must be one of 'train', 'eval', 'test'.
  114. Defaults to 'train'.
  115. Raises:
  116. ValueError: the `mode` error. `train` is supported only.
  117. Returns:
  118. int: the batch size value of `mode`, i.e., `DataLoader.{mode}.sampler.batch_size` in config.
  119. """
  120. if mode == "train":
  121. if "batch_size" in self:
  122. return self.batch_size
  123. else:
  124. # Default batch size
  125. return 4
  126. else:
  127. raise ValueError(
  128. f"Getting `batch_size` in {repr(mode)} mode is not supported."
  129. )
  130. def _make_custom_dataset_config(self, dataset_root_path: str) -> dict:
  131. """construct the dataset config that meets the format requirements
  132. Args:
  133. dataset_root_path (str): the root directory of dataset.
  134. Returns:
  135. dict: the dataset config.
  136. """
  137. ds_cfg = {
  138. "train_dataset": {
  139. "type": "SegDataset",
  140. "dataset_root": dataset_root_path,
  141. "train_path": os.path.join(dataset_root_path, "train.txt"),
  142. "mode": "train",
  143. },
  144. "val_dataset": {
  145. "type": "SegDataset",
  146. "dataset_root": dataset_root_path,
  147. "val_path": os.path.join(dataset_root_path, "val.txt"),
  148. "mode": "val",
  149. },
  150. }
  151. return ds_cfg