make_data_files.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import os
  2. import argparse
  3. import random
  4. from config import config
  5. def parse_args():
  6. parser = argparse.ArgumentParser()
  7. parser.add_argument('--data_dir', help='dataset dir',
  8. default='./data', type=str)
  9. parser.add_argument('--train_percent', help='percent for train images (train+val = all * train_percent)',
  10. default=0.9, type=float)
  11. parser.add_argument('--val_percent', help='percent for val images (val = (train+val) * val_percent)',
  12. default=0.1, type=float)
  13. args = parser.parse_args()
  14. return args
  15. def remove_lastline_blank(path):
  16. f = open(path, 'rb+')
  17. if len(f.readlines()) > 0:
  18. f.seek(-1, os.SEEK_END)
  19. if f.next() == '\n':
  20. f.seek(-1, os.SEEK_END)
  21. f.truncate()
  22. f.close()
  23. if __name__ == '__main__':
  24. args = parse_args()
  25. assert os.path.exists(args.data_dir), args.data_dir + 'does not exist'
  26. txt_dir = args.data_dir
  27. print('Save splits files to ' + os.path.abspath(txt_dir))
  28. cfg = config()
  29. class_idxes = cfg.get_model_index()
  30. # output files
  31. info_path = os.path.join(txt_dir, 'info.txt')
  32. train_path = os.path.join(txt_dir, 'train.txt')
  33. val_path = os.path.join(txt_dir, 'val.txt')
  34. train_val_path = os.path.join(txt_dir, 'trainval.txt')
  35. test_path = os.path.join(txt_dir, 'test.txt')
  36. info_file = open(info_path, 'w')
  37. train_file = open(train_path, 'w')
  38. val_file = open(val_path, 'w')
  39. train_val_file = open(train_val_path, 'w')
  40. test_file = open(test_path, 'w')
  41. total_all = 0
  42. total_train_val = 0
  43. total_val = 0
  44. total_train = 0
  45. total_test = 0
  46. # load all images
  47. dataset_dir = os.path.join(args.data_dir, 'dataset')
  48. class_dirs = os.listdir(dataset_dir)
  49. for class_dir in class_dirs:
  50. if class_dir not in class_idxes:
  51. print('[WARN] {} not in train classes'.format(class_dir))
  52. continue
  53. files_all = os.listdir(os.path.join(dataset_dir, class_dir))
  54. files_all = [n for n in files_all if n.endswith('.jpg')]
  55. num_all = len(files_all)
  56. num_train_val = int(num_all * args.train_percent)
  57. num_val = int(num_train_val * args.val_percent)
  58. num_train = num_train_val - num_val
  59. num_test = num_all - num_train_val
  60. files_train_val = random.sample(files_all, num_train_val)
  61. files_val = random.sample(files_train_val, num_val)
  62. total_all = total_all + num_all
  63. total_train_val = total_train_val + num_train_val
  64. total_val = total_val + num_val
  65. total_train = total_train + num_train
  66. total_test = total_test + num_test
  67. for f in files_all:
  68. line = '{}/{} {}\n'.format(class_dir, f, class_idxes[class_dir])
  69. if f in files_train_val:
  70. train_val_file.write(line)
  71. if f in files_val:
  72. val_file.write(line)
  73. else:
  74. train_file.write(line)
  75. else:
  76. test_file.write(line)
  77. info_file.write('train: {}/{} \n'.format(total_train, total_all))
  78. info_file.write('val: {}/{} \n'.format(total_val, total_all))
  79. info_file.write('trainval: {}/{} \n'.format(total_train_val, total_all))
  80. info_file.write('test: {}/{} \n'.format(total_test, total_all))
  81. print 'train: {}/{}'.format(total_train, total_all)
  82. print 'val: {}/{}'.format(total_val, total_all)
  83. print 'trainval: {}/{}'.format(total_train_val, total_all)
  84. print 'test: {}/{}'.format(total_test, total_all)
  85. train_batch_size = 64
  86. val_batch_size = 32
  87. test_iter = total_val / val_batch_size
  88. max_test_interval = total_train / train_batch_size
  89. print('')
  90. print('test_iter = {}'.format(test_iter))
  91. print('test_interval < {}'.format(max_test_interval))
  92. train_file.close()
  93. val_file.close()
  94. train_val_file.close()
  95. test_file.close()
  96. remove_lastline_blank(train_path)
  97. remove_lastline_blank(val_path)
  98. remove_lastline_blank(train_val_path)
  99. remove_lastline_blank(test_path)