下载python版本的cifar数据
把数据中的图片数据保存成图片,如下方法:
# -*- coding:utf-8 -*- import pickle as p import os import numpy as np import matplotlib.pyplot as plt import matplotlib.image as plimg 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='latin1') X = datadict['data'] Y = datadict['labels'] F = datadict['filenames'] X = X.reshape(10000, 3, 32, 32) Y = np.array(Y) F = np.array(F) return X, Y, F 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("./data/data/batches.meta") imgX, imgY, imgF = load_CIFAR_batch("./data/data/data_batch_1") print(imgX.shape) print("正在保存图片:") for i in range(imgX.shape[0]): imgs = imgX[i - 1] 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" name1 = imgF[i-1] lable = imgY[i-1] path = "./data/images/"+str(lable) if not os.path.exists(path): os.makedirs(path) img.save(path+"/"+name1,"png")#文件夹下是RGB融合后的图像 print("保存完毕.")