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("保存完毕.")