プリンキピア

第22章付録:深層学習のための数学

22.9.1. Optical Character Recognition22.9.1 光学文字認識

MNIST (LeCun et al., 1998) is one of widely used datasets. It contains 60,000 images for training and 10,000 images for validation. Each image contains a handwritten digit from 0 to 9. The task is classifying each image into the corresponding digit.
英語のヒント
MNIST(LeCun et al., 1998)は、広く使われているデータセットの一つである。これには、訓練用の60,000枚の画像と検証用の10,000枚の画像が含まれる。各画像には、0から9までの手書きの数字が一つ含まれている。タスクは、各画像を対応する数字に分類することである。
Gluon provides a MNIST class in the data.vision module to automatically retrieve the dataset from the Internet. Subsequently, Gluon will use the already-downloaded local copy. We specify whether we are requesting the training set or the test set by setting the value of the parameter train to True or False, respectively. Each image is a grayscale image with both width and height of 28 with shape (28,28,1). We use a customized transformation to remove the last channel dimension. In addition, the dataset represents each pixel by an unsigned 8-bit integer. We quantize them into binary features to simplify the problem.
英語のヒント
Gluonは、インターネットからデータセットを自動的に取得するMNISTクラスをdata.visionモジュールで用意している。その後は、Gluonはすでにダウンロードしたローカルのコピーを使う。パラメータtrainの値をそれぞれTrueかFalseに設定することで、訓練集合とテスト集合のどちらを要求しているかを指定する。各画像は、幅と高さがどちらも28のグレースケール画像で、形は(28、28、1)である。最後のチャネルの次元を取り除くため、カスタムの変換を使う。さらに、データセットは各画素を符号なし8ビット整数で表す。問題を単純にするため、これらを二値の特徴に量子化する。
data_transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor(),
    lambda x: torch.floor(x * 255 / 128).squeeze(dim=0)
])

mnist_train = torchvision.datasets.MNIST(
    root='./temp', train=True, transform=data_transform, download=True)
mnist_test = torchvision.datasets.MNIST(
    root='./temp', train=False, transform=data_transform, download=True)
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./temp/MNIST/raw/train-images-idx3-ubyte.gz
100%|██████████| 9912422/9912422 [00:00<00:00, 115752065.81it/s]
Extracting ./temp/MNIST/raw/train-images-idx3-ubyte.gz to ./temp/MNIST/raw

Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./temp/MNIST/raw/train-labels-idx1-ubyte.gz
100%|██████████| 28881/28881 [00:00<00:00, 5234904.66it/s]
Extracting ./temp/MNIST/raw/train-labels-idx1-ubyte.gz to ./temp/MNIST/raw

Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./temp/MNIST/raw/t10k-images-idx3-ubyte.gz
100%|██████████| 1648877/1648877 [00:00<00:00, 43715298.68it/s]Extracting ./temp/MNIST/raw/t10k-images-idx3-ubyte.gz to ./temp/MNIST/raw


Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./temp/MNIST/raw/t10k-labels-idx1-ubyte.gz
100%|██████████| 4542/4542 [00:00<00:00, 21501725.47it/s]
Extracting ./temp/MNIST/raw/t10k-labels-idx1-ubyte.gz to ./temp/MNIST/raw
We can access a particular example, which contains the image and the corresponding label.
英語のヒント
画像と対応するラベルを含む、特定の例にアクセスできる。
image, label = mnist_train[2]
image.shape, label
(torch.Size([28, 28]), 4)
Our example, stored here in the variable image, corresponds to an image with a height and width of 28 pixels.
英語のヒント
ここで変数imageに格納した例は、高さと幅が28画素の画像に対応する。
image.shape, image.dtype
(torch.Size([28, 28]), torch.float32)
Our code stores the label of each image as a scalar. Its type is a 32-bit integer.
英語のヒント
コードは各画像のラベルをスカラーとして格納する。その型は32ビット整数である。
label, type(label)
(4, int)
We can also access multiple examples at the same time.
英語のヒント
複数の例に同時にアクセスすることもできる。
images = torch.stack([mnist_train[i][0] for i in range(10, 38)], dim=0)
labels = torch.tensor([mnist_train[i][1] for i in range(10, 38)])
images.shape, labels.shape
(torch.Size([28, 28, 28]), torch.Size([28]))
Let’s visualize these examples.
英語のヒント
これらの例を視覚化しよう。
d2l.show_images(images, 2, 9);