__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 pathlib import Path
  15. from ...base import BaseDatasetChecker
  16. from ..model_list import MODELS
  17. from .dataset_src import check, convert, deep_analyse, split_dataset
  18. class ClsDatasetChecker(BaseDatasetChecker):
  19. """Dataset Checker for Image Classification Model"""
  20. entities = MODELS
  21. sample_num = 10
  22. def get_dataset_root(self, dataset_dir: str) -> str:
  23. """find the dataset root dir
  24. Args:
  25. dataset_dir (str): the directory that contain dataset.
  26. Returns:
  27. str: the root directory of dataset.
  28. """
  29. anno_dirs = list(Path(dataset_dir).glob("**/images"))
  30. assert len(anno_dirs) == 1
  31. dataset_dir = anno_dirs[0].parent.as_posix()
  32. return dataset_dir
  33. def convert_dataset(self, src_dataset_dir: str) -> str:
  34. """convert the dataset from other type to specified type
  35. Args:
  36. src_dataset_dir (str): the root directory of dataset.
  37. Returns:
  38. str: the root directory of converted dataset.
  39. """
  40. return convert(src_dataset_dir)
  41. def split_dataset(self, src_dataset_dir: str) -> str:
  42. """repartition the train and validation dataset
  43. Args:
  44. src_dataset_dir (str): the root directory of dataset.
  45. Returns:
  46. str: the root directory of splited dataset.
  47. """
  48. return split_dataset(
  49. src_dataset_dir,
  50. self.check_dataset_config.split.train_percent,
  51. self.check_dataset_config.split.val_percent,
  52. )
  53. def check_dataset(self, dataset_dir: str, sample_num: int = sample_num) -> dict:
  54. """check if the dataset meets the specifications and get dataset summary
  55. Args:
  56. dataset_dir (str): the root directory of dataset.
  57. sample_num (int): the number to be sampled.
  58. Returns:
  59. dict: dataset summary.
  60. """
  61. return check(dataset_dir, self.output)
  62. def analyse(self, dataset_dir: str) -> dict:
  63. """deep analyse dataset
  64. Args:
  65. dataset_dir (str): the root directory of dataset.
  66. Returns:
  67. dict: the deep analysis results.
  68. """
  69. return deep_analyse(dataset_dir, self.output)
  70. def get_show_type(self) -> str:
  71. """get the show type of dataset
  72. Returns:
  73. str: show type
  74. """
  75. return "image"
  76. def get_dataset_type(self) -> str:
  77. """return the dataset type
  78. Returns:
  79. str: dataset type
  80. """
  81. return "ClsDataset"