Dataset之MNIST:MNIST(手写数字图片识别及其ubyte.gz文件)数据集简介、下载、使用方法(包括数据增强)之详细攻略
Dataset之MNIST:MNIST(手寫數(shù)字圖片識別及其ubyte.gz文件)數(shù)據(jù)集簡介、下載、使用方法(包括數(shù)據(jù)增強,將已有MNIST數(shù)據(jù)集通過移動像素上下左右的方法來擴大數(shù)據(jù)集為初始數(shù)據(jù)集的5倍))之詳細(xì)攻略
目錄
MNIST數(shù)據(jù)集的簡介
1、mnist 對象中各個屬性的含義和大小
2、數(shù)據(jù)集的應(yīng)用—訓(xùn)練和預(yù)測
3、基于MNIST數(shù)據(jù)集最新算法預(yù)測準(zhǔn)確率對比
MNIST數(shù)據(jù)集的下載
1、基于python語言根據(jù)爬蟲技術(shù)自動下載MNIST數(shù)據(jù)集
2、TensorFlow的封裝下使用MNIST數(shù)據(jù)集
(1)、MNIST數(shù)據(jù)集下載及輸出基本信息
(2)、利用TF查看MNIST數(shù)據(jù)集中訓(xùn)練集的前20張圖片
(3)、查看mnist_train數(shù)據(jù)集
MNIST數(shù)據(jù)集的使用方法
1、數(shù)據(jù)集增強代碼演示
1.1、思路
1.2、代碼實現(xiàn)
MNIST數(shù)據(jù)集的簡介
? ? ?四個gz文件,一共大約11M左右。
? ? ? ? MNIST是一個非常有名的手寫體數(shù)字識別數(shù)據(jù)集(手寫數(shù)字灰度圖像數(shù)據(jù)集),在很多資料中,這個數(shù)據(jù)集都會被用作深度學(xué)習(xí)的入門樣例。
? ? ? ?MNIST數(shù)據(jù)集是由0 到9 的數(shù)字圖像構(gòu)成的。訓(xùn)練圖像有6 萬張,測試圖像有1 萬張。MNIST數(shù)據(jù)集是NIST數(shù)據(jù)集的一個子集,它包含了60000張圖片作為訓(xùn)練數(shù)據(jù),10000張圖片作為測試數(shù)據(jù)。每一張圖片都有對應(yīng)的標(biāo)簽數(shù)字,訓(xùn)練圖像一共高60000 張,供研究人員訓(xùn)練出合適的模型。測試圖像一共高10000 張,供研究人員測試訓(xùn)練的模型的性能。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 單張圖片樣本的矩陣表示
? ? ? ?在上圖中右側(cè)顯示了一張數(shù)字1的圖片,而右側(cè)顯示了這個圖片所對應(yīng)的像素矩陣。
? ? ? ?MNIST 數(shù)據(jù)集主要由一些手寫數(shù)字的圖片和相應(yīng)的標(biāo)簽組成,圖片一共高10 類, 分別對應(yīng)從0~9 ,共10 個阿拉伯?dāng)?shù)字。在MNIST數(shù)據(jù)集中的每一張圖片都代表了0~9中的一個數(shù)字。
? ? ? ?MNIST的圖像,每張圖片是包含28 像素× 28 像素的灰度圖像(1 通道),各個像素的取值在0 到255 之間。每個圖像數(shù)據(jù)都相應(yīng)地標(biāo)有數(shù)字標(biāo)簽。每張圖片都由一個28 ×28 的矩陣表示,每張圖片都由一個784 維的向量表示(28*28=784),如圖所示。圖片的大小都為28*28,且數(shù)字都會出現(xiàn)在圖片的正中間。?處理后的每一張圖片是一個長度為784的一維數(shù)組,這個數(shù)組中的元素對應(yīng)了圖片像素矩陣中的每一個數(shù)字。
1、mnist 對象中各個屬性的含義和大小
文件名 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 大小 ? ? ? ? ? ? ? ? ? ?特點
train-images-idx3-ubyte.gz ? ? ?≈9.45 MB ? ? ? ? ? 訓(xùn)練圖像數(shù)據(jù)
train-labels-idx 1-ubyte.gz ? ? ?≈0.03MB ??? ? ? ? ? 訓(xùn)練圖像的標(biāo)
ti Ok-images-idx3-ubyte.gz ? ?≈?t.57MB??? ? ? ? ? ?測試圖像數(shù)據(jù)
t l Ok-labels-idxl-ubyte.gz ? ? ?≈4.4KB ???? ? ? ? ? ? 測試圖像的標(biāo)
? ? ? ? 原始的MNIST 數(shù)據(jù)集中包含60000 張訓(xùn)練圖片和10000 張測試圖片。
? ? ? ? 而在TensorFlow 中,又將原先的60000 張訓(xùn)練圖片重新劃分成了新的55000張訓(xùn)練圖片和5000張驗證圖片。所以在mnist 對象中,數(shù)據(jù)一共分為三部分: mnist.train 是訓(xùn)練圖片數(shù)據(jù), mnist. validation 是驗證圖片數(shù)據(jù),mnist.test是測試圖片數(shù)據(jù),這正好對應(yīng)了機器學(xué)習(xí)中的訓(xùn)練集、驗證集和測試集。一般來說,會在訓(xùn)練集上訓(xùn)練模型,通過模型在驗證集上的表現(xiàn)調(diào)整參數(shù),最后通過測試集確定模型的性能。
2、數(shù)據(jù)集的應(yīng)用—訓(xùn)練和預(yù)測
? ? ? ? ?這些圖像可以用于學(xué)習(xí)和推理。MNIST數(shù)據(jù)集的一般使用方法是,先用訓(xùn)練圖像進(jìn)行學(xué)習(xí),再用學(xué)習(xí)到的模型度量能在多大程度上對測試圖像進(jìn)行正確的分類。
? ? ? ? 在原始的MNIST 數(shù)據(jù)集中(官網(wǎng)MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges),可以找到多達(dá)68 種模型在該數(shù)據(jù)集上的準(zhǔn)確率數(shù)據(jù),包括相應(yīng)的論文出處。這些模型包括線性分類器、K 近鄰方法、普通的神經(jīng)網(wǎng)絡(luò)、卷積神經(jīng)網(wǎng)絡(luò)等。
3、基于MNIST數(shù)據(jù)集最新算法預(yù)測準(zhǔn)確率對比
相關(guān)鏈接:?Classification datasets results
MNIST數(shù)據(jù)集的下載
1、基于python語言根據(jù)爬蟲技術(shù)自動下載MNIST數(shù)據(jù)集
Dataset之MNIST:自定義函數(shù)mnist.load_mnist根據(jù)網(wǎng)址下載mnist數(shù)據(jù)集(四個ubyte.gz格式數(shù)據(jù)集文件)
Dataset之MNIST:MNIST(手寫數(shù)字圖片識別+ubyte.gz文件)數(shù)據(jù)集的下載(基于python語言根據(jù)爬蟲技術(shù)自動下載MNIST數(shù)據(jù)集)
2、TensorFlow的封裝下使用MNIST數(shù)據(jù)集
? ? ? ? TensorFlow的封裝讓使用MNIST數(shù)據(jù)集變得更加方便。
? ? ? ? MNIST數(shù)據(jù)集提供了4個下載文件,在tensorflow中可將這四個文件直接下載放于一個目錄中并加載,如下代碼所示,如果指定目錄中沒有數(shù)據(jù),那么tensorflow會自動去網(wǎng)絡(luò)上進(jìn)行下載。通過input_data.read_data_sets函數(shù)生成的類會自動將MNIST數(shù)據(jù)集劃分為train, validation和test三個數(shù)據(jù)集。
?? ? ?其中train這個集合內(nèi)含有55000張圖片,validation集合內(nèi)含有5000張圖片,這兩個集合組成了MNIST本身提供的訓(xùn)練數(shù)據(jù)集。test集合內(nèi)有10000張圖片,這些圖片都來自與MNIST提供的測試數(shù)據(jù)集。
(1)、MNIST數(shù)據(jù)集下載及輸出基本信息
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #這是TensorFlow 為了教學(xué)Mnist而提前設(shè)計好的程序 # number 1 to 10 data mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #TensorFlow 會檢測數(shù)據(jù)是否存在。當(dāng)數(shù)據(jù)不存在時,系統(tǒng)會自動,在當(dāng)前代碼py文件位置,自動創(chuàng)建MNIST_data文件夾,并將數(shù)據(jù)下載到該件夾內(nèi)。當(dāng)執(zhí)行完語句后,讀者可以自行前往MNIST_data/文件夾下查看上述4 個文件是否已經(jīng)被正確地下載 #若因網(wǎng)絡(luò)問題無法正常下載,可以前往MNIST官網(wǎng)http://yann.lecun.com/exdb/mnist/使用下載工具下載上述4 個文件, 并將它們復(fù)制到MNIST_data/文件夾中。#查看訓(xùn)練數(shù)據(jù)的大小 print(mnist.train.images.shape) #(55000, 784) print(mnist.train.labels.shape) #(55000, 10)#查看驗證數(shù)據(jù)的大小 print(mnist.validation.images.shape) #(5000, 784) print(mnist.validation.labels.shape) #(5000, 10)#查看測試數(shù)據(jù)的大小 print(mnist.test.images.shape) #(10000, 784) print(mnist.test.labels.shape) #(10000, 10)print(mnist.train.images[0,:]) #打印出第0張訓(xùn)練圖片對應(yīng)的向量表示(2)、利用TF查看MNIST數(shù)據(jù)集中訓(xùn)練集的前20張圖片
#利用TF查看MNIST數(shù)據(jù)集中訓(xùn)練集的前20張圖片 from tensorflow.examples.tutorials.mnist import input_data import scipy.misc import os# 讀取MNIST數(shù)據(jù)集。如果不存在會事先下載。 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# 我們把原始圖片保存在MNIST_data/raw/文件夾下 # 如果沒有這個文件夾會自動創(chuàng)建 save_dir = 'MNIST_data/raw/' if os.path.exists(save_dir) is False:os.makedirs(save_dir)# 保存前20張圖片 for i in range(20):# 請注意,mnist.train.images[i, :]就表示第i張圖片(序號從0開始)image_array = mnist.train.images[i, :]# TensorFlow中的MNIST圖片是一個784維的向量,我們重新把它還原為28x28維的圖像。image_array = image_array.reshape(28, 28)# 保存文件的格式為 mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpgfilename = save_dir + 'mnist_train_%d.jpg' % i# 將image_array保存為圖片# 先用scipy.misc.toimage轉(zhuǎn)換為圖像,再調(diào)用save直接保存。scipy.misc.toimage(image_array, cmin=0.0, cmax=1.0).save(filename)print('Please check: %s ' % save_dir)(3)、查看mnist_train數(shù)據(jù)集
打印出前20張圖片的One-hot編碼+前20張圖片所對應(yīng)的標(biāo)簽
#mnist_train數(shù)據(jù)集:打印出前20張圖片的One-hot編碼+前20張圖片所對應(yīng)的標(biāo)簽 from tensorflow.examples.tutorials.mnist import input_data import numpy as np # 讀取mnist數(shù)據(jù)集。如果不存在會事先下載。 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# 看前20張訓(xùn)練圖片的label for i in range(20):# 得到one-hot表示,形如(0, 1, 0, 0, 0, 0, 0, 0, 0, 0)one_hot_label = mnist.train.labels[i, :]# 通過np.argmax我們可以直接獲得原始的label# 因為只有1位為1,其他都是0label = np.argmax(one_hot_label)print('mnist_train中,第 %d 張圖片One-hot編碼'% i,mnist.train.labels[i,:])print('mnist_train中,第 %d 張圖片對應(yīng)的 label: %d' % (i, label))MNIST數(shù)據(jù)集的使用方法
1、數(shù)據(jù)集增強代碼演示
Dataset之MNIST:MNIST(手寫數(shù)字圖片識別)數(shù)據(jù)集簡介、下載、使用方法(包括數(shù)據(jù)集增強)之詳細(xì)攻略
1.1、思路
1.2、代碼實現(xiàn)
數(shù)據(jù)集增強(將已有MNIST數(shù)據(jù)集通過移動像素上下左右的方法來擴大數(shù)據(jù)集為初始數(shù)據(jù)集的5倍)
from __future__ import print_functionimport cPickle import gzip import os.path import randomimport numpy as npprint("Expanding the MNIST training set")if os.path.exists("../data/mnist_expanded.pkl.gz"): print("The expanded training set already exists. Exiting.") else:f = gzip.open("../data/mnist.pkl.gz", 'rb')training_data, validation_data, test_data = cPickle.load(f)f.close()expanded_training_pairs = []j = 0for x, y in zip(training_data[0], training_data[1]):expanded_training_pairs.append((x, y))image = np.reshape(x, (-1, 28))j += 1if j % 1000 == 0: print("Expanding image number", j)for d, axis, index_position, index in [(1, 0, "first", 0),(-1, 0, "first", 27),(1, 1, "last", 0),(-1, 1, "last", 27)]:new_img = np.roll(image, d, axis)if index_position == "first": new_img[index, :] = np.zeros(28)else: new_img[:, index] = np.zeros(28)expanded_training_pairs.append((np.reshape(new_img, 784), y))random.shuffle(expanded_training_pairs)expanded_training_data = [list(d) for d in zip(*expanded_training_pairs)]print("Saving expanded data. This may take a few minutes.")f = gzip.open("../data/mnist_expanded.pkl.gz", "w") cPickle.dump((expanded_training_data, validation_data, test_data), f)f.close()相關(guān)文章
Dataset:利用Python將已有mnist數(shù)據(jù)集通過移動像素上下左右的方法來擴大數(shù)據(jù)集為初始數(shù)據(jù)集的5倍
總結(jié)
以上是生活随笔為你收集整理的Dataset之MNIST:MNIST(手写数字图片识别及其ubyte.gz文件)数据集简介、下载、使用方法(包括数据增强)之详细攻略的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【LaTeX应用】文字方向,文字竖排
- 下一篇: win10 tensorflow MTC