split_dataset.py 3.2 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 os
  15. import os.path as osp
  16. import random
  17. import shutil
  18. from .....utils import logging
  19. from .....utils.file_interface import custom_open
  20. def split_dataset(root_dir, train_percent, val_percent):
  21. """split dataset"""
  22. assert train_percent > 0, ValueError(
  23. f"The train_percent({train_percent}) must greater than 0!"
  24. )
  25. assert val_percent > 0, ValueError(
  26. f"The val_percent({val_percent}) must greater than 0!"
  27. )
  28. if train_percent + val_percent != 100:
  29. raise ValueError(
  30. f"The sum of train_percent({train_percent})and val_percent({val_percent}) should be 100!"
  31. )
  32. img_dir = osp.join(root_dir, "images")
  33. assert osp.exists(img_dir), FileNotFoundError(
  34. f"The dir of images ({img_dir}) doesn't exist, please check!"
  35. )
  36. ann_dir = osp.join(root_dir, "annotations")
  37. assert osp.exists(ann_dir), FileNotFoundError(
  38. f"The dir of annotations ({ann_dir}) doesn't exist, please check!"
  39. )
  40. img_file_list = [osp.join("images", img_name) for img_name in os.listdir(img_dir)]
  41. img_num = len(img_file_list)
  42. ann_file_list = [
  43. osp.join("annotations", ann_name) for ann_name in os.listdir(ann_dir)
  44. ]
  45. ann_num = len(ann_file_list)
  46. assert img_num == ann_num, ValueError(
  47. "The number of images and annotations must be equal!"
  48. )
  49. split_tags = ["train", "val"]
  50. mapping_line_list = []
  51. for tag in split_tags:
  52. mapping_file = osp.join(root_dir, f"{tag}.txt")
  53. if not osp.exists(mapping_file):
  54. logging.info(f"The mapping file ({mapping_file}) doesn't exist, ignored.")
  55. continue
  56. with custom_open(mapping_file, "r") as fp:
  57. lines = filter(None, (line.strip() for line in fp.readlines()))
  58. mapping_line_list.extend(lines)
  59. sample_num = len(mapping_line_list)
  60. random.shuffle(mapping_line_list)
  61. split_percents = [train_percent, val_percent]
  62. start_idx = 0
  63. for tag, percent in zip(split_tags, split_percents):
  64. if tag == "test" and percent == 0:
  65. continue
  66. end_idx = start_idx + round(sample_num * percent / 100)
  67. end_idx = min(end_idx, sample_num)
  68. mapping_file = osp.join(root_dir, f"{tag}.txt")
  69. if os.path.exists(mapping_file):
  70. shutil.move(mapping_file, mapping_file + ".bak")
  71. logging.info(
  72. f"The original mapping file ({mapping_file}) "
  73. f"has been backed up to ({mapping_file}.bak)"
  74. )
  75. with custom_open(mapping_file, "w") as fp:
  76. fp.write("\n".join(mapping_line_list[start_idx:end_idx]))
  77. start_idx = end_idx
  78. return root_dir