这篇教程python 把CIFAR-10]数据集转换为图片格式写得很实用,希望能帮到您。 import numpy as np import pickle as pkl import imageio #将cifar10数据转化为图片+标签格式
#定义反序列函数 def unpickle(file): fo = open(file, 'rb') dict = pkl.load(fo,encoding='bytes') #以二进制的方式加载 fo.close() return dict #转换train数据集 for j in range(1, 6): dataName = "./dataset/cifar-10-batches-py/data_batch_" + str(j) Xtr = unpickle(dataName) print (dataName + " is loading...") for i in range(0, 10000): img = np.reshape(Xtr[b'data'][i], (3, 32, 32)) img = img.transpose(1, 2, 0) picName = './dataset/cifar-10-batches-py/data_batch_/train/' + str(Xtr[b'labels'][i]) + '/' + str(i + (j - 1)*10000) + '.jpg' imageio.imwrite(picName, img) print(dataName + " loaded.") print ("test_batch is loading...") #转换test数据集 testXtr = unpickle("./dataset/cifar-10-batches-py/test_batch") for i in range(0, 10000): img = np.reshape(testXtr[b'data'][i], (3, 32, 32)) img = img.transpose(1, 2, 0) picName = './dataset/cifar-10-batches-py/data_batch_/test/' + str(testXtr[b'labels'][i]) + '_' + str(i) + '.jpg' imageio.imwrite(picName, img) print ("test_batch loaded.")
import numpy as np import pickle as pkl import imageio #将cifar10数据转化为图片+标签格式
#定义反序列函数 def unpickle(file): fo = open(file, 'rb') dict = pkl.load(fo,encoding='bytes') #以二进制的方式加载 fo.close() return dict #转换train数据集 for j in range(1, 6): dataName = "./dataset/cifar-10-batches-py/data_batch_" + str(j) Xtr = unpickle(dataName) print (dataName + " is loading...") for i in range(0, 10000): img = np.reshape(Xtr[b'data'][i], (3, 32, 32)) img = img.transpose(1, 2, 0) picName = './dataset/cifar-10-batches-py/data_batch_/train/' + str(Xtr[b'labels'][i]) + '/' + str(i + (j - 1)*10000) + '.jpg' imageio.imwrite(picName, img) print(dataName + " loaded.") print ("test_batch is loading...") #转换test数据集 testXtr = unpickle("./dataset/cifar-10-batches-py/test_batch") for i in range(0, 10000): img = np.reshape(testXtr[b'data'][i], (3, 32, 32)) img = img.transpose(1, 2, 0) picName = './dataset/cifar-10-batches-py/data_batch_/test/' + str(testXtr[b'labels'][i]) + '/' + str(i) + '.jpg' imageio.imwrite(picName, img) print ("test_batch loaded.") python把当前图片保存到指定文件目录 Python挑选文件夹里宽大于300图片的方法 |