哎呀,整了一天,终于整完了。CIFRA-10的文件应该有过更新,文件已经变了,其存储方式也变了。所以按之前的数据读取方式,展示的图片都不正常。
现在的数据存储方式(对于python来说)是字典型。以前的第一个字节是label,剩余3072(32x32)为像素,感觉不对了。CIFRA官网的说法
import numpy as np
import cv2 as cv
from PIL import Image
import pickle
import glob
import os
filenames = glob.glob('cifar-10-batches-py/data_batch_*')
j = 0
for filename in filenames:
j += 1
image=[]
labels=[]
with open(filename,'rb') as file:
data = pickle.load(file,encoding='bytes')
image += list(data[b'data'])
labels += list(data[b'labels'])
img = np.reshape(image,[-1,3,32,32])
#print(image)
for i in range(len(image)):
img1 = np.transpose(img[i],[1,2,0])
if not os.path.exists('Image/train/batch_'+str(j)):
os.makedirs('Image/train/batch_'+str(j)+'/')
cv.imwrite('Image/train/batch_'+str(j)+'/'+str(i)+'.png',img1)
print(j)