__init__.py 3.4 KB

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