split_dataset.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from random import shuffle
  16. from .....utils.file_interface import custom_open
  17. def split_dataset(root_dir, train_rate, gallery_rate, query_rate):
  18. """
  19. 将图像数据集按照比例分成训练集、验证集和测试集,并生成对应的.txt文件。
  20. Args:
  21. root_dir (str): 数据集根目录路径。
  22. train_rate (int): 训练集占总数据集的比例(%)。
  23. gallery_rate (int): 被查询数据集占总数据集的比例(%)。
  24. query_rate (int): 查询数据集占总数据集的比例(%)。
  25. Returns:
  26. str: 数据划分结果信息。
  27. """
  28. sum_rate = train_rate + gallery_rate + query_rate
  29. assert (
  30. sum_rate == 100
  31. ), f"The sum of train_rate({train_rate}), gallery_rate({gallery_rate}), query_rate({query_rate}) should equal 100!"
  32. assert (
  33. train_rate > 0 and gallery_rate > 0 and query_rate > 0
  34. ), f"The train_rate({train_rate}) and gallery_rate({gallery_rate}) and query_rate({query_rate}) should be greater than 0!"
  35. tags = ["train", "gallery", "query"]
  36. valid_path = False
  37. image_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. image_files = image_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, {tags[1]}.txt, do not exist in the dataset directory."
  51. shuffle(image_files)
  52. start = 0
  53. image_num = len(image_files)
  54. rate_list = [train_rate, gallery_rate, query_rate]
  55. for i, tag in enumerate(tags):
  56. rate = rate_list[i]
  57. if rate == 0:
  58. continue
  59. end = start + round(image_num * rate / 100)
  60. if sum(rate_list[i + 1 :]) == 0:
  61. end = image_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(image_files[id])
  68. start = end
  69. return root_dir