split_data.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. 在split_data.py文件中放入以下代码并运行,这个文件是划分训练、验证、测试集。其中支持修改train_percent、val_percent、test_percent,改变训练集、验证集和测试集比例
  5. """
  6. # 将图片和标注数据按比例切分为 训练集和测试集
  7. import shutil
  8. import random
  9. import os
  10. import argparse
  11. # 检查文件夹是否存在
  12. def mkdir(path):
  13. if not os.path.exists(path):
  14. os.makedirs(path)
  15. def main(image_dir, txt_dir, save_dir):
  16. # 创建文件夹
  17. mkdir(save_dir)
  18. images_dir = os.path.join(save_dir, 'images')
  19. labels_dir = os.path.join(save_dir, 'labels')
  20. img_train_path = os.path.join(images_dir, 'train')
  21. img_test_path = os.path.join(images_dir, 'test')
  22. img_val_path = os.path.join(images_dir, 'val')
  23. label_train_path = os.path.join(labels_dir, 'train')
  24. label_test_path = os.path.join(labels_dir, 'test')
  25. label_val_path = os.path.join(labels_dir, 'val')
  26. mkdir(images_dir);
  27. mkdir(labels_dir);
  28. mkdir(img_train_path);
  29. mkdir(img_test_path);
  30. mkdir(img_val_path);
  31. mkdir(label_train_path);
  32. mkdir(label_test_path);
  33. mkdir(label_val_path);
  34. # 数据集划分比例,训练集80%,验证集10%,测试集10%,按需修改
  35. train_percent = 0.8
  36. val_percent = 0.1
  37. test_percent = 0.1
  38. total_txt = os.listdir(txt_dir)
  39. num_txt = len(total_txt)
  40. list_all_txt = range(num_txt) # 范围 range(0, num)
  41. num_train = int(num_txt * train_percent)
  42. num_val = int(num_txt * val_percent)
  43. num_test = num_txt - num_train - num_val
  44. train = random.sample(list_all_txt, num_train)
  45. # 在全部数据集中取出train
  46. val_test = [i for i in list_all_txt if not i in train]
  47. # 再从val_test取出num_val个元素,val_test剩下的元素就是test
  48. val = random.sample(val_test, num_val)
  49. print("训练集数目:{}, 验证集数目:{},测试集数目:{}".format(len(train), len(val), len(val_test) - len(val)))
  50. for i in list_all_txt:
  51. name = total_txt[i][:-4]
  52. srcImage = os.path.join(image_dir, name + '.jpg')
  53. srcLabel = os.path.join(txt_dir, name + '.txt')
  54. if i in train:
  55. dst_train_Image = os.path.join(img_train_path, name + '.jpg')
  56. dst_train_Label = os.path.join(label_train_path, name + '.txt')
  57. shutil.copyfile(srcImage, dst_train_Image)
  58. shutil.copyfile(srcLabel, dst_train_Label)
  59. elif i in val:
  60. dst_val_Image = os.path.join(img_val_path, name + '.jpg')
  61. dst_val_Label = os.path.join(label_val_path, name + '.txt')
  62. shutil.copyfile(srcImage, dst_val_Image)
  63. shutil.copyfile(srcLabel, dst_val_Label)
  64. else:
  65. dst_test_Image = os.path.join(img_test_path, name + '.jpg')
  66. dst_test_Label = os.path.join(label_test_path, name + '.txt')
  67. shutil.copyfile(srcImage, dst_test_Image)
  68. shutil.copyfile(srcLabel, dst_test_Label)
  69. if __name__ == '__main__':
  70. """
  71. python split_datasets.py --image-dir my_datasets/color_rings/imgs --txt-dir my_datasets/color_rings/txts --save-dir my_datasets/color_rings/train_data
  72. """
  73. parser = argparse.ArgumentParser(description='split datasets to train,val,test params')
  74. parser.add_argument('--image-dir', type=str, default=r"VOCdevkit\images", help='image path dir')
  75. parser.add_argument('--txt-dir', type=str, default=r"VOCdevkit\txt", help='txt path dir')
  76. parser.add_argument('--save-dir', default=r"VOCdevkit\datsets", type=str, help='save dir')
  77. args = parser.parse_args()
  78. image_dir = args.image_dir
  79. txt_dir = args.txt_dir
  80. save_dir = args.save_dir
  81. main(image_dir, txt_dir, save_dir)