CIFAR100數據集下載及介紹

一、CIFAR10數據集下載

import urllib.request
import tarfile
import os

url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
filepath = "cifar-10-python.tar.gz"
if not os.path.isfile(filepath):
    result = urllib.request.urlretrieve(url, filepath)
    print('downloaded:', result)
     
if not os.path.exists("cifar-10-batches-py"):
    tfile = tarfile.open("cifar-10-python.tar.gz", 'r:gz')
    result = tfile.extractall('.')
    print('extracted:', result)
else:
    print('Data has existed.')

CIFAR-10(Canadian Institute For Advanced Research)是一個經典的圖像分類數據集,共有10個類別,每個類別有6000張32*32的彩色圖片,其中50000張作為訓練集,10000張作為測試集。

二、CIFAR10數據集讀取

import pickle
import numpy as np
 
def load_CIFAR_batch(filename):
    with open(filename, 'rb') as f:
        datadict = pickle.load(f, encoding='latin1')
        X = datadict['data']
        Y = datadict['labels']
        X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")
        Y = np.array(Y)
        return X, Y
 
def load_CIFAR10(ROOT):
    xs = []
    ys = []
    for b in range(1,6):
        f = os.path.join(ROOT, 'data_batch_%d' % (b,))
        X, Y = load_CIFAR_batch(f)
        xs.append(X)
        ys.append(Y)
    Xtr = np.concatenate(xs)
    Ytr = np.concatenate(ys)
    del X, Y
    Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
    return Xtr, Ytr, Xte, Yte

CIFAR-10數據集中包含5個批量的訓練數據,每個批量大小為10000,測試數據包含一個批量,大小也為10000。以上代碼可以讀取CIFAR-10數據集並將其轉化為易於處理的numpy數組。

三、CIFAR10數據集介紹

import numpy as np
import matplotlib.pyplot as plt
 
def visualize_CIFAR10_data(X_train, y_train, classes, samples_per_class=7):
    num_classes = len(classes)
    for y, cls in enumerate(classes):
        idxes = np.flatnonzero(y_train == y)
        idxes = np.random.choice(idxes, samples_per_class, replace=False)
        for i, idx in enumerate(idxes):
            plt_idx = i * num_classes + y + 1
            plt.subplot(samples_per_class, num_classes, plt_idx)
            plt.imshow(X_train[idx].astype('uint8'))
            plt.axis('off')
            if i == 0:
                plt.title(cls)
    plt.show()

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
    cifar10_dir = '../datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
         
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]
         
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image
     
    return X_train, y_train, X_val, y_val, X_test, y_test

CIFAR-10數據集是由TensorFlow提供的,其中包括訓練、測試、驗證集,每張圖片都有一個標籤,總共10個類別。以上代碼用於可視化數據集以及獲取數據集。

四、CIFAR100數據集下載

import urllib.request
import tarfile
import os

url = "https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz"
filepath = "cifar-100-python.tar.gz"
if not os.path.isfile(filepath):
    result = urllib.request.urlretrieve(url, filepath)
    print('downloaded:', result)
     
if not os.path.exists("cifar-100-python"):
    tfile = tarfile.open("cifar-100-python.tar.gz", 'r:gz')
    result = tfile.extractall('.')
    print('extracted:', result)
else:
    print('Data has existed.')

CIFAR-100數據集共有100個類別,每個類別有600張32*32的彩色圖片,其中50000張作為訓練集,10000張作為測試集。CIFAR-100數據集的組織方式與CIFAR-10相似,但是它有更多的類別,更多的樣本。運行以上代碼可以下載CIFAR-100數據集。

五、CIFAR100數據集介紹

def load_CIFAR100(filename):
    with open(filename, 'rb') as f:
        datadict = pickle.load(f, encoding='latin1')
        X = datadict['data']
        Y = datadict['fine_labels']
        X = X.reshape(50000, 3, 32, 32).transpose(0,2,3,1).astype("float")
        Y = np.array(Y)
        return X, Y

def visualize_CIFAR100_data(X_train, y_train, classes, samples_per_class=7):
    num_classes = len(classes)
    for y, cls in enumerate(classes):
        idxes = np.flatnonzero(y_train == y)
        idxes = np.random.choice(idxes, samples_per_class, replace=False)
        for i, idx in enumerate(idxes):
            plt_idx = i * num_classes + y + 1
            plt.subplot(samples_per_class, num_classes, plt_idx)
            plt.imshow(X_train[idx].astype('uint8'))
            plt.axis('off')
            if i == 0:
                plt.title(cls)
    plt.show()

def get_CIFAR100_data(num_training=49000, num_validation=1000, num_test=10000):
    cifar100_dir = '../datasets/cifar-100-python'
    X_train, y_train = load_CIFAR100(os.path.join(cifar100_dir, 'train'))
    X_test, y_test = load_CIFAR100(os.path.join(cifar100_dir, 'test'))
    classes = ['beaver', 'dolphin', 'otter', 'seal', 'whale','aquarium fish', 'flatfish', 'ray', 'shark', 'trout','orchids', 'poppies', 'roses', 'sunflowers', 'tulips','bottles', 'bowls', 'cans', 'cups', 'plates','apples', 'mushrooms', 'oranges', 'pears', 'sweet peppers','clock', 'computer keyboard', 'lamp', 'telephone', 'television','bed', 'chair', 'couch', 'table', 'wardrobe','bee', 'beetle', 'butterfly', 'caterpillar', 'cockroach','bear', 'leopard', 'lion', 'tiger', 'wolf','bridge', 'castle', 'house', 'road', 'skyscraper','cloud', 'forest', 'mountain', 'plain', 'sea','camel', 'cattle', 'chimpanzee', 'elephant', 'kangaroo','fox', 'porcupine', 'possum', 'raccoon', 'skunk','crab', 'lobster', 'snail', 'spider', 'worm','baby', 'boy', 'girl', 'man', 'woman','crocodile', 'dinosaur', 'lizard', 'snake', 'turtle','hamster', 'mouse', 'rabbit', 'shrew', 'squirrel','maple', 'oak', 'palm', 'pine', 'willow','bicycle', 'bus', 'motorcycle', 'pickup truck', 'train','lawn-mower', 'rocket', 'streetcar', 'tank', 'tractor']
    num_classes = len(classes)
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image
    return X_train, y_train, X_val, y_val, X_test, y_test, classes

CIFAR-100數據集是一個更加複雜的數據集,它有更多的類別,更多的樣本。以上代碼也用於可視化數據集以及獲取數據集。

六、CIFAR100數據集大小

CIFAR-100數據集大小為169MB,可以從以下鏈接中下載:https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz

七、CIFAR10數據集格式

CIFAR-10數據集中的每個批量都是一個Python Pickle字典,其中包含以下鍵:

data: 一個10000 * 3072的numpy數組,第一個維度是圖像的索引,第二個維度是展平的圖像像素值,該數組中的值在0到255之間。

labels: 由大小為10000的1D列表組成的一個長度為10000的numpy數組,其中每個元素是一個類別ID。

八、CIFAR100數據集準確率

CIFAR-100的分類準確率通常在70%到75%之間,具體取決於所用的演算法和架構。與CIFAR-10相比,它是更加挑戰性的,但是它也是一個非常好的機器學習基準測試集。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190352.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-29 22:32
下一篇 2024-11-29 22:32

相關推薦

  • Python讀取CSV數據畫散點圖

    本文將從以下方面詳細闡述Python讀取CSV文件並畫出散點圖的方法: 一、CSV文件介紹 CSV(Comma-Separated Values)即逗號分隔值,是一種存儲表格數據的…

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • 如何用Python統計列表中各數據的方差和標準差

    本文將從多個方面闡述如何使用Python統計列表中各數據的方差和標準差, 並給出詳細的代碼示例。 一、什麼是方差和標準差 方差是衡量數據變異程度的統計指標,它是每個數據值和該數據值…

    編程 2025-04-29
  • Python多線程讀取數據

    本文將詳細介紹多線程讀取數據在Python中的實現方法以及相關知識點。 一、線程和多線程 線程是操作系統調度的最小單位。單線程程序只有一個線程,按照程序從上到下的順序逐行執行。而多…

    編程 2025-04-29
  • Python爬取公交數據

    本文將從以下幾個方面詳細闡述python爬取公交數據的方法: 一、準備工作 1、安裝相關庫 import requests from bs4 import BeautifulSou…

    編程 2025-04-29
  • Python兩張表數據匹配

    本篇文章將詳細闡述如何使用Python將兩張表格中的數據匹配。以下是具體的解決方法。 一、數據匹配的概念 在生活和工作中,我們常常需要對多組數據進行比對和匹配。在數據量較小的情況下…

    編程 2025-04-29
  • Python數據標準差標準化

    本文將為大家詳細講述Python中的數據標準差標準化,以及涉及到的相關知識。 一、什麼是數據標準差標準化 數據標準差標準化是數據處理中的一種方法,通過對數據進行標準差標準化可以將不…

    編程 2025-04-29
  • 如何使用Python讀取CSV數據

    在數據分析、數據挖掘和機器學習等領域,CSV文件是一種非常常見的文件格式。Python作為一種廣泛使用的編程語言,也提供了方便易用的CSV讀取庫。本文將介紹如何使用Python讀取…

    編程 2025-04-29
  • Python根據表格數據生成折線圖

    本文將介紹如何使用Python根據表格數據生成折線圖。折線圖是一種常見的數據可視化圖表形式,可以用來展示數據的趨勢和變化。Python是一種流行的編程語言,其強大的數據分析和可視化…

    編程 2025-04-29
  • Python如何打亂數據集

    本文將從多個方面詳細闡述Python打亂數據集的方法。 一、shuffle函數原理 shuffle函數是Python中的一個內置函數,主要作用是將一個可迭代對象的元素隨機排序。 在…

    編程 2025-04-29

發表回復

登錄後才能評論