[深度学习][CIFRA数据处理] Python 读取CIFRA-10数据集

版权声明:博客地址:https://blog.csdn.net/weixin_41028208,未经博主允许不得转载。QQ:335848046。微博:小黑_songrn。 https://blog.csdn.net/weixin_41028208/article/details/85251553

CIFRA-10数据集介绍:https://blog.csdn.net/weixin_41028208/article/details/85145776

CIFRA-10数据集结构

以Python加载,每个批处理文件都包含一个字典,包含以下elements:

  • data
    • 一个 10000x3072 的uint8s numpy数组。矩阵的每一行都存储一个32x32的彩色图像。前1024个条目包含红色通道值,下一个1024表示绿色,最后1024个表示蓝色。图像以行主顺序存储,因此数组的前32个条目是图像第一行的红色通道值。
  • labels
    • 0-9范围内的10000个数字列表。索引i处的数字表示阵列数据中第i个图像的标签。

数据集包含另一个名为batches.meta的文件。它也包含一个Python dictionary 对象。它有以下条目:

  • label_names
    • 一个10元素列表,为上述标签数组中的数字标签提供有意义的名称。例如,label_names[0]=="airplane",label_names [1]=="cars"等。

代码部分

# 读取训练集
images, labels = [], []
for filename in ['%s/data_batch_%d' % (directory, j) for j in range(1, 6)]:
    with open(filename, 'rb') as fo:
        if 'Windows' in platform.platform():
            cifar10 = pickle.load(fo, encoding='bytes')
        elif 'Linux' in platform.platform():
            cifar10 = pickle.load(fo, encoding='bytes')
    for i in range(len(cifar10[b"labels"])):
        image = numpy.reshape(cifar10[b"data"][i], (3, 32, 32))
        image = numpy.transpose(image, (1, 2, 0))
        image = image.astype(float)
        images.append(image)
    labels += cifar10[b"labels"]
images = numpy.array(images, dtype='float')
labels = numpy.array(labels, dtype='int')

# 读取测试集
images, labels = [], []
for filename in ['%s/test_batch' % (directory)]:
    with open(filename, 'rb') as fo:
        if 'Windows' in platform.platform():
            cifar10 = pickle.load(fo, encoding='bytes')
        elif 'Linux' in platform.platform():
            cifar10 = pickle.load(fo, encoding='bytes')
    for i in range(len(cifar10[b"labels"])):
        image = numpy.reshape(cifar10[b"data"][i], (3, 32, 32))
        image = numpy.transpose(image, (1, 2, 0))
        image = image.astype(float)
        images.append(image)
    labels += cifar10[b"labels"]
images = numpy.array(images, dtype='float')
labels = numpy.array(labels, dtype='int')

猜你喜欢

转载自blog.csdn.net/weixin_41028208/article/details/85251553