這個資料集就像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
#載入序列檔案
defload_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個粗標籤
defload_cifar_train
(filename)
:with
open
(filename,
'rb'
)as f:
datadict=load_pickle(f)
x=datadict[
'data'
] y=datadict[
'fine_labels'
] x=x.reshape(
50000,3
,32,32
).transpose(0,
2,3,
1).astype(
"float"
)#reshape()是在不改變矩陣的數值的前提下修改矩陣的形狀,transpose()對矩陣進行轉置
y=np.array(y)
return x,y
defload_cifar_test
(filename)
:with
open
(filename,
'rb'
)as f:
datadict=load_pickle(f)
x=datadict[
'data'
] y=datadict[
'fine_labels'
] x=x.reshape(
10000,3
,32,32
).transpose(0,
2,3,
1).astype(
"float"
)#reshape()是在不改變矩陣的數值的前提下修改矩陣的形狀,transpose()對矩陣進行轉置
y=np.array(y)
return x,y
#返回可以直接使用的資料集
defload_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一致,不過標籤不一樣,粗標籤中包含精標籤,但此**只使用了精標籤。 Python3 轉換 cifar100 資料集
cifar官網給出的python介面的檔案都是用python cpickle工具 pickled 的,可以看見 cifar 官網給出的例程是 python 2 def unpickle file import cpickle with open file,rb as fo dict cpickle....
資料集處理 CIFAR10
transform transforms.compose transforms.totensor transforms.normalize 0.5,0.5,0.5 0.5,0.5,0.5 trainset torchvision.datasets.cifar10 root cifar10 train...
CIFAR 10資料集讀取
參考 1 使用讀取方式pickle def unpickle file import pickle with open file,rb as fo dict pickle.load fo,encoding bytes return dict 返回的是乙個python字典 2 通過字典的內建函式,獲取...