def unpickle(file):
import cPickle
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
meta_data = unpickle("./batches.meta")
meta_data
{'label_names': ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'], 'num_cases_per_batch': 10000, 'num_vis': 3072}
data_batch_1 = unpickle("./data_batch_1")
data_batch_1.__class__.__name__
'dict'
data_batch_1.keys()
['data', 'labels', 'batch_label', 'filenames']
print data_batch_1["labels"][0]
print meta_data["label_names"][data_batch_1["labels"][0]]
6 frog
# 最初にRの1024次元、次にGの1024次元、最後にBの1024次元が格納
print data_batch_1["data"][0].shape
print data_batch_1["data"][0].reshape((3, 32, 32))
(3072,) [[[ 59 43 50 ..., 158 152 148] [ 16 0 18 ..., 123 119 122] [ 25 16 49 ..., 118 120 109] ..., [208 201 198 ..., 160 56 53] [180 173 186 ..., 184 97 83] [177 168 179 ..., 216 151 123]] [[ 62 46 48 ..., 132 125 124] [ 20 0 8 ..., 88 83 87] [ 24 7 27 ..., 84 84 73] ..., [170 153 161 ..., 133 31 34] [139 123 144 ..., 148 62 53] [144 129 142 ..., 184 118 92]] [[ 63 45 43 ..., 108 102 103] [ 20 0 0 ..., 55 50 57] [ 21 0 8 ..., 50 50 42] ..., [ 96 34 26 ..., 70 7 20] [ 96 42 30 ..., 94 34 34] [116 94 87 ..., 140 84 72]]]
import matplotlib.pyplot as plt
%matplotlib inline
# transposeで(row, column, chnnel)の順番に並び替えている
plt.imshow(data_batch_1["data"][0].reshape((3, 32, 32)).transpose(1, 2, 0))
<matplotlib.image.AxesImage at 0x1092e1550>