check_dataset.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import random
  17. from PIL import Image, ImageOps
  18. from collections import defaultdict
  19. from .....utils.errors import DatasetFileNotFoundError, CheckFailedError
  20. from .utils.visualizer import draw_label
  21. def check(dataset_dir, output, sample_num=10, dataset_type="ShiTuRec"):
  22. """check dataset"""
  23. dataset_dir = osp.abspath(dataset_dir)
  24. # Custom dataset
  25. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  26. raise DatasetFileNotFoundError(file_path=dataset_dir)
  27. tags = ["train", "gallery", "query"]
  28. delim = " "
  29. valid_num_parts = 2
  30. sample_cnts = dict()
  31. label_map_dict = dict()
  32. sample_paths = defaultdict(list)
  33. labels = []
  34. for tag in tags:
  35. file_list = osp.join(dataset_dir, f"{tag}.txt")
  36. if not osp.exists(file_list):
  37. if tag in ("train", "gallery", "query"):
  38. # train, gallery, query file lists must exist
  39. raise DatasetFileNotFoundError(
  40. file_path=file_list,
  41. solution=f"Ensure that both `train.txt`, `gallery.txt`, `query.txt` exist in {dataset_dir}",
  42. )
  43. else:
  44. # tag == 'test'
  45. continue
  46. else:
  47. with open(file_list, "r", encoding="utf-8") as f:
  48. all_lines = f.readlines()
  49. random.seed(123)
  50. random.shuffle(all_lines)
  51. sample_cnts[tag] = len(all_lines)
  52. for line in all_lines:
  53. substr = line.strip("\n").split(delim)
  54. if len(substr) != valid_num_parts:
  55. raise CheckFailedError(
  56. f"The number of delimiter-separated items in each row in {file_list} \
  57. should be {valid_num_parts} (current delimiter is '{delim}')."
  58. )
  59. file_name = substr[0]
  60. label = substr[1]
  61. img_path = osp.join(dataset_dir, file_name)
  62. if not osp.exists(img_path):
  63. raise DatasetFileNotFoundError(file_path=img_path)
  64. vis_save_dir = osp.join(output, "demo_img")
  65. if not osp.exists(vis_save_dir):
  66. os.makedirs(vis_save_dir)
  67. if len(sample_paths[tag]) < sample_num:
  68. img = Image.open(img_path)
  69. img = ImageOps.exif_transpose(img)
  70. vis_im = draw_label(img, label)
  71. vis_path = osp.join(vis_save_dir, osp.basename(file_name))
  72. vis_im.save(vis_path)
  73. sample_path = osp.join(
  74. "check_dataset", os.path.relpath(vis_path, output)
  75. )
  76. sample_paths[tag].append(sample_path)
  77. attrs = {}
  78. attrs["train_samples"] = sample_cnts["train"]
  79. attrs["train_sample_paths"] = sample_paths["train"]
  80. attrs["gallery_samples"] = sample_cnts["gallery"]
  81. attrs["gallery_sample_paths"] = sample_paths["gallery"]
  82. attrs["query_samples"] = sample_cnts["query"]
  83. attrs["query_sample_paths"] = sample_paths["query"]
  84. return attrs