CIFAR-10 can go to http://www.cs.toronto.edu/~kriz/cifar.html download (remember to download the python format)
CIFAR-10 Data composition:
Training set and test set have 50000 and 10000 pictures respectively, RGB3 channel, size 32x32, the following is the composition of data_batch_1 (using Pickle.load function):
You can see that there are four parts, clear
For read CIFAR-10 data, there are two functions, as shown below
1 def load_cifar_batch (filename): 2 "" "Load single batch of Cifar" "3 with open (filename, ' RB ') as F:4 Datadi ct = 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) Xtr = np.concatenate (xs) #使变成行向量11 Ytr = np.concatenate (ys) del X, Y13 Xte, yte = Load_cifar_ Batch (Os.path.join (ROOT, ' Test_batch ')) return Xtr, Ytr, Xte, Yte
——————————————————————————————————————————————
There are several statements to note:
X = X.reshape (10000, 3, +, 1). Transpose (0, 2, 3,). Astype ("float")
At first, the size of X is (10000, 3072 (3*32*32)). First of all, reshape is well understood, and the final Astype format conversion is also well understood.
But why call transpose and transpose axes? I think it's just a matter of turning a picture into a vector. Is it for easy retrieval?
Xs.append (X) integrates 5 batch, Np.concatenate (XS) makes the final XTR size (50000,32,32,3)
Of course, it takes a step xtr_rows = Xtr.reshape (xtr.shape[0], 32 * 32 * 3) so that each image is called a row vector, and eventually there are 50,000 line vectors (xtr_rows dimensions are (50000,3072))
——————————————————————————————————————————————
In summary, for convenience, should not directly from the beginning do not call reshape (10000, 3, 3, 1). Astype ("float"), direct append and concatenate can not be exported XTR _rows, huh?
Know the Bo friends can discuss!
Python reads into the CIFAR-10 database