check_dataset.py 3.3 KB

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