第4章分類のための線形ニューラルネットワーク
4.2.1. Loading the Dataset4.2.1 データセットの読み込み
Since the Fashion-MNIST dataset is so useful, all major frameworks provide preprocessed versions of it. We can download and read it into memory using built-in framework utilities.
英語のヒント
Fashion-MNISTデータセットはとても役に立つので、主要なフレームワークはどれも前処理済みの版を提供している。フレームワークに組み込まれたユーティリティを使って、これをダウンロードし、メモリに読み込める。
class FashionMNIST(d2l.DataModule): #@save
"""The Fashion-MNIST dataset."""
def __init__(self, batch_size=64, resize=(28, 28)):
super().__init__()
self.save_hyperparameters()
trans = transforms.Compose([transforms.Resize(resize),
transforms.ToTensor()])
self.train = torchvision.datasets.FashionMNIST(
root=self.root, train=True, transform=trans, download=True)
self.val = torchvision.datasets.FashionMNIST(
root=self.root, train=False, transform=trans, download=True)Fashion-MNIST consists of images from 10 categories, each represented by 6000 images in the training dataset and by 1000 in the test dataset. A test dataset is used for evaluating model performance (it must not be used for training). Consequently the training set and the test set contain 60,000 and 10,000 images, respectively.
英語のヒント
Fashion-MNISTは10カテゴリーの画像からなり、各カテゴリーは訓練データセットでは6000枚、テストデータセットでは1000枚の画像で表される。テストデータセットはモデルの性能を評価するのに使う(訓練に使ってはならない)。その結果、訓練集合とテスト集合には、それぞれ60,000枚と10,000枚の画像が含まれる。
data = FashionMNIST(resize=(32, 32))
len(data.train), len(data.val)(60000, 10000)The images are grayscale and upscaled to pixels in resolution above. This is similar to the original MNIST dataset which consisted of (binary) black and white images. Note, though, that most modern image data has three channels (red, green, blue) and that hyperspectral images can have in excess of 100 channels (the HyMap sensor has 126 channels). By convention we store an image as a tensor, where is the number of color channels, is the height and is the width.
英語のヒント
画像はグレースケールで、上では解像度画素に拡大している。これは、(二値の)白黒画像からなっていた元のMNISTデータセットに似ている。ただし、現代の画像データのほとんどは三つのチャネル(赤、緑、青)をもち、ハイパースペクトル画像は100を超えるチャネルをもちうることに注意してほしい(HyMapセンサーには126のチャネルがある)。慣例として、画像はのテンソルとして保存する。ここでは色のチャネル数、は高さ、は幅である。
data.train[0][0].shapetorch.Size([1, 32, 32])The categories of Fashion-MNIST have human-understandable names. The following convenience method converts between numeric labels and their names.
英語のヒント
Fashion-MNISTのカテゴリーには、人が理解できる名前が付いている。次の便利なメソッドは、数値のラベルとその名前とを相互に変換する。
@d2l.add_to_class(FashionMNIST) #@save
def text_labels(self, indices):
"""Return text labels."""
labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
return [labels[int(i)] for i in indices]