Python读入CIFAR-10数据库
https://www.cnblogs.com/hans209/p/6919851.html
CIFAR-10可以去http://www.cs.toronto.edu/~kriz/cifar.html下載(記得下載python格式)
?
CIFAR-10數據組成:
訓練集和測試集分別有50000和10000張圖片,RGB3通道,尺寸32×32,如下為data_batch_1的組成(使用pickle.load函數):
?
?可以看到有四部分,清晰明了
?
對于CIFAR-10數據的讀取,函數有兩個,如下所示
1 def load_CIFAR_batch(filename): 2 """ load single batch of cifar """ 3 with open(filename, 'rb') as f: 4 datadict = pickle.load(f,encoding='latin1') 5 X = datadict['data'] 6 Y = datadict['labels'] 7 X = X.reshape(10000, 3, 32,32).transpose(0,2,3,1).astype("float") 8 Y = np.array(Y) 9 return X, Y?
——————————————————————————————————————————————
1 def load_CIFAR10(ROOT):2 """ load all of cifar """3 xs = []4 ys = []5 for b in range(1,6):6 f = os.path.join(ROOT, 'data_batch_%d' % (b, ))7 X, Y = load_CIFAR_batch(f)8 xs.append(X) 9 ys.append(Y) 10 Xtr = np.concatenate(xs)#使變成行向量 11 Ytr = np.concatenate(ys) 12 del X, Y 13 Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch')) 14 return Xtr, Ytr, Xte, Yte?
——————————————————————————————————————————————
其中有幾個語句要注意一下:
X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float")
起初,X的size為(10000, 3072(3*32*32))。首先reshape很好理解,最后astype的格式轉換也很好理解。
可是為什么要調用transpose,轉置軸呢?就我認為只需要把一幅圖像轉成行向量就可以了。是為了方便檢索嗎?
?
xs.append(X)將5個batch整合起來;np.concatenate(xs)使得最終Xtr的尺寸為(50000,32,32,3)
?
當然還需要一步Xtr_rows = Xtr.reshape(Xtr.shape[0], 32 * 32 * 3)使得每一副圖像稱為一個行向量,最終就有了50000個行向量(Xtr_rows的尺寸為(50000,3072))
——————————————————————————————————————————————
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的Python读入CIFAR-10数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2013计算机系统导论,计算机系统导论2
- 下一篇: python库——h5py入门讲解