check_dataset.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 json
  15. import os
  16. import os.path as osp
  17. from collections import Counter, defaultdict
  18. from pathlib import Path
  19. from PIL import Image, ImageOps
  20. from .....utils.deps import function_requires_deps, is_dep_available
  21. from .....utils.errors import DatasetFileNotFoundError
  22. from .utils.visualizer import draw_bbox
  23. if is_dep_available("pycocotools"):
  24. from pycocotools.coco import COCO
  25. @function_requires_deps("pycocotools")
  26. def check(dataset_dir, output, sample_num=10):
  27. """check dataset"""
  28. dataset_dir = osp.abspath(dataset_dir)
  29. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  30. raise DatasetFileNotFoundError(file_path=dataset_dir)
  31. sample_cnts = dict()
  32. sample_paths = defaultdict(list)
  33. defaultdict(Counter)
  34. tags = ["instance_train", "instance_val"]
  35. for _, tag in enumerate(tags):
  36. file_list = osp.join(dataset_dir, f"annotations/{tag}.json")
  37. if not osp.exists(file_list):
  38. if tag in ("instance_train", "instance_val"):
  39. # train and val file lists must exist
  40. raise DatasetFileNotFoundError(
  41. file_path=file_list,
  42. solution=f"Ensure that both `instance_train.json` and `instance_val.json` exist in \
  43. {dataset_dir}/annotations",
  44. )
  45. else:
  46. continue
  47. else:
  48. with open(file_list, "r", encoding="utf-8") as f:
  49. jsondata = json.load(f)
  50. coco = COCO(file_list)
  51. num_class = len(coco.getCatIds())
  52. vis_save_dir = osp.join(output, "demo_img")
  53. image_info = jsondata["images"]
  54. sample_cnts[tag] = len(image_info)
  55. sample_num = min(sample_num, len(image_info))
  56. for i in range(sample_num):
  57. file_name = image_info[i]["file_name"]
  58. img_id = image_info[i]["id"]
  59. img_path = osp.join(dataset_dir, "images", file_name)
  60. if not osp.exists(img_path):
  61. raise DatasetFileNotFoundError(file_path=img_path)
  62. img = Image.open(img_path)
  63. img = ImageOps.exif_transpose(img)
  64. vis_im = draw_bbox(img, coco, img_id)
  65. vis_path = osp.join(vis_save_dir, file_name)
  66. Path(vis_path).parent.mkdir(parents=True, exist_ok=True)
  67. vis_im.save(vis_path)
  68. sample_path = osp.join(
  69. "check_dataset", os.path.relpath(vis_path, output)
  70. )
  71. sample_paths[tag].append(sample_path)
  72. attrs = {}
  73. attrs["num_classes"] = num_class
  74. attrs["train_samples"] = sample_cnts["instance_train"]
  75. attrs["train_sample_paths"] = sample_paths["instance_train"]
  76. attrs["val_samples"] = sample_cnts["instance_val"]
  77. attrs["val_sample_paths"] = sample_paths["instance_val"]
  78. return attrs