check_dataset.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  17. from PIL import Image
  18. import json
  19. import numpy as np
  20. from .....utils.errors import DatasetFileNotFoundError
  21. def check(dataset_dir, output, sample_num=10):
  22. """ check dataset """
  23. dataset_dir = osp.abspath(dataset_dir)
  24. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  25. raise DatasetFileNotFoundError(file_path=dataset_dir)
  26. sample_cnts = dict()
  27. sample_paths = defaultdict(list)
  28. delim = '\t'
  29. valid_num_parts = 2
  30. tags = ['train', 'val']
  31. for _, tag in enumerate(tags):
  32. file_list = osp.join(dataset_dir, f'{tag}.txt')
  33. if not osp.exists(file_list):
  34. if tag in ('train', 'val'):
  35. # train and val file lists must exist
  36. raise DatasetFileNotFoundError(
  37. file_path=file_list,
  38. solution=f"Ensure that both `train.txt` and `val.txt` exist in \
  39. {dataset_dir}")
  40. else:
  41. continue
  42. else:
  43. with open(file_list, 'r', encoding='utf-8') as f:
  44. all_lines = f.readlines()
  45. sample_cnts[tag] = len(all_lines)
  46. for idx, line in enumerate(all_lines):
  47. substr = line.strip("\n").split(delim)
  48. if len(line.strip("\n")) < 1:
  49. continue
  50. assert len(substr) == valid_num_parts or len(
  51. line.strip("\n")) <= 1, \
  52. f"Error in {line}, \
  53. The number of delimiter-separated items in each row in {file_list} \
  54. should be {valid_num_parts} (current delimiter is '{delim}')."
  55. file_name = substr[0]
  56. label = substr[1]
  57. img_path = osp.join(dataset_dir, file_name)
  58. if len(sample_paths[tag]) < sample_num:
  59. sample_paths[tag].append(
  60. os.path.relpath(img_path, output))
  61. if not osp.exists(img_path):
  62. raise DatasetFileNotFoundError(file_path=img_path)
  63. # check det label
  64. label = json.loads(label)
  65. for item in label:
  66. assert "points" in item and "transcription" in item, \
  67. f"line {idx} is not in the correct format."
  68. box = np.array(item['points'])
  69. assert box.shape[1] == 2, \
  70. f"{box} in line {idx} is not in the correct format."
  71. txt = item['transcription']
  72. assert isinstance(txt, str), \
  73. f"{txt} in line {idx} is not in the correct format."
  74. attrs = {}
  75. attrs['train_samples'] = sample_cnts['train']
  76. attrs['train_sample_paths'] = sample_paths['train'][:sample_num]
  77. attrs['val_samples'] = sample_cnts['val']
  78. attrs['val_sample_paths'] = sample_paths['val'][:sample_num]
  79. return attrs