一般来说pytorch直接用就可以了,但tf好像不可以?或者有其他需求的,就需要单独提取出来…
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
import os
base = "/home/share/yanghaozhe/pytorchdataset/cifar-10-batches-py"
saves = "/home/share/yanghaozhe/cifar-imgs/"
os.makedirs(saves,exist_ok=True)
fs = os.listdir(base)
fs = [os.path.join(base,i) for i in fs if "batch" in i]
classes=('plane','car','bird','cat','deer','dog','frog','horse','ship','truck')
from matplotlib import pyplot as plt
def extract(label,filename,batch_label,img):
mode = "train" if b"train" in batch_label else "test"
fn = os.path.join(mode,classes[label],filename.decode())
... # 保存方法,如plt.imsave()
# 注意 plt 保存 png 是四通道argb, 保存 jpg 才是三通道rgb
return fn
def extract_dic(dic):
for label,filename in zip(dic[b"labels"],dic[b'filenames'],dic[b'data']):
print(extract(label,filename,dic[b'batch_label']))
for f in fs:
dic = unpickle(f)
extract_dic(dic)