config.py 6.1 KB

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