1. 程式人生 > >[深度學習][CIFRA資料處理] Python 讀取CIFRA-10資料集

[深度學習][CIFRA資料處理] Python 讀取CIFRA-10資料集

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')