CIFAR-100数据集可视化图片

CIFAR数据集地址:http://www.cs.toronto.edu/~kriz/cifar.html

官方下载后的数据集文件是以byte形式存储的图像文件,如果我们想要可视化图片,则需要自行写一个脚本。
以下以CIFAR-100的测试集为例,它的文件名为test,从该文件中提取出10000张 32 × 32 32\times 32 32×32的图片,并保存每张图片的label到groud_truth.txt

import pickle as p
import numpy as np
from PIL import Image

def load_CIFAR_batch(filename):
    """ load single batch of cifar """
    with open(filename, 'rb')as f:
        datadict = p.load(f, encoding='bytes')
        # 以字典的形式取出数据
        X = datadict[b'data']
        Y = datadict[b'fine_labels']
        X = X.reshape(10000, 3, 32, 32)
        Y = np.array(Y)
        print(Y.shape)
        return X, Y


if __name__ == "__main__":
    imgX, imgY = load_CIFAR_batch("./data/cifar-100-python/test")
    with open('ground_truth.txt', 'a+') as f:
        for i in range(imgY.shape[0]):
            f.write('img'+str(i)+' '+str(imgY[i])+'\n')

    for i in range(imgX.shape[0]):
        imgs = imgX[i]
        img0 = imgs[0]
        img1 = imgs[1]
        img2 = imgs[2]
        i0 = Image.fromarray(img0)
        i1 = Image.fromarray(img1)
        i2 = Image.fromarray(img2)
        img = Image.merge("RGB",(i0,i1,i2))
        name = "img" + str(i)+".png"
        img.save("./pic1/"+name,"png")
    print("save successfully!")

生成的测试集图片:
CIFAR-100数据集可视化图片_第1张图片

你可能感兴趣的:(深度学习与pytorch)