CIFAR10数据集的可视化

引用的CIFAR10数据集下载地址:http://www.cs.toronto.edu/~kriz/cifar.html

我没有下载CIFAR-10 binary version这里用的是CIFAR-10 python version


代码里文件路径用CIFAR10解压后的文件绝对路径

 load_CIFAR_Labels("D:/PythonProject/TF/cifar-10-batches-bin/batches.meta")
    imgX, imgY = load_CIFAR_batch("D:/PythonProject/TF/cifar-10-batches-bin/data_batch_1")

import pickle as p
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as plimg
from PIL import Image
def load_CIFAR_batch(filename):
    with open(filename, 'rb')as f:
        #datadict = p.load(f)
        datadict = p.load(f,encoding='latin1')
        X = datadict['data']
        Y = datadict['labels']
        X = X.reshape(10000, 3, 32, 32)
        Y = np.array(Y)
        return X, Y

def load_CIFAR_Labels(filename):
    with open(filename, 'rb') as f:
        lines = [x for x in f.readlines()]
        print(lines)


if __name__ == "__main__":
    load_CIFAR_Labels("D:/PythonProject/TF/cifar-10-batches-bin/batches.meta")
    imgX, imgY = load_CIFAR_batch("D:/PythonProject/TF/cifar-10-batches-bin/data_batch_1")
    print(imgX.shape)
    print("正在保存图片:")
    #for i in range(imgX.shape[0]):
    for i in range(30):
        #imgs = imgX[i - 1]
        imgs = imgX[i]
        img0 = imgs[0]
        img1 = imgs[1]
        img2 = imgs[2]
        i0 = Image.fromarray(img0)#从数据,生成image对象
        i1 = Image.fromarray(img1)
        i2 = Image.fromarray(img2)
        img = Image.merge("RGB",(i0,i1,i2))
        name = "img" + str(i)+'.png'
        img.save("D:/a/"+name,"png")#文件夹下是RGB融合后的图像
        for j in range(imgs.shape[0]):
#                img = imgs[j - 1]
                img = imgs[j]
                name = "img" + str(i) + str(j) + ".png"
                print("正在保存图片" + name)
                plimg.imsave("D:/b/" + name, img)#文件夹下是RGB分离的图像

    print("保存完毕.")








  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值