__init__.py 3.1 KB

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