check_dataset.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 json
  16. import os.path as osp
  17. from PIL import Image, ImageOps
  18. from collections import defaultdict
  19. from .....utils.errors import DatasetFileNotFoundError, CheckFailedError
  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. max_recorded_sample_cnts = 50
  30. sample_cnts = dict()
  31. sample_paths = defaultdict(list)
  32. for tag in tags:
  33. file_list = osp.join(dataset_dir, f"{tag}.txt")
  34. if not osp.exists(file_list):
  35. if tag in ("train", "val"):
  36. # train and val file lists must exist
  37. raise DatasetFileNotFoundError(
  38. file_path=file_list,
  39. solution=f"Ensure that both `train.txt` and `val.txt` exist in {dataset_dir}",
  40. )
  41. else:
  42. # tag == 'test'
  43. continue
  44. else:
  45. with open(file_list, "r", encoding="utf-8") as f:
  46. all_lines = f.readlines()
  47. sample_cnts[tag] = len(all_lines)
  48. for line in all_lines:
  49. info = json.loads(line.strip("\n"))
  50. file_name = info["filename"]
  51. cells = info["html"]["cells"].copy()
  52. structure = info["html"]["structure"]["tokens"].copy()
  53. img_path = osp.join(dataset_dir, file_name)
  54. if not os.path.exists(img_path):
  55. raise DatasetFileNotFoundError(file_path=img_path)
  56. vis_save_dir = osp.join(output, "demo_img")
  57. if not osp.exists(vis_save_dir):
  58. os.makedirs(vis_save_dir)
  59. if len(sample_paths[tag]) < sample_num:
  60. img = Image.open(img_path)
  61. img = ImageOps.exif_transpose(img)
  62. vis_path = osp.join(vis_save_dir, osp.basename(file_name))
  63. img.save(vis_path)
  64. sample_path = osp.join(
  65. "check_dataset", os.path.relpath(vis_path, output)
  66. )
  67. sample_paths[tag].append(sample_path)
  68. boxes_num = len(cells)
  69. tokens_num = sum(
  70. [
  71. structure.count(x)
  72. for x in ["<td>", "<td", "<eb></eb>", "<td></td>"]
  73. ]
  74. )
  75. if boxes_num != tokens_num:
  76. raise CheckFailedError(
  77. f"The number of cells needs to be consistent with the number of tokens "
  78. "but the number of cells is {boxes_num}, and the number of tokens is {tokens_num}."
  79. )
  80. meta = {}
  81. meta["train_samples"] = sample_cnts["train"]
  82. meta["train_sample_paths"] = sample_paths["train"][:sample_num]
  83. meta["val_samples"] = sample_cnts["val"]
  84. meta["val_sample_paths"] = sample_paths["val"][:sample_num]
  85. return meta