[Python][原创]cifar10数据集转图片2020版本

网上找了很多不能用,我这个测试可以用,转换后分成10个文件夹,train有10个文件夹,test有10个文件夹
import cv2
import numpy as np
import os
import pickle

if not os.path.exists("./train"):
    os.makedirs("./train")

if not os.path.exists("./test"):
    os.makedirs("./test")
label_name_list = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
for name in label_name_list:
    if not os.path.exists("./train/"+name):
        os.makedirs("./train/"+name)
    if not os.path.exists("./test/"+name):
        os.makedirs("./test/"+name)
def unpickle(file):
    with open(file, 'rb') as f:
        dict = pickle.load(f,encoding="bytes")
    return dict


def main(cifar10_data_dir):
    for i in range(1, 6):
        train_data_file = os.path.join(cifar10_data_dir, 'data_batch_' + str(i))
        print(train_data_file)
        data = unpickle(train_data_file)
        print('unpickle done')
        for j in range(10000):
            img = np.reshape(data[b'data'][j], (3, 32, 32))
            img = img.transpose(1, 2, 0)
            img_name = './train/' + label_name_list[data[b'labels'][j]] + '/' + str(j + (i - 1) * 10000) + '.jpg'
            cv2.imwrite(img_name, img)

    test_data_file = os.path.join(cifar10_data_dir, 'test_batch')
    data = unpickle(test_data_file)
    for i in range(10000):
        img = np.reshape(data[b'data'][i], (3, 32, 32))
        img = img.transpose(1, 2, 0)
        img_name = './test/' + label_name_list[data[b'labels'][i]] + '/' + str(i) + '.jpg'
        cv2.imwrite(img_name, img)


if __name__ == "__main__":
    main('C:/Users/Administrator/Desktop/cifar-10-batches-py')

你可能感兴趣的:(Python)