check_dataset.py 3.4 KB

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