Share via


手寫數位的 MNIST 資料庫

手寫數字的 MNIST 資料庫包含一個訓練集 (共有 60,000 個範例) 及一個測試集 (共有 10,000 個範例)。 數字已大小正規化且在固定大小的影像置中。

注意

Microsoft 會以「如目前」為基礎提供 Azure 開放資料集。 Microsoft 不會就您使用資料集做出任何明示或默示擔保或條件。 根據當地法律所允許的範圍,Microsoft 會免除因使用資料集而產生的任何損害或損失的所有責任,包括直接、衍生、特殊、間接、附帶或懲罰性。

此資料集是根據 Microsoft 接收來源資料的原始條款所提供。 資料集可能包含源自 Microsoft 的資料。

此資料集的來源是手寫數字的 MNIST 資料庫。 這是國家標準與技術 研究所發佈的 較大 NIST 手印表單和字元資料庫的 子集。

儲存位置

  • Blob 帳戶:azureopendatastorage
  • 容器名稱:mnist

有四個檔案直接位於容器中:

  • train-images-idx3-ubyte.gz:訓練集影像 (9,912,422 個位元組)
  • train-labels-idx1-ubyte.gz:訓練集標籤(28,881 個位元組)
  • t10k-images-idx3-ubyte.gz:測試集影像 (1,648,877 位元組)
  • t10k-labels-idx1-ubyte.gz:測試集標籤 (4,542 個位元組)

資料存取

Azure Notebooks

使用 Azure 機器學習表格式資料集,將 MNIST 載入資料框架。

如需 Azure 機器學習資料集的詳細資訊,請參閱 建立 Azure 機器學習資料集

將資料框架中取得完整的資料集

from azureml.opendatasets import MNIST

mnist = MNIST.get_tabular_dataset()
mnist_df = mnist.to_pandas_dataframe()
mnist_df.info()

取得定型和測試資料框架

mnist_train = MNIST.get_tabular_dataset(dataset_filter='train')
mnist_train_df = mnist_train.to_pandas_dataframe()
X_train = mnist_train_df.drop("label", axis=1).astype(int).values/255.0
y_train = mnist_train_df.filter(items=["label"]).astype(int).values

mnist_test = MNIST.get_tabular_dataset(dataset_filter='test')
mnist_test_df = mnist_test.to_pandas_dataframe()
X_test = mnist_test_df.drop("label", axis=1).astype(int).values/255.0
y_test = mnist_test_df.filter(items=["label"]).astype(int).values

繪製數位的一些影像

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# now let's show some randomly chosen images from the traininng set.
count = 0
sample_size = 30
plt.figure(figsize=(16, 6))
for i in np.random.permutation(X_train.shape[0])[:sample_size]:
    count = count + 1
    plt.subplot(1, sample_size, count)
    plt.axhline('')
    plt.axvline('')
    plt.text(x=10, y=-10, s=y_train[i], fontsize=18)
    plt.imshow(X_train[i].reshape(28, 28), cmap=plt.cm.Greys)
plt.show()

下載或掛接 MNIST 原始檔案 Azure 機器學習檔案資料集。

這只適用于以 Linux 為基礎的計算。 如需 Azure 機器學習資料集的詳細資訊,請參閱 建立 Azure 機器學習資料集

mnist_file = MNIST.get_file_dataset()
mnist_file
mnist_file.to_path()

將檔案下載到本機儲存體

import os
import tempfile

data_folder = tempfile.mkdtemp()
data_paths = mnist_file.download(data_folder, overwrite=True)
data_paths

掛接檔案。 當定型作業將在遠端計算上執行時很有用。

import gzip
import struct
import pandas as pd
import numpy as np

# load compressed MNIST gz files and return pandas dataframe of numpy arrays
def load_data(filename, label=False):
    with gzip.open(filename) as gz:
        gz.read(4)
        n_items = struct.unpack('>I', gz.read(4))
        if not label:
            n_rows = struct.unpack('>I', gz.read(4))[0]
            n_cols = struct.unpack('>I', gz.read(4))[0]
            res = np.frombuffer(gz.read(n_items[0] * n_rows * n_cols), dtype=np.uint8)
            res = res.reshape(n_items[0], n_rows * n_cols)
        else:
            res = np.frombuffer(gz.read(n_items[0]), dtype=np.uint8)
            res = res.reshape(n_items[0], 1)
    return pd.DataFrame(res)
import sys
mount_point = tempfile.mkdtemp()
print(mount_point)
print(os.path.exists(mount_point))

if sys.platform == 'linux':
  print("start mounting....")
  with mnist_file.mount(mount_point):
    print("list dir...")
    print(os.listdir(mount_point))
    print("get the dataframe info of mounted data...")
    train_images_df = load_data(next(path for path in data_paths if path.endswith("train-images-idx3-ubyte.gz")))
    print(train_images_df.info())

Azure Databricks

使用 Azure 機器學習表格式資料集,將 MNIST 載入資料框架。

如需 Azure 機器學習資料集的詳細資訊,請參閱 建立 Azure 機器學習資料集

將資料框架中取得完整的資料集

# This is a package in preview.
from azureml.opendatasets import MNIST

mnist = MNIST.get_tabular_dataset()
mnist_df = mnist.to_spark_dataframe()
display(mnist_df.limit(5))

下載或掛接 MNIST 原始檔案 Azure 機器學習檔案資料集。

這只適用于以 Linux 為基礎的計算。 如需 Azure 機器學習資料集的詳細資訊,請參閱 建立 Azure 機器學習資料集

mnist_file = MNIST.get_file_dataset()
mnist_file
mnist_file.to_path()

將檔案下載到本機儲存體

import os
import tempfile

mount_point = tempfile.mkdtemp()
mnist_file.download(mount_point, overwrite=True)

掛接檔案。 當定型作業將在遠端計算上執行時很有用。

import gzip
import struct
import pandas as pd
import numpy as np

# load compressed MNIST gz files and return numpy arrays
def load_data(filename, label=False):
    with gzip.open(filename) as gz:
        gz.read(4)
        n_items = struct.unpack('>I', gz.read(4))
        if not label:
            n_rows = struct.unpack('>I', gz.read(4))[0]
            n_cols = struct.unpack('>I', gz.read(4))[0]
            res = np.frombuffer(gz.read(n_items[0] * n_rows * n_cols), dtype=np.uint8)
            res = res.reshape(n_items[0], n_rows * n_cols)
        else:
            res = np.frombuffer(gz.read(n_items[0]), dtype=np.uint8)
            res = res.reshape(n_items[0], 1)
    return pd.DataFrame(res)
import sys
mount_point = tempfile.mkdtemp()
print(mount_point)
print(os.path.exists(mount_point))
print(os.listdir(mount_point))

if sys.platform == 'linux':
  print("start mounting....")
  with mnist_file.mount(mount_point):
    print(context.mount_point )
    print(os.listdir(mount_point))  
    train_images_df = load_data(os.path.join(mount_point, 'train-images-idx3-ubyte.gz'))
    print(train_images_df.info())

下一步

檢視開放式資料集目錄中 的其餘資料集