python读取CIFAR数据集

CIFAR10/100数据集链接地址:http://www.cs.toronto.edu/~kriz/cifar.html

读取数据集函数

import numpy as np
import os
#网站给出的读取CIFAR10的函数
def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
        # print(dict.keys())
        #可以看出这个字典的keys
        # print(dict.__contains__(b'data'))
    X = dict[b'data']
    Y = dict[b'labels']
        #将(10000,3072)转变成为图片格式,其中转置函数是为了符合通道在最后的位置
    X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float")
    Y = np.array(Y)
    return X,Y

#输入文件目录,输出数据集的训练集数据和标签,测试集数据和标签
def load_cifar(ROOT):
    xs = []
    ys = []
    for b in range(1,6):
        f = os.path.join(ROOT,'data_batch_%d' % b)
        X ,Y = unpickle(f)
        xs.append(X)
        ys.append(Y)
    # print(len(xs))
    Xtr = np.concatenate(xs)
    # print(Xtr.shape)
    Ytr = np.concatenate(ys)
    del X,Y
    Xte,Yte = unpickle(os.path.join(ROOT,'test_batch'))
    return Xtr,Ytr,Xte,Yte
load_cifar('D:/PyCharm 2018.3.5/workplace/labtest/cifar-10-batches-py'

 

你可能感兴趣的:(TensorFlow)