check_dataset.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 defaultdict
  18. from PIL import Image, ImageOps
  19. from .....utils.errors import DatasetFileNotFoundError
  20. def check(dataset_dir, output, dataset_type="PubTabTableRecDataset", sample_num=10):
  21. """
  22. Check whether the dataset is valid.
  23. """
  24. if dataset_type == "PubTabTableRecDataset":
  25. # Custom dataset
  26. if not osp.exists(dataset_dir) or not osp.isdir(dataset_dir):
  27. raise DatasetFileNotFoundError(file_path=dataset_dir)
  28. tags = ["train", "val"]
  29. sample_cnts = dict()
  30. sample_paths = defaultdict(list)
  31. for tag in 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 {dataset_dir}",
  39. )
  40. else:
  41. # tag == 'test'
  42. continue
  43. else:
  44. with open(file_list, "r", encoding="utf-8") as f:
  45. all_lines = f.readlines()
  46. sample_cnts[tag] = len(all_lines)
  47. for line in all_lines:
  48. info = json.loads(line.strip("\n"))
  49. file_name = info["filename"]
  50. info["html"]["cells"].copy()
  51. info["html"]["structure"]["tokens"].copy()
  52. img_path = osp.join(dataset_dir, file_name)
  53. if not os.path.exists(img_path):
  54. raise DatasetFileNotFoundError(file_path=img_path)
  55. vis_save_dir = osp.join(output, "demo_img")
  56. if not osp.exists(vis_save_dir):
  57. os.makedirs(vis_save_dir)
  58. if len(sample_paths[tag]) < sample_num:
  59. img = Image.open(img_path)
  60. img = ImageOps.exif_transpose(img)
  61. vis_path = osp.join(vis_save_dir, osp.basename(file_name))
  62. img.save(vis_path)
  63. sample_path = osp.join(
  64. "check_dataset", os.path.relpath(vis_path, output)
  65. )
  66. sample_paths[tag].append(sample_path)
  67. meta = {}
  68. meta["train_samples"] = sample_cnts["train"]
  69. meta["train_sample_paths"] = sample_paths["train"][:sample_num]
  70. meta["val_samples"] = sample_cnts["val"]
  71. meta["val_sample_paths"] = sample_paths["val"][:sample_num]
  72. return meta