在做 TensorFlow和Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的時(shí)候,需要利用到一個(gè)MNIST數(shù)據(jù)集,數(shù)據(jù)集的格式是以
.idx1-ubyte
后綴,包含60000個(gè)訓(xùn)練圖像知残。將這些圖像展示出來,需要利用到[struct模塊] iii.run比庄。
下載MNIST訓(xùn)練數(shù)據(jù)集
手動(dòng)下載
下載鏈接為: http://yann.lecun.com/exdb/mnist/ 下載好之后解壓就可以了求妹,網(wǎng)站好像被墻了?
mark
使用tensorflow自帶下載
可以看到佳窑,這個(gè)地方是有監(jiān)督學(xué)習(xí) (有l(wèi)abel這個(gè)東西嘛)
from tensorflow.examples.tutorials.mnist import input_data
# 下載mnist數(shù)據(jù)集
mnist = input_data.read_data_sets('/tmp/', one_hot=True)
# 數(shù)字(label)只能是0-9制恍,神經(jīng)網(wǎng)絡(luò)使用10個(gè)出口節(jié)點(diǎn)就可以編碼表示0-9;
# 1 -> [0,1.0,0,0,0,0,0,0,0] one_hot表示只有一個(gè)出口節(jié)點(diǎn)是hot
# 2 -> [0,0.1,0,0,0,0,0,0,0]
# 5 -> [0,0,0,0,0,1.0,0,0,0]
# /tmp是macOS的臨時(shí)目錄神凑,重啟系統(tǒng)數(shù)據(jù)丟失; Linux的臨時(shí)目錄也是/tmp
詳細(xì)步驟
讀取文件
with open(filename ,'rb') as f1:
buf1 = f1.read()
還有另外一種常用的方法净神,兩個(gè)方法目前來看沒有什么區(qū)別。
f1 = open(filename , 'rb')
buf = binfile.read() # 先使用二進(jìn)制方式把文件都讀進(jìn)來
跨過頭部區(qū)域
train-images-idx3-ubyte
TRAINING SET IMAGE FILE (train-images-idx3-ubyte):
[offset] [type] [value] [description]
0000 32 bit integer 0x00000803(2051) magic number
0004 32 bit integer 60000 number of images
0008 32 bit integer 28 number of rows
0012 32 bit integer 28 number of columns
0016 unsigned byte ?? pixel
0017 unsigned byte ?? pixel
........
xxxx unsigned byte ?? pixel
可以看到頭部有4個(gè)integer 類型,設(shè)置image_index += struct.calcsize('>IIII')
計(jì)算4個(gè)integer 值的位置强挫,然后image_index 直接跳過去。至于為什么用IIII薛躬,愿意的話可以點(diǎn)擊了解俯渤。
temp = struct.unpack_from('>784B', buf1, image_index)
# '>784B'的意思就是用大端法讀取784( 28*28 )個(gè)unsigned byte
im = np.reshape(temp,(28,28))
最后那句np.reshape(temp,(28,28))
是以下兩句的縮寫
im = np.array(im)
im = im.reshape(28,28)
train-labels-idx1-ubyte
可以看到頭部有2個(gè)integer 類型,同理型宝,label_index 直接跳過去八匠。
TRAINING SET LABEL FILE (train-labels-idx1-ubyte):
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (MSB first)
0004 32 bit integer 60000 number of items
0008 unsigned byte ?? label
0009 unsigned byte ?? label
........
xxxx unsigned byte ?? label
The labels values are 0 to 9.
顯示圖片
plt.imshow(im , cmap='gray')
應(yīng)該就可以看到圖片了,是一張5趴酣, 當(dāng)然頭部文件還是要有的
%matplotlib inline
import numpy as np
import struct
import matplotlib.pyplot as plt
path = 'E:\\Machine Learning\\train-images.idx3-ubyte'
with open(path,'rb') as f1:
buf1 = f1.read()
image_index = 0
image_index += struct.calcsize('>IIII')
temp = struct.unpack_from('>784B', buf1, image_index)
# '>784B'的意思就是用大端法讀取784( 28*28 )個(gè)unsigned byte
im = np.reshape(temp,(28,28))
plt.imshow(im , cmap='gray')
give me 5
多張圖片讀取
多張圖片
import numpy as np
import struct
import matplotlib.pyplot as plt
def readfile():
with open('E:\\Machine Learning\\train-images.idx3-ubyte','rb') as f1:
buf1 = f1.read()
with open('E:\\Machine Learning\\train-labels.idx1-ubyte','rb') as f2:
buf2 = f2.read()
return buf1, buf2
def get_image(buf1):
image_index = 0
image_index += struct.calcsize('>IIII')
im = []
for i in range(9):
temp = struct.unpack_from('>784B', buf1, image_index) # '>784B'的意思就是用大端法讀取784個(gè)unsigned byte
im.append(np.reshape(temp,(28,28)))
image_index += struct.calcsize('>784B') # 每次增加784B
return im
def get_label(buf2): # 得到標(biāo)簽數(shù)據(jù)
label_index = 0
label_index += struct.calcsize('>II')
return struct.unpack_from('>9B', buf2, label_index)
if __name__ == "__main__":
image_data, label_data = readfile()
im = get_image(image_data)
label = get_label(label_data)
for i in range(9):
plt.subplot(3, 3, i + 1)
title = u"標(biāo)簽對(duì)應(yīng)為:"+ str(label[i])
plt.title(title, fontproperties='SimHei')
plt.imshow(im[i], cmap='gray')
plt.show()
遇到的一些坑:
- 中文標(biāo)題亂碼的問題
plt.title(title, fontproperties='SimHei') # 后邊這個(gè)字體**SimHei**加上就好了
- 標(biāo)題內(nèi)部不能用+
在外部加好之后梨树,賦值給新變量,然后放進(jìn)title
即可