Python入门:可视化cifar10和cifar100数据集

cifar10数据集如下:

cifar100数据集如下:

可视化代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/6/28 11:07
# @Author  : HJH
# @Site    : 
# @File    : generate_cifar10_data_txt.py
# @Software: PyCharm

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


def load_cifar10(filename, num):
    with open(filename, 'rb')as f:
        datadict = p.load(f, encoding='latin1')
        images = datadict['data']
        labels = datadict['labels']
        images = images.reshape(num, 3, 32, 32)
        labels = np.array(labels)
        return images, labels.tolist()


def load_cifar100(filename, num):
    '''
    字典属性:
    b’firename’:图片的文件名
    b’batch_label’:图片对应批次
    b’fine_labels’:0~99,对应图像分类的标签
    b’coarse_labels’:0~19,对应图像超类的标签
    b’data’:10000X3072的NumPy数组,每行表示一个图片实例,其中每个实例都以32(长)X32(宽)X3(RGB)表示。
    '''
    with open(filename, 'rb')as f:
        datadict = p.load(f, encoding='latin1')
        images = datadict['data']
        labels = datadict['fine_labels']
        images = images.reshape(num, 3, 32, 32)
        labels = np.array(labels)
        return images, labels.tolist()


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


if __name__ == "__main__":
    # test/train/
    num = 10000
    load_labels_name("./images/cifar_100/meta")
    images, labels = load_cifar100("./images/cifar_100/test", num)
    print(images.shape)
    print("正在保存图片:")
    all_name = []
    with open('F:/DataSet/cifar/cifar100_test.txt', 'w') as f:
        for i in range(num):
            imgs = images[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("F:/DataSet/cifar/cifar100_test/" + name, "png")  # 文件夹下是RGB融合后的图像
            f.write('/root/hujiahui/DataSet/cifar100/test/' + name + '\t' + str(labels[i]) + '\n')
    print("保存完毕.")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值