完成CIFAR-10數據集可視化
-
① CIFAR-10數據集包含60000個32*32的彩色圖像,共有10類。有50000個訓練圖像和10000個測試圖像。
數據集分爲5個訓練塊和1個測試塊,每個塊有10000個圖像。測試塊包含從每類隨機選擇的1000個圖像。訓練塊以隨機的順序包含這些圖像,但一些訓練塊可能比其它類包含更多的圖像。訓練塊每類包含5000個圖像。
②data——1個10000*3072大小的uint8s數組。數組的每行存儲1張32*32的圖像。第1個1024包含紅色通道值,下1個包含綠色,最後的1024包含藍色。圖像存儲以行順序爲主,所以數組的前32列爲圖像第1行的紅色通道值。
labels——1個10000數的範圍爲0~9的列表。索引i的數值表示數組data中第i個圖像的標籤。③數據集中包含另外1個叫batches.meta的文件。它也包含1個Python字典對象。有如下列元素:
label_names——1個10元素的列表,給labels中的數值標籤以有意義的名稱。例如,label_names[0] == “airplane”, label_names[1] == “automobile”等。
- 下載數據集
CIFAR-10數據集下載鏈接 - 運行python代碼
-
# -*- coding:utf-8 -*- # QQ2737499951奇蹟 # 2019.12.13 import pickle import numpy as np import matplotlib.pyplot as plt from PIL import Image import os classification = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] images_dir = "./images/" test_dir = "./test/" images_batch = "./cifar-10-batches-py/data_batch_" test_batch = "./cifar-10-batches-py/test_batch" def load_images(save_path, path_batch, num=1): data = [] labels = [] print("path_batch", path_batch) with open(path_batch, mode='rb') as file: data_dict = pickle.load(file, encoding='bytes') data += list(data_dict[b'data']) labels += list(data_dict[b'labels']) img = np.reshape(data, [-1, 3, 32, 32]) if not os.path.exists(save_path): os.mkdir(save_path) for i in range(img.shape[0]): r = img[i][0] g = img[i][1] b = img[i][2] ir = Image.fromarray(r) ig = Image.fromarray(g) ib = Image.fromarray(b) rgb = Image.merge("RGB", (ir, ig, ib)) if i < 10: # 只顯示前10張圖片 plt.imshow(rgb) plt.axis('off') # 不顯示座標軸 plt.show() label = classification[labels[i]] save_dir = save_path + label + "/" if not os.path.exists(save_dir): os.mkdir(save_dir) name = label + "_img-" + str(i) + ".png" rgb.save(save_dir + name, "PNG") # 文件夾下是RGB融合後的圖像 if __name__ == "__main__": # images print("正在保存images圖片:") for i in range(5): path = images_batch + str(i + 1) load_images(images_dir, path) print("保存images完畢.") # test print("正在保存test圖片:") load_images(test_dir, test_batch) print("保存test完畢.")
- 完成可視化過程