diff --git a/Code/1_data_prepare/1_1_cifar10_to_png.py b/Code/1_data_prepare/1_1_cifar10_to_png.py index 9df14994ab093c6f3f7a46203882690234b16f2e..4ff2c88c9cb2dba41f580096b45d5c27230b00c6 100644 --- a/Code/1_data_prepare/1_1_cifar10_to_png.py +++ b/Code/1_data_prepare/1_1_cifar10_to_png.py @@ -8,17 +8,17 @@ import numpy as np import os import pickle -data_dir = '../../Data/cifar-10-batches-py/' -train_o_dir = '../../Data/cifar-10-png/raw_train/' -test_o_dir = '../../Data/cifar-10-png/raw_test/' + +data_dir = os.path.join("..", "..", "Data", "cifar-10-batches-py") +train_o_dir = os.path.join("..", "..", "Data", "cifar-10-png", "raw_train") +test_o_dir = os.path.join("..", "..", "Data", "cifar-10-png", "raw_test") Train = False # 不解压训练集,仅解压测试集 # 解压缩,返回解压后的字典 def unpickle(file): - fo = open(file, 'rb') - dict_ = pickle.load(fo, encoding='bytes') - fo.close() + with open(file, 'rb') as fo: + dict_ = pickle.load(fo, encoding='bytes') return dict_ def my_mkdir(my_dir): @@ -30,7 +30,7 @@ def my_mkdir(my_dir): if __name__ == '__main__': if Train: for j in range(1, 6): - data_path = data_dir + "data_batch_" + str(j) # data_batch_12345 + data_path = os.path.join(data_dir, "data_batch_" + str(j)) # data_batch_12345 train_data = unpickle(data_path) print(data_path + " is loading...") @@ -50,7 +50,7 @@ if __name__ == '__main__': print("test_batch is loading...") # 生成测试集图片 - test_data_path = data_dir + "test_batch" + test_data_path = os.path.join(data_dir, "test_batch") test_data = unpickle(test_data_path) for i in range(0, 10000): img = np.reshape(test_data[b'data'][i], (3, 32, 32))