dataset_process.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import cv2
  3. import numpy as np
  4. '''
  5. 处理CIFAR-10数据集,对[cifar-10-python.tar.gz]文件解压后的处理操作,将data_batch文件解压为图片,标签文件生成操作
  6. '''
  7. # CIFAR-10数据集官方给出的python3解压数据文件函数,返回数据字典
  8. def unpickle(file):
  9. import pickle
  10. with open(file, 'rb') as fo:
  11. dict = pickle.load(fo, encoding='bytes')
  12. return dict
  13. # 定义解压后batch文件夹
  14. file_dir = './dataset/CIFAR-10/cifar-10-batches-py'
  15. dataset_dir = './dataset/CIFAR-10'
  16. train_dic = f'{dataset_dir}/train/'
  17. test_dic = f'{dataset_dir}/test/'
  18. # 判断文件夹是否存在,不存在的话创建文件夹
  19. if not os.path.exists(train_dic):
  20. os.mkdir(train_dic)
  21. if not os.path.exists(test_dic):
  22. os.mkdir(test_dic)
  23. # 训练集有五个批次,每个批次10000个图片,测试集有10000张图片
  24. def cifar10_img(file_dir):
  25. '''
  26. 处理cifar-10数据集解压后的batch文件处理
  27. :param file_dir: cifar-10-python.tar.gz 解压后的文件夹地址
  28. '''
  29. # 处理训练集
  30. for i in range(1, 6):
  31. data_name = file_dir + '/' + 'data_batch_' + str(i)
  32. data_dict = unpickle(data_name)
  33. print(data_name + ' is processing')
  34. for j in range(10000):
  35. img = np.reshape(data_dict[b'data'][j], (3, 32, 32))
  36. img = np.transpose(img, (1, 2, 0))
  37. # 通道顺序为RGB
  38. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  39. # 要改成不同的形式的文件只需要将文件后缀修改即可
  40. img_name = train_dic + str(data_dict[b'labels'][j]) + str((i) * 10000 + j) + '.jpg'
  41. cv2.imwrite(img_name, img)
  42. print(data_name + ' is done')
  43. # 处理测试集
  44. test_data_name = file_dir + '/test_batch'
  45. print(test_data_name + ' is processing')
  46. test_dict = unpickle(test_data_name)
  47. for m in range(10000):
  48. img = np.reshape(test_dict[b'data'][m], (3, 32, 32))
  49. img = np.transpose(img, (1, 2, 0))
  50. # 通道顺序为RGB
  51. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  52. # 要改成不同的形式的文件只需要将文件后缀修改即可
  53. img_name = test_dic + str(test_dict[b'labels'][m]) + str(10000 + m) + '.jpg'
  54. cv2.imwrite(img_name, img)
  55. print(test_data_name + ' is done')
  56. print('Finish transforming to image')
  57. # 处理描述文件
  58. meta_name = file_dir + '/' + 'batches.meta'
  59. meta_dict = unpickle(meta_name)
  60. label_names = [str(item) for item in meta_dict[b'label_names']]
  61. print(meta_name + ' is done')
  62. f = open(f'{dataset_dir}/class.txt', 'w') # 创建类型描述文件
  63. for label_name in label_names:
  64. line = label_name + '\n'
  65. f.write(line)
  66. f.close()
  67. def gen_label_txt(label_txt_path, img_dir):
  68. '''
  69. 生成标签文件,描述图片名称与标签对应关系,格式[文件名 标签值]
  70. :param label_txt_path: 生成的标签文件路径,例如:./dataset/CIFAR-10/train.txt
  71. :param img_dir: 处理图像文件夹,例如:./dataset/CIFAR-10/train
  72. '''
  73. f = open(label_txt_path, 'w') # 创建标签文件
  74. img_list = os.listdir(img_dir) # 图像文件夹下所有png图片
  75. for img_name in img_list:
  76. img_path = os.path.join(img_dir, img_name)
  77. label = img_name[0]
  78. line = img_path + ' ' + label + '\n'
  79. f.write(line)
  80. f.close()
  81. def write_class_list(classes, class_txt_path):
  82. with open(class_txt_path, 'w') as f:
  83. for cls in sorted(classes):
  84. f.write(cls + '\n')
  85. if __name__ == '__main__':
  86. # 处理解压后文件
  87. cifar10_img(file_dir)
  88. # 生成标签文件
  89. gen_label_txt(dataset_dir + '/train.txt', train_dic)
  90. gen_label_txt(dataset_dir + '/test.txt', test_dic)