演算法研究 MNIST資料集

2021-09-07 20:17:37 字數 3269 閱讀 4872

mnist(mixed national institute of standards and technology database)是乙個計算機視覺資料集,它包含70000張手寫數字的灰度,其中每一張包含 28 x 28 個畫素點。

每一張都有對應的標籤,也就是對應的數字,例如上面這張的標籤就是 1。

60000行的訓練資料集是乙個形狀為 [60000, 784] 的張量,第乙個維度數字用來索引,第二個維度數字用來索引每張中的畫素點。在此張量裡的每乙個元素,都表示某張裡的某個畫素的強度值,值介於 0 和 1 之間。每乙個畫素點用乙個utf-8來表示。範圍是0~255,0表示是黑色(背景色),255表示白色(前景色)

60000行的訓練資料標籤集是乙個形狀為 [60000, 10] 的張量。採用one-hot vectors來表示0~9之間數字,從0開始到9,總共10個位置。乙個one-hot向量是[1,10]的張量,每一次只有乙個維度上面的數字是1,其他各個維度的數字都為0。數字n表示為乙個只有在第n個維度為1的1*10的向量。表示如下:

60000行的訓練資料標籤集是乙個形狀為[60000,1]的張量。每個維度表示對應的標籤值,標籤的訪問是0~9。

train-images-idx3-ubyte.gz: 60000個訓練 (9912422 bytes)

train-labels-idx1-ubyte.gz: 60000個訓練標籤 (28881 bytes)

t10k-images-idx3-ubyte.gz: 10000個測試 (1648877 bytes)

t10k-labels-idx1-ubyte.gz: 10000個測試標籤 (4542 bytes)

train-images-idx3-ubyte.gz解壓之後會生成乙個train-images.idx3-ubyte的檔案。該檔案的格式如下:

train-labels-idx1-ubyte.gz解壓之後,會生成train-labels.idx1-ubyte的檔案,檔案格式如下:

t10k-images-idx3-ubyte.gz解壓之後,會生成乙個t10k-images.idx3-ubyte的檔案,其格式如下:

t10k-labels-idx1-ubyte.gz解壓之後,生成t10k-labels.idx1-ubyte的檔案,格式如下:

基於上個章節中提到的4個檔案的資料格式,我們可以將data進行預處理,分割出影象檔案出來。

在處理binary資料之前,需要特別注意的是mnist資料集是採用大端格式(高位元組優先,即數字0x01020304在記憶體中儲存的情況是(從低位址到高位址)0x01,0x02,0x03,0x04)。

import numpy as np

import struct

import matplotlib.pyplot as plt

import time as time

trainimgfilename = r'/home/yml/桌面/mnist/train-images.idx3-ubyte'

tibinfile = open(trainimgfilename,'rb')

tibuf = tibinfile.read()

trainlabelfilename = r'/home/yml/桌面/mnist/train-labels.idx1-ubyte'

tlbinfile = open(trainlabelfilename,'rb')

tlbuf = tlbinfile.read()

tiindex = 0

tlindex = 0

timagic,tinumimages,tinumrows,tinumcolumn = struct.unpack_from('>iiii',tibuf,tiindex)

tiindex += struct.calcsize('>iiii')

tlmagic,tlnumlabel = struct.unpack_from('>ii',tlbuf,tlindex)

tlindex += struct.calcsize('>ii')

fig = plt.figure()

plotwindow = fig.add_subplot(111)

for i in range(tinumimages):

im = struct.unpack_from('>784b',tibuf,tiindex)

tiindex += struct.calcsize('>784b')

im = np.array(im)

im = im.reshape(28,28)

label = struct.unpack_from('>b', tlbuf,tlindex)

tlindex += struct.calcsize('>b')

print(label)

label = label[0]

plt.title('true number :%d'%label)

plt.axis('off')

plt.imshow(im,cmap='gray')

plt.pause(1)

tibinfile.close()

tlbinfile.close()

plt.close()

參考文獻

MNIST資料集介紹

mnist資料集包含了6w張作為訓練資料,1w作為測試資料。在mnist資料集中,每一張都代表了0 9中的乙個數字,的大小都是28 28,且數字都會出現在的正中間。資料集包含了四個檔案 t10k images idx3 ubyte.gz 測試資料 t10k labels idx1 ubyte.gz ...

Mnist資料集簡介

1,基本概念 mnist是乙個非常有名的手寫體數字識別資料集,在很多資料中,這個資料集都會被用作深度學習的入門樣例。而tensorflow的封裝讓使用mnist資料集變得更加方便。mnist資料集是nist資料集的乙個子集,mnist 資料集可在 獲取,它包含了四個部分 1 training set...

MNIST資料集介紹

大多數示例使用手寫數字的mnist資料集 1 該資料集包含60,000個用於訓練的示例和10,000個用於測試的示例。這些數字已經過尺寸標準化並位於影象中心,影象是固定大小 28x28畫素 其值為0到1。為簡單起見,每個影象都被平展並轉換為784 28 28 個特徵的一維numpy陣列。在我們的示例...