cifar-100数据集处理
cifar-100数据集处理
这个数据集就像CIFAR-10,除了它有100个类,每个类包含600个图像。每类各有500个训练图像和100个测试图像。图⽚⼤⼩为32323,有两个标签,⼀个是粗标签coarse_labels = 20,⼀个是精标签fine_labels = 100。我选择使⽤精标签作为label:
import numpy as np
import random
import pickle
import platform
import os
#加载序列⽂件
def load_pickle(f):
version=platform.python_version_tuple()#判断python的版本
if version[0]=='2':
return pickle.load(f)
elif version[0]=='3':
return pickle.load(f,encoding='latin1')
raise ValueError("invalid python version:{}".format(version))
#处理原数据,cifar-100中有两个标签:fine_labels:表⽰0-99个精确标签;coarse_labels表⽰0-19个粗标签
def load_CIFAR_train(filename):
with open(filename,'rb')as f:
datadict=load_pickle(f)
X=datadict['data']
Y=datadict['fine_labels']
shape(50000,3,32,32).transpose(0,2,3,1).astype("float")
#reshape()是在不改变矩阵的数值的前提下修改矩阵的形状,transpose()对矩阵进⾏转置
Y=np.array(Y)
return X,Y
def load_CIFAR_test(filename):
with open(filename,'rb')as f:
datadict=load_pickle(f)
import pickle
X=datadict['data']
Y=datadict['fine_labels']
shape(10000,3,32,32).transpose(0,2,3,1).astype("float")
#reshape()是在不改变矩阵的数值的前提下修改矩阵的形状,transpose()对矩阵进⾏转置
Y=np.array(Y)
return X,Y
#返回可以直接使⽤的数据集
def load_CIFAR100(ROOT):
Xtr,Ytr = load_CIFAR_train(os.path.join(ROOT,'train'))
Xte,Yte=load_CIFAR_test(os.path.join(ROOT,'test'))
return Xtr,Ytr,Xte,Yte
datasets ='./cifar-100-python'
train_x,train_y,test_x,test_y = load_CIFAR100(datasets)
print('train_x shape:%s, train_y shape:%s'%(train_x.shape, train_y.shape))
print('test_x shape:%s, test_y shape:%s'%(test_x.shape, test_y.shape))
输出结果为:
train_x shape:(50000,32,32,3), train_y shape:(50000,)
test_x shape:(10000,32,32,3), test_y shape:(10000,)
输出结果与cifar-10⼀致,不过标签不⼀样,粗标签中包含精标签,但此代码只使⽤了精标签。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。