split_dataset.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from random import shuffle
  16. from .....utils.file_interface import custom_open
  17. def split_dataset(root_dir, train_rate, val_rate):
  18. """
  19. Split the image dataset into training, validation, and test sets according to the given ratios,
  20. and generate corresponding .txt files.
  21. Args:
  22. root_dir (str): Path to the root directory of the dataset.
  23. train_rate (int): Percentage of the dataset to be used as the training set.
  24. val_rate (int): Percentage of the dataset to be used as the validation set.
  25. Returns:
  26. str: Information about the dataset split results.
  27. """
  28. sum_rate = train_rate + val_rate
  29. assert (
  30. sum_rate == 100
  31. ), f"The sum of train_rate({train_rate}), val_rate({val_rate}) should equal 100!"
  32. assert (
  33. train_rate > 0 and val_rate > 0
  34. ), f"The train_rate({train_rate}) and val_rate({val_rate}) should be greater than 0!"
  35. tags = ["train", "val"]
  36. valid_path = False
  37. video_files = []
  38. for tag in tags:
  39. split_image_list = os.path.abspath(os.path.join(root_dir, f"{tag}.txt"))
  40. rename_image_list = os.path.abspath(os.path.join(root_dir, f"{tag}.txt.bak"))
  41. if os.path.exists(split_image_list):
  42. with custom_open(split_image_list, "r") as f:
  43. lines = f.readlines()
  44. video_files = video_files + lines
  45. valid_path = True
  46. if not os.path.exists(rename_image_list):
  47. os.rename(split_image_list, rename_image_list)
  48. assert (
  49. valid_path
  50. ), f"The files to be divided{tags[0]}.txt, {tags[1]}.txt, do not exist in the dataset directory."
  51. shuffle(video_files)
  52. start = 0
  53. video_num = len(video_files)
  54. rate_list = [train_rate, val_rate]
  55. for i, tag in enumerate(tags):
  56. rate = rate_list[i]
  57. if rate == 0:
  58. continue
  59. end = start + round(video_num * rate / 100)
  60. if sum(rate_list[i + 1 :]) == 0:
  61. end = video_num
  62. txt_file = os.path.abspath(os.path.join(root_dir, tag + ".txt"))
  63. with custom_open(txt_file, "w") as f:
  64. m = 0
  65. for id in range(start, end):
  66. m += 1
  67. f.write(video_files[id])
  68. start = end
  69. return root_dir